Skip to content

Instantly share code, notes, and snippets.

@larscwallin
Created March 27, 2013 18:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save larscwallin/5256901 to your computer and use it in GitHub Desktop.
Save larscwallin/5256901 to your computer and use it in GitHub Desktop.
Simplx DOMPDF Snippet. Depends on http://dompdf.github.com/
<?php
/*
simplx.dompdf
*/
$filename = isset($filename) ? $filename : false;
$outputto = isset($outputto) ? $outputto : 'file';
$directory = isset($directory) ? $directory : $modx->getOption('assets_path');
$html = isset($html) ? $html : false;
$papersize = isset($papersize) ? $papersize : 'a4';
$orientation = isset($orientation) ? $orientation : 'portrait';;
$success = false;
$domPdf;
function writePdf($html, $filename, $outputto, $directory) {
global $modx, $domPdf;
if (!isset($domPdf)) {
// Include the class
include(($modx->getOption('core_path').'components/simplx/dompdf/dompdf_config.inc.php'));
$domPdf = new DOMPDF();
$domPdf->set_paper($papersize, $orientation);
if(!$domPdf){
$modx->log(modX::LOG_LEVEL_ERROR, 'Exception in Snippet: simplx.dompdf, Unable to instantiate domPdf Object');
return false;
}
}
$domPdf->load_html($html);
$domPdf->render();
switch($outputto) {
//case 'header':
// $this->domPdf->stream($fileName);
// break;
case 'file':
if(!is_dir($directory)){
// This directory does not exist so we return false.
$modx->log(modX::LOG_LEVEL_ERROR, 'Exception in Snippet: simplx.dompdf, Directory "'.$directory.'" does not exist.');
return false;
}
$output = $domPdf->output();
$success = file_put_contents(($directory.$filename), $output);
if($success){
return true;
}else{
return false;
}
break;
case 'string':
$output = $domPdf->output();
return $output;
break;
}
return true;
}
if($html && $filename){
$success = writePdf($html,$filename,$outputto,$directory);
if($success){
return true;
}else{
$modx->log(modX::LOG_LEVEL_ERROR, 'Exception in Snippet: simplx.dompdf, Unable to create file "'.$fileName.'".');
return false;
}
}else{
$modx->log(modX::LOG_LEVEL_ERROR, 'Exception in Snippet: simplx.dompdf, Parameters "html" or "fileName" are missing or empty.');
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment