Generate PDF from PHP
A very important topic for many web applications is to create PDF files from PHP and to download it to a client. Very often, this is used to generate invoices, to export certain information or to let the user generate special reports. Many PHP frameworks use their own PDF tools and there are also different PHP modules out there. A very easy and good option is to generate PDF files directly from HTML Code. This can be done with HTML2PDF and this is a free software which is developed under LGPL license.
HTML2PDF
HTML2PDF computes PDF files directly from HTML Code. The generated PDF file is stored on your server and can be downloaded by a client. This is very simple, you only need the free to use HTML2PDF library which can be downloaded here.
This library can be used in your PHP file after including:
require_once("../library/Html2pdf/html2pdf.class.php");
In a PHP script you can easily convert static HTML Code to PDF. All you have to do is to call these lines of code:
$content = '<html><body><h1>Einfaches Beispiel</h1></body></html>'; //HTML to PDF conversion $html2pdf = new HTML2PDF($mode,'A4','de',true,'UTF-8',array(10, 10, 10, 10)); $html2pdf->WriteHTML($content); $html2pdf->Output($filename.'.pdf');
First we define some HTML Code. Basically you only need to use the same HTML Code that you use for displaying your site. This HTML Code can also use CSS Code which also works for your PDF file. Then we create a new HTML2PDF class with some params. We can define different things:
- mode (L for landscape and P for portrait format)
- page size (for example A4)
- encoding (very important for HTML code!)
- border (you can define blank space at the border of the page – for printers)
Then we set the HTML code for the class and the last line creates a PDF file with the given filename. The directory should have the right write permissions!
That’s it! A good solution is to create special CSS stylesheets for printing HTML code, I personally create a area with the website logo and some header and footer information.
Advantages
The main advantage is, that you do not need to learn different PDF tools of different frameworks. Also you do not need to write complicated source code for PDF elements. With HTML it is far easier, because you can test your sites directly in your browser. For a web developer, this is much simpler. The only difficult thing may be to write a CSS stylesheet for all PDF outputs, but a web designer can also do it for you if CSS is a problem. This CSS can be used for multiple projects.
your code does not working properly .. its return error „[ERROR] It seems that HTML2PDF dependencies are not installed… you must install thems with `composer install`“
no need composer install,
just place up to your framework vendor folder, and done.
Can it working for page break
Hi,
How to add watermark image in pdf file using html2pdf class.
The same way you will create a watermark in html. You can set a watermark image as background of your site.