Below following code generate UTF-8 encoded HTML content is exported to PDF file. Download mPDF library form GitHub using composer https://github.com/mpdf/mpdf
Composer using PHP:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '795f976fe0ebd8b75f26a6dd68f78fd3453ce79f32ecb33e7fd087d39bfeb978342fb73ac986cd4f54edd0dc902601dc') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
Downlaod mPDF:
php composer.phar require mpdf/mpdf
PHP code to generate PDF from HTML file or from URL
<?php
require_once dirname(__FILE__).'/mpdf/vendor/autoload.php';
$htmlContent = file_get_contents(htmlfile / URL of the page);
$mpdf = new MpdfMpdf(['tempDir' => '/tmp']); or $mpdf = new MpdfMpdf(); // With/without temporary folder location for pdf generator
$mpdf->setFooter($pdfType. ', Date: {DATE j-m-Y}, Page: {PAGENO}'); // Date and page number to PDF footer.
$mpdf->WriteHTML($htmlContent);
$mpdf->Output($tmpPdfFile); // PDF file location
?>
If need to add pagebreak in PDF add following HTML code to HTML. This will add new page in PDF.
<div class='pageBreak'></div>
.pageBreak { page-break-before: always;}
Leave a Reply