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:

$dompdf = new DOMPDF();
$filename =“pdffilename.pdf“;
$pdfoutput = $dompdf->output();
$filenamePath = ‚./path/to/the/savefolder/‘.$filename;
$fp = fopen($filenamePath, „a“);
fwrite($fp, $pdfoutput);

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();

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

Leave a Comment

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