/** * @return self */ public function convert($filePathIn, $filePathOut, $formats = ['html', 'pdf']) { $output = ['html' => '', 'pdf' => '']; $replace = ['content' => '', 'js' => '', 'css' => '']; try { $tpl = TemplatesRegistry::get($this->usedTemplate); foreach (['js', 'css'] as $type) { foreach ($tpl[$type] as $file) { if (!is_readable($file)) { throw new \Exception("Cannot load file {$file}."); } $file = file_get_contents($file); $replace[$type] .= $file . "\n"; } } if (file_exists("{$filePathOut}.html")) { unlink("{$filePathOut}.html"); } $replace['content'] = $this->mdParser->parse(file_get_contents($filePathIn)); $output['html'] = $this->latte->renderToString($tpl['layout'], $replace); $this->fs->write("{$filePathOut}.html", $output['html']); if (in_array('pdf', $formats)) { try { $html2pdf = new \HTML2PDF('P', 'A4', 'cs'); $html2pdf->setDefaultFont('dejavusans'); //$html2pdf->addFont('dejavusans'); $html2pdf->pdf->SetDisplayMode('real'); $output['html'] = str_replace('\\xe28087', "  ", $output['html']); $html2pdf->writeHTML($output['html']); $pdf = $html2pdf->Output("{$filePathOut}.pdf", 'S'); $this->fs->write("{$filePathOut}.pdf", $pdf); } catch (Html2PdfException $e) { $formatter = new ExceptionFormatter($e); echo "PDF: " . $formatter->getHtmlMessage(); } } if (!in_array('html', $formats)) { //unlink("{$filePathOut}.html"); } } catch (\InvalidArgumentException $e) { echo $e->getMessage(), "\n"; } return $this; }
<?php /** * Html2Pdf Library - example * * HTML => PDF converter * distributed under the LGPL License * * @package Html2pdf * @author Laurent MINGUET <*****@*****.**> * @copyright 2016 Laurent MINGUET */ require_once dirname(__FILE__) . '/../vendor/autoload.php'; use Spipu\Html2Pdf\Html2Pdf; use Spipu\Html2Pdf\Exception\Html2PdfException; use Spipu\Html2Pdf\Exception\ExceptionFormatter; try { ob_start(); include dirname(__FILE__) . '/res/exemple02.php'; $content = ob_get_clean(); $html2pdf = new Html2Pdf('P', 'A4', 'fr', true, 'UTF-8', array(15, 5, 15, 5)); $html2pdf->pdf->SetDisplayMode('fullpage'); $html2pdf->writeHTML($content); $html2pdf->Output('exemple02.pdf'); } catch (Html2PdfException $e) { $formatter = new ExceptionFormatter($e); echo $formatter->getHtmlMessage(); }