Last active
December 12, 2018 17:19
-
-
Save jonleverrier/a120bc53ec85f7c3622b758247663216 to your computer and use it in GitHub Desktop.
PDF Crowd MODX Plugin. Using the API from PDF Crowd, this plugin will convert a MODX resource in to PDF format and then save it to your web server in a specified location.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* PDF Crowd MODX Plugin | |
* | |
* Using the API from PDF Crowd, this plugin will convert a MODX resource in to | |
* PDF format and then save it to your web server in a specified location. | |
* | |
* The plugin is setup to trigger witin a certain MODX context. | |
* For example, a "pdf" context. | |
* | |
* To use this plugin, you will need a username and API key which is available from: | |
* https://pdfcrowd.com/user/sign_up/?pid=api-trial | |
* | |
* You will also need to download the php class and place it in: | |
* /assets/components/pdfcrowd/pdfcrowd.php | |
* | |
* Events: OnDocFormSave | |
* | |
* @author Jon Leverrier <[email protected]> | |
* version: 0.1 | |
* | |
*/ | |
$eventName = $modx->event->name; | |
switch($eventName) { | |
case 'OnDocFormSave': | |
// load some default settings | |
$pdfc_user = 'xxx'; | |
$pdfc_key = '123'; | |
$target_context = 'pdf'; | |
$pdf_dir = 'pdf/'; | |
// get current time to append to file name | |
$date_stamp = date('-dmY'); | |
// get the current page id | |
$id = (!empty($id) ? $id : $modx->resource->get('id')); | |
// make a full url from resource id | |
$target_url = $modx->makeUrl($id,'','','https'); | |
// get modResource object | |
$current_resouceid = $modx->getObject('modResource', $id); | |
// get current context_key from modResource object | |
$current_context = $current_resouceid->get('context_key'); | |
// get current alias from modResource object | |
$current_alias = $current_resouceid->get('alias'); | |
// if current_context is target_context do something, if not do nothing | |
if ($current_context == $target_context){ | |
require MODX_ASSETS_PATH . 'components/pdfcrowd/pdfcrowd.php'; | |
try | |
{ | |
// create the API client instance | |
$client = new \Pdfcrowd\HtmlToPdfClient($pdfc_user, $pdfc_key); | |
// run the conversion and write the result to a file | |
$client->convertUrlToFile($target_url, MODX_ASSETS_PATH . $pdf_dir . $current_alias . $date_stamp . ".pdf"); | |
} | |
catch(\Pdfcrowd\Error $why) | |
{ | |
// report the error to modx error log | |
$modx->log(modx::LOG_LEVEL_ERROR,'[ PDF Crowd ]' . $why); | |
} | |
} | |
else { | |
// do nothing | |
return; | |
} | |
break; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment