public function generatePDF($imageDirectory, $prefix, $suffix, $html, $output_html = false, $inject_autoprint_js = true)
{
!$output_html && ($html = $this->remapAssetPaths($html));
!$output_html && ($html = $this->remapCanvasImagePaths($html));
$this->findOrCreateDirectory($imageDirectory);
$html_file = $suffix ? "{$imageDirectory}" . DIRECTORY_SEPARATOR . "{$prefix}_{$suffix}.html" : "{$imageDirectory}" . DIRECTORY_SEPARATOR . "{$prefix}.html";
$pdf_file = $suffix ? "{$imageDirectory}" . DIRECTORY_SEPARATOR . "{$prefix}_{$suffix}.pdf" : "{$imageDirectory}" . DIRECTORY_SEPARATOR . "{$prefix}.pdf";
$footer_file = $suffix ? "{$imageDirectory}" . DIRECTORY_SEPARATOR . "footer_{$suffix}.html" : "{$imageDirectory}" . DIRECTORY_SEPARATOR . 'footer.html';
$this->writeFile($html_file, $html);
$footer = $this->formatFooter($this->readFile(Yii::app()->basePath . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'print' . DIRECTORY_SEPARATOR . 'pdf_footer.php'), $this->left, $this->middle, $this->right, $this->patients, $this->barcodes, $this->docrefs);
$this->writeFile($footer_file, $footer);
if ($output_html) {
echo $html . $footer;
return true;
}
$top_margin = $this->top_margin ? '-T ' . $this->top_margin : '';
$bottom_margin = $this->bottom_margin ? '-B ' . $this->bottom_margin : '';
$left_margin = $this->left_margin ? '-L ' . $this->left_margin : '';
$right_margin = $this->right_margin ? '-R ' . $this->right_margin : '';
$nice = Yii::app()->params['wkhtmltopdf_nice_level'] ? 'nice -n' . Yii::app()->params['wkhtmltopdf_nice_level'] . ' ' : '';
$res = $this->execute($nice . escapeshellarg($this->wkhtmltopdf) . ' --footer-html ' . escapeshellarg($footer_file) . " --print-media-type {$top_margin} {$bottom_margin} {$left_margin} {$right_margin} " . escapeshellarg($html_file) . ' ' . escapeshellarg($pdf_file) . ' 2>&1');
if (!$this->fileExists($pdf_file) || $this->fileSize($pdf_file) == 0) {
if ($this->fileSize($pdf_file) == 0) {
$this->deleteFile($pdf_file);
}
throw new Exception("Unable to generate {$pdf_file}: {$res}");
}
$this->deleteFile($html_file);
$this->deleteFile($footer_file);
if ($pdf = $this->getPDFOptions($pdf_file)) {
if ($inject_autoprint_js) {
$pdf->injectJS('print(true);');
}
$pdf->disablePrintScaling();
$pdf->write();
}
return true;
}