First of all my target is to write an Extras for modx to export website contents as epub file.
I searched already if something like that exists but I did not find anything. Does anyone know extras like that? Or can anyone suggest me the best way to do it in modx?
My thought is to gather all rendered html files and resources and then use an php based epub library to generate epub file.
But I did not find a way to get the rendered html files from modx.
I can get the template and I can also get the html snippet code but I need the whole html file.
MODX info:
MODX-Version: MODX Revolution 2.7.3-pl (traditional)
Versions-Codename: Revolution
There is multiple ways to go about this.
You could write a standalone PHP application that calls all the URLs on your site from an XML sitemap or an array of links and then downloads the rendered HTML using the file_get_contents() function (see doc here).
Something along the lines of this (following code is untested):
// [filename] => [URL]
$pages = array(
'index.html' => 'https://example.com/',
'contact.html' => 'https://example.com/contact.html',
);
foreach($pages as $filename => $link){
$filePath = $_SERVER['DOCUMENT_ROOT'].'/'.$filename;
$html= file_get_contents($link);
$handle = fopen($filePath,"w");
fwrite($handle,$html);
fclose($handle);
}
Alternatively you could write a snippet within MODX and get the resource URLs directly using xPDO. The following code will get the links and file names for all resources and exclude weblinks, symlinks and static resources in the process. If you're planning on implementing the following code within your package, you will need to adjust it slightly.
$resources = $modx->getIterator('modResource', array(
'class_key' => 'modDocument',
));
$pages = array();
foreach($resources as $resource){
$pages[$resource->get('alias').'.html'] = $modx->makeUrl($resource->get('id'), '', '', 'full');
}
Output of the $pages array in the above code would be as follows:
Array
(
[index.html] => https://example.com/
[test.html] => https://example.com/test.html
)
Related
I have a series of PDF files on my shared hosting webserver which I'm writing a PHP script for to catalogue them on the screen. I've added metadata to the PDF files - Document Title, Author and Subject. The filename is composed of the Author and Title so I can construct the catalogue text from that. However, I want to display the contents of the 'Subject' metadata field as well.
Because I'm using shared hosting, I cannot install any extra PHP extensions. They have the free version of PDFLib but this doesn't include any functions to load the PDF file or to extract metadata.
This is the script so far which just displays a list of the filenames...
function catalogue($folder){
$files = preg_grep('/^([^.])/', scandir($folder));
foreach($files as $file){
echo($file.'<br/>');
}
}
So, I've not made much progress :(
I've tried PDF_open_pdi_document() but this is not part of the installed PDFLib extension. I've tried PDF_pcos_get_string() but all I get with...
PDF_pcos_get_string($file,0,'author');
...is...
pdf_pcos_get_string(): supplied resource is not a valid pdf object resource
...and I can find literally ZERO help on the web for this function. Literally nothing!
I am running PHP 7.4 on the shared hosting.
Metadata aren't encrypted like the PDF, so you can use file_get_contents, find the pattern for the subject (<</Subject) and extract it using either a regex or a simple combination of strpos/substr.
Thank you #drdlp. I've used file_get_contents() to load in the PDF and extract and display the metadata.
function catalogue($folder){
$files = preg_grep('/^([^.])/', scandir($folder));
foreach($files as $file){
$page = file_get_contents($file);
$metadata = preg_match_all('/\/[^\(]*\(([^\/\)]*)/',$page,$matches);
$author = $matches[1][0];
$subject = $matches[1][4];
$title = $matches[1][5];
echo($title.'/'.$subject.'/'.$author.'<br>');
}
}
/
However, this is very slow for 40 odd PDF articles in a folder.
How can I speed this up?
I've begun experimenting with pdf.js for which I can load all the basic details from files first (filename etc) and then update them with Javascript after the page has loaded.
However, I clearly don't know enough about Javascript to make this work. This is what I have so far and I am very stuck. I've imported pdf.js from mozilla.github.io/pdf.js/build/pdf.js...
function pdf_metadata(file_url,id){
var pdfjsLib = window['pdfjs-dist/build/pdf'];
pdfjsLib.GlobalWorkerOptions.workerSrc = '//mozilla.github.io/pdf.js/build/pdf.worker.js';
var loadingTask = pdfjsLib.getDocument(file_url);
loadingTask.promise.then(function(pdf) {
pdf.getMetadata().then(function(details) {
console.log(details);
document.getElementById(id).innerHTML=details;
}).catch(function(err) {
console.log('Error getting meta data');
console.log(err);
});
});
}
The line console.log(details); outputs an object to the console. From there I have no idea how to extract any data at all. Therefore document.getElementById(id).innerHTML=details; displays nothing.
This is the object which is output to the console.
I am using the following modules:
media
media_youtube
Styles
and would like to render a thumbnail of a Youtube video in a template (.tpl). Which theme function should I use and with what params?
My best quess would be something like:
$my_media['style_name'] = 'unlinked_thumbnail';
print theme('file_styles',$my_media);
where $my_media is an array containing fid,uri,filename,filemime etc.
Since i'm very new to Drupal, all my attempts to make sense of the modules source code have failed. I feel like I have tried all possible combinations of style names defined in the youtube and styles module without getting any output.
Rendering the video itself however works just fine using
print theme('media_youtube_video',$my_media);
How do you guys do it?
Digging around in the media_youtube module code there's a function that will build this up for you in includes/media_youtube.formatters.inc called media_youtube_file_formatter_image_view(). You can use code like the following to render the thumbnail image:
// Make sure the correct include file is loaded
module_load_include('inc', 'media_youtube', '/includes/media_youtube.formatters.inc');
// Load the file
$file = file_load($my_media['fid']);
// Set up the settings array with your image style
$display['settings'] = array('image_style' => 'unlinked_thumbnail');
// Get the render array for the thumbnail image
$image_render_array = media_youtube_file_formatter_image_view($file, $display, LANGUAGE_NONE);
// Render it
print render($image_render_array);
One more example, which makes possible to render video thumbnail (vimeo, youtube, qbrick, etc.) with your custom image style.
$file - File object that you can easily get from media field.
$wrapper = file_stream_wrapper_get_instance_by_uri($file->uri);
$image_uri = $wrapper->getLocalThumbnailPath();
$image_rendered = theme('image_style', array(
'style_name' => 'YOUR_IMAGE_STYLE_HERE',
'path' => $image_uri
));
Please note, that if you want to render also image, than you need to check $file->type if it's video or image, because it will use different wrapper objects with different methods.
And if you want to print both thumb+video you could do this :
<?php print render($content['field_your_field']); ?>
If you in a tpl and want to use $variables - it will be something like:
<?php print render($variables['content']['field_url']['#object']->content['field_video']);?>
Hopu u can use it...
I guess no one was lucky to found the best solution of handling reports in php, specialy when it's a .doc/x report or file .... i searched for sometime and then i found phpdocx.com .. amazing php script, but it just doesn't work, and i don't know exactly where to find the output file ... and unfortunately the documentation doesn't help at any level ...
Now i need to know the way this script work .. i mean how results come out and become usable ... and what needs it take the script to work .. because it simply doesn't work on my local host .. i am using appache 2, php 5.2.6 ..
I don't actually need more than writing html with in ( a real doc format file, not rename a html file to .doc !! ), so if there is any solution ( without the COM Lib ... i am not on a windows server ) to generate real doc file with HTML .. please but it here
Thanks very much in advance :)
I guess no one was lucky to found the best solution of handling
reports in php, specialy when it's a .doc/x report or file
This is not the question corresponding to the title, but you should try OpenTBS.
It's an open source PHP library which builds DOCX with the technique of templates.
No temp directory, no extra exe needed. First create your DOCX, XLSX, PPTX with Ms Office, (ODT, ODS, ODP are also supported, that's OpenOffice files). Then you use OpenTBS to load the template and change the content using the Template Engine (easy, see the demo). At the end, you save the result where you need. It can be a new file, a download flow, a PHP binary string.
OpenTBS can also change pictures and charts in a document.
Demo page
Documentation
The documentation of PHPDocX has been greatly improved.
Have you tried to look at the PHPDocX tutorial?
You may also have a look at the Forum.
require_once "Path of phpdocx library/CreateDocx.inc";
$docx = new CreateDocx();
$html = 'your data will store in this variable';
$docx->embedHTML(
$html,
array(
'parseDivsAsPs' => true,
'downloadImages' => true,
'WordStyles' => array(
'<table>' => 'MediumGrid3-accent5PHPDOCX'
),
'tableStyle' => 'NormalTablePHPDOCX'
)
);
$docx->createDocx($varPublicPath.'/word_export_file/example1_'.time());
// this is location where your docx file will generate(inside word_export_file docx file will store)
Background: I have been attempting to read the rating that is assigned in Adobe Bridge CS3 using the creative commons Metadata toolkit for php without success. I am using shared hosting so I do not have an oppotunity to recompile php with different modules.
Is php code available that could be used to read the rating that is embedded in the .jpg file? I have read that this is an xmp (xml) formatted section within the file.
I'm posting my solution here in case someone else has a similiar problem and reads this thread. Here is what I found:
Windows Vista add the rating to the exif section embedded in the file
Adobe Bridge adds another section to the jpg file that contains data formatted in xml. The xml + data structure is referred to as the xmp file.
I hadn't yet processed the file with Adobe bridge, that is why I was unable to read the xmp data with the Metadata toolkit.
Using the Creative Commons - Metadata toolkit I was able to read the ratings using the following code. This code is part of a Drupal Module, some of the referenced variables are Drupal settings: variable_get() is a Drupal function to read a variable from a perssistent data store.
include_once("PHP_JPEG_Metadata_Toolkit_1.11/JPEG.php");
include_once("PHP_JPEG_Metadata_Toolkit_1.11/Photoshop_File_Info.php");
include_once("PHP_JPEG_Metadata_Toolkit_1.11/EXIF.php");
include_once("PHP_JPEG_Metadata_Toolkit_1.11/XMP.php");
$photodir = variable_get('rotate_images_sourcefiles_dir',"sites/default/files/imageNodes");
$rating_threshold = variable_get('rotate_images_rating_threshold',3);
$allImages=dir($photodir);
$filenames = scandir($photodir);
foreach($filenames as $filename){
$rating = null;
$info = pathinfo($filename);
if (strtolower($info['extension'])=="jpg"){
// First try to get the rating from the EXIF data, this is where it is stored by Windows Vista
$exif = get_EXIF_JPEG( $photodir . "/" . $filename );
$rating = $exif[0][18246]['Data'][0];
$jpeg_header = get_jpeg_header_data($photodir . "/" . $filename );
// If no rating was found in the EXIF data, it may be in the Adobe format xmp section
if ($rating == null){
if($jpeg_header != false){
$xmp = get_XMP_text($jpeg_header);
$xmpArray = read_XMP_array_from_text($xmp);
$rating = $xmpArray[0]['children'][0]['children'][0][attributes]['xap:Rating'];
}
}
}
}
I did need to modify the EXIF_Tags.php file in the metadata toolkit by adding an additional entry to the EXIF Tags array. I reported this to the author, but I don't believe he is maintaing the module any longer. The source is on sourceforge, but I don't know how to post a patch. So you may need to make the change to EXIF.php yourself to make the code work.
'EXIF' => array (
// Exif IFD
18246 => array ( 'Name' => "Rating",
'Type' => "Numeric" ),
Theoretically if you use fgets you should be able to read it. It would be helpful if you know where this section begins in terms of bytes into the file.
I have a PDF which has an image on it (a logo)
I want to open it with PHP and write some text over it and save it back to the file system.
I've done this with the Zend framework before but this project is using code igniter so I need either a standalone lib or a code igniter plugin.
thanks
Zend_Pdf is a standalone lib.
Zend Framework is deliberately designed with a Use-At-Will architecture, so you can use most components with no (or very little) dependencies on other components in the framework.
To use Zend_PDF in Code Igniter, place the Zend/Pdf folder into your CI project's include path, so it is accessible. Then include it with
// Load Zend_Pdf class
require_once('Zend/Pdf.php');
See this (general) tutorial:
http://devzone.zend.com/article/2525
In my esperience, the first question you should ask is: where the original pdf come from?
Do you want to create the pdf from php, as a template, and then insert the text on it in a second time?
Or you create the pdf in other ways, and then fulfill it via php?
In the first case, go with zend pdf, and write down your class to handle it.
In the second case, you may want to take a look to pdftk, that allow you to merge an fdf file with a PDF file.
Here an example with a forms (and the createFDF.php file), but the behavior can be applyed in many other ways...
Its quite simple:
<?php
require_once('createFDF.php');
$data = array(
'field_1' => 'Text 1',
'Field_2' => 'Text 2
);
$FDF_file = 'myfile.fdf';
$PDF_file = 'mypdf.pdf';
$PDF_source = 'your-pdf-original-file.pdf';
$fdf_data = createFDF($PDF_source, $data);
$fh = fopen($FDF_file, 'w');
fwrite($fh, $fdf_data);
fclose($fh);
?>
<h2>File FDF created.</h2>
<?php
passthru("pdftk $PDF_source fill_form $FDF_file output $PDF_file flatten");
?>
<h2>Pdf merged.</h2>
but, you will need to create the original pdf file with forms within by hand (or, as far as i know, there is no tools to create it via php)
You can try to set the following header:
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
// The PDF source is in original.pdf
readfile(base_url($pdf->URL));
?>