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.
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.
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.
The control question - what are cookies? Choose the correct answer.
Cookies are not sweets, but text files
We want to have an overview of how it goes on our website. But you have the power to affect how much we know about your visit.
As application and web developers, we are very interested in analytical data, so we will be grateful for your absolute consent.
Select your preferred cookie permissions, the basic ones are necessary for operation, others we can use only with your consent.
Your personal data will be processed and information from your device (cookies, personality identifiers and other data collected) may be stored.