76 protected $bin =
'/usr/bin/wkhtmltopdf';
78 protected $options = array();
79 protected $pageOptions = array();
80 protected $objects = array();
84 protected $tmpFiles = array();
89 const REGEX_HTML =
'/<.*html.*>/i';
96 if($options!==array())
105 if($this->tmpFile!==null)
106 unlink($this->tmpFile);
108 foreach($this->tmpFiles as $tmp)
118 public function addPage($input,$options=array())
120 $options[
'input'] = preg_match(self::REGEX_HTML, $input) ? $this->
createTmpFile($input) : $input;
121 $this->objects[] = array_merge($this->pageOptions,$options);
132 $options[
'input'] =
"cover $input";
133 $this->objects[] = array_merge($this->pageOptions,$options);
143 $options[
'input'] =
"toc";
144 $this->objects[] = $options;
158 copy($pdfFile,$filename);
168 public function send($filename=null)
173 header(
'Pragma: public');
174 header(
'Expires: 0');
175 header(
'Cache-Control: must-revalidate, post-check=0, pre-check=0');
176 header(
'Content-Type: application/pdf');
177 header(
'Content-Transfer-Encoding: binary');
178 header(
'Content-Length: '.filesize($pdfFile));
181 header(
"Content-Disposition: attachment; filename=\"$filename\"");
194 foreach($options as $key=>$val)
200 $this->options[] = $val;
202 $this->options[$key] = $val;
210 $this->pageOptions = $options;
226 if($this->tmp===null)
227 $this->tmp = sys_get_temp_dir();
237 if($this->tmpFile===null)
239 $tmpFile = tempnam($this->
getTmpDir(),
'tmp_WkHtmlToPdf_');
242 $this->tmpFile = $tmpFile;
247 return $this->tmpFile;
256 $command = $this->bin;
260 foreach($this->objects as $object)
262 $command .=
' '.$object[
'input'];
263 unset($object[
'input']);
267 return $command.
' '.$filename;
278 $descriptors = array(
279 1 => array(
'pipe',
'w'),
280 2 => array(
'pipe',
'w'),
282 $process = proc_open($command, $descriptors, $pipes);
284 if(is_resource($process)) {
286 $stdout = stream_get_contents($pipes[1]);
287 $stderr = stream_get_contents($pipes[2]);
291 $result = proc_close($process);
294 $this->error =
"Could not run command $command:\n$stderr";
296 $this->error =
"Could not run command $command";
298 return $this->error===null;
309 $tmpFile = tempnam($this->
getTmpDir(),
'tmp_WkHtmlToPdf_');
310 rename($tmpFile, ($tmpFile.=
'.html'));
311 file_put_contents($tmpFile, $content);
313 $this->tmpFiles[] = $tmpFile;
325 foreach($options as $key=>$val)
329 $out .=
" --$key $val";