PDF creation by PHP

I tried a lot to create PDF files by using PHP frameworks. For Java I’m using iText, but there is no interface to use iText by a PHP server. So I googled a lot and there are several frameworks to create these files. I tried them all an my opinion is, that domPDF is the best one, becase it is the easiest of all to use.

To create a PDF by domPDF, I only have to create a normal HTML page. This page I have to give to the renderer of domPDF and this renderer creates a PDF file out of this HTML code which is a one to one copy of the HTML page.

To use the domPDF framework, there are only a few lines of code necessary:

require_once(„dompdf_config.inc.php“);
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$filename =“pdffilename.pdf“;
$pdfoutput = $dompdf->output();
$filenamePath = ‚./path/to/the/savefolder/‘.$filename;
$fp = fopen($filenamePath, „a“);
fwrite($fp, $pdfoutput);
fclose($fp);

This is all. More is not needed. The variable $html is the String which includes the HTML page. It is absolutely necessary, that the HTML code is valid. otherwise the PDF is not rendered correctly.

If you do not want to store the file on server and only stream it to the browser it is already more easier than the code above:

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream(„sample.pdf“);

So have much fun to work with this framework and if you want please comment this post if I was able to help you a little bit. Or maybe click a like button below and post it to your social platform.

Print Friendly, PDF & Email

One thought on “PDF creation by PHP

Schreibe einen Kommentar zu hp drivers Antworten abbrechen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*
*