Created
March 27, 2013 18:41
-
-
Save larscwallin/5256901 to your computer and use it in GitHub Desktop.
Simplx DOMPDF Snippet.
Depends on http://dompdf.github.com/
This file contains hidden or 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 | |
/* | |
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