How to create PDF document in PHPMay 14, 2016
PDF gains its popularity for more than 20 years, mainly thanks to the same look and formatting on different systems and devices. In web development, we are using it mainly but not only for static presentations, as mail attachment or generating invoices.
The last mentioned application is dealt in most web applications, especially in e-shops where invoices with a certain appearance and content needs are generated from user data. Technically speaking, there is a need to transform the HTML code into the PDF document. This transformation is provided by PHP libraries. Many PHP libraries are available on the market and this article will introduce a selected library that is freely downloadable from the internet. We'll show you how to install it and how to use it.
We are talking about the mPDF library, which is one of the more familiar and widely used. It is based on older FPDF and HTML2FPDF libraries with a number of enhancements.
As mentioned above, the main function of this PHP library is to convert HTML code along with CSS styles into a PDF document. Compared to HTML2FPDF is mPDF slower and creates larger files. On the other hand it has more support for CSS styles and many useful features, which is preferable for us. With mPDF, you can create rich PDF documents with headers and footers, including photos, password protection, watermarks, page numbering, and more. Another important argument for prioritizing this library over others is that it is up to date and supports the latest version of PHP.
mPDF installation and setup
Recommended way of installing is via composer.
composer require mpdf/mpdf
After this step, you have successfully installed mPDF in your web project.
mPDF initialization and first example
require_once __DIR__ . '/vendor/autoload.php'; $mpdf = new \Mpdf\Mpdf(); $mpdf->WriteHTML('<h1>Hello world!</h1>'); $mpdf->Output();
In the example you can see three more commands after initializing the library. The first creates a new library object that is always created before working with the library. The second command requires HTML code which represents the content of our PDF document. The third command offers the user a downloadable PDF or opens it directly in the browser.
MPDF documentation describes the available HTML tags for conversion which means there should be no problem with common tags such as div, tables, headers, various lists, or text. Programmers like when HTML code and CSS design are in different files. This principle can be fully used in mPDF.
Suppose we have all the styles in "stylesheet.css" and want to include them in the PDF. We use the same WriteHTML () function as in the first example, but we add another parameter to it. This second parameter will tell you what type the content of the first parameter will be. For example, if we enter 1, it means that the first parameter is the CSS script. By number two we define the insertion of HTML code only. For 0, which is also the default, we say that we insert an HTML document containing CSS scripts in addition to HTML code.
In the example we will show the addition of the CSS PDF file and then its simple HTML code.
$mpdf = new \Mpdf\Mpdf(); $stylesheet = file_get_contents('stylesheet.css'); $mpdf->WriteHTML($stylesheet, 1); $mpdf->WriteHTML('<h1>Hello world!</h1><p>How are you?</p>', 2); $mpdf->Output();
If you need to define more specific parameters like text encoding or page format, we add them to the library constructor, i.e. command$mpdf = new \Mpdf\Mpdf() , for example:
$mpdf = new \Mpdf\Mpdf([ 'mode' => 'utf-8', 'format' => 'A5', 'orientation' => 'L' ]);
Examples of some basic config parameters and their values can be found at https://mpdf.github.io/reference/mpdf-functions/construct.html
We work with mPDF when we develop websites and e-shops and we recommend using it. But as there are more libraries like this, it is up to you to choose and use it. Other known libraries with this feature include WkHtmlToPdf, HTML2PDF, TCPDF or dompdf.