0
votes

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

1

1 Answers

1
votes

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
)