00001 <?php
00018 class ppPDF extends Plugin
00019 {
00020 var $fn_pdf;
00021 var $tempdir;
00022
00023
00024 var $paper_size = 'letter';
00025 var $paper_orientation = 'portrait';
00026
00027 function ppPDF()
00028 {
00029 $this->Plugin();
00030
00031 if(!defined('PDF_BACKEND')) {
00032 define('PDF_BACKEND', 'prince');
00033 }
00034
00035 $this->tempdir = defined('DIR_FS_TEMP') ? DIR_FS_TEMP : DS.'tmp';
00036 }
00037
00046 function convert($html, $filename='')
00047 {
00048 if(!$filename) {
00049 $filename = $this->fn_pdf = tempnam($this->tempdir, 'pdf');
00050 }
00051
00052 if(PDF_BACKEND == 'prince') {
00053 return $this->_convert_prince($html, $filename);
00054 }
00055
00056 if(PDF_BACKEND == 'dompdf') {
00057 return $this->_convert_dompdf($html, $filename);
00058 }
00059
00060 trigger_error('PDF_BACKEND is undefined or invalid');
00061 return false;
00062 }
00063
00071 function output($filename='')
00072 {
00073 header('Content-Type: application/pdf');
00074 header('Content-Length: '.filesize($this->fn_pdf));
00075 if($filename) {
00076 header('Content-Disposition: inline; filename="'.$filename.'"');
00077 }
00078 readfile($this->fn_pdf);
00079
00080
00081 @unlink($this->fn_pdf);
00082 $this->fn_pdf = '';
00083 }
00084
00091 function set_paper($size, $orientation='portrait')
00092 {
00093 $this->paper_size = $size;
00094 $this->paper_orientation = $orientation;
00095 }
00096
00097 function _convert_dompdf($html, $filename)
00098 {
00099 require_once(DIR_FS_APP.DS.'extlib'.DS.'dompdf'.DS.'dompdf_config.inc.php');
00100 $pdf = new DOMPDF();
00101 $pdf->set_paper($this->paper_size, $this->paper_orientation);
00102 $pdf->load_html($html);
00103 $pdf->render();
00104
00105 file_put_contents($filename, $pdf->output());
00106 return true;
00107 }
00108
00109 function _convert_prince($html, $filename)
00110 {
00111 $f_html = tempnam($this->tempdir, 'html');
00112 file_put_contents($f_html, $html);
00113
00114 $cmd = realpath(DIR_FS_APP.DS.'extlib'.DS.'prince'.DS.'bin'.DS.'prince');
00115 $cmd .= " --silent -i html -o \"$filename\" $f_html";
00116 system($cmd);
00117 @unlink($f_html);
00118 return true;
00119 }
00120
00121 }
00122
00123 ?>