In this tutorial, we will learn how to convert HTML and CSS styled pages to PDF using PHP. We will use third-party libraries to achieve this because PHP does not have in-built core functionality for this.
You just need a little bit of knowledge of HTML and PHP to convert the HTML page into PDF with CSS styling. I’ll explain to you each and every step. So let’s get started.
As you probably know or hear from somewhere, there are many pdf converters third-party PHP libraries. That will allows you to convert your HTML page into PDF with styling. You can also make any modifications in files for better conversion in PDF.
The following are the best libraries to convert HTML to PDF using the PHP script code.
So these are the best PHP libraries for transferring the HTML content with design into PDF files. You can choose any of them.
Let’s have a look at how to convert HTML pages to PDF using these PHP libraries one by one.
Convert HTML to PDF Using TCPDF
The TCPDF PHP library will generate a PDF document with a writing couple of lines of code. It is easy to use this library. Just clone the GitHub repository or you can also download directly from GitHub.
Then you can import this library on your PHP file and use the library function. This library allows you modification while converting HTML pages into PDF. It has many collections of examples and ready-to-test snippets on its official homepage.
- No external libraries are required for the basic functions.
- All standard page formats, custom page formats, custom margins and units of measure.
- UTF-8 Unicode and Right-To-Left languages.
- TrueTypeUnicode, OpenTypeUnicode v1, TrueType, OpenType v1, Type1 and CID-0 fonts.
- Font subsetting.
- Images, graphic (geometric figures) and transformation methods.
- Supports JPEG, PNG and SVG images natively, all images supported by GD (GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM) and all images supported via ImagMagick (http://www.imagemagick.org/script/formats.php)
- 1D and 2D barcodes: CODE 39, ANSI MH10.8M-1983, USD-3, 3 of 9, CODE 93, USS-93, Standard 2 of 5, Interleaved 2 of 5, CODE 128 A/B/C, 2 and 5 Digits UPC-Based Extension, EAN 8, EAN 13, UPC-A, UPC-E, MSI, POSTNET, PLANET, RMS4CC (Royal Mail 4-state Customer Code), CBC (Customer Bar Code), KIX (Klant index – Customer index), Intelligent Mail Barcode, Onecode, USPS-B-3200, CODABAR, CODE 11, PHARMACODE, PHARMACODE TWO-TRACKS, Datamatrix, QR-Code, PDF417.
- JPEG and PNG ICC profiles, Grayscale, RGB, CMYK, Spot Colors and Transparencies.
- Automatic page header and footer management.
- Document encryption up to 256 bit and digital signature certifications.
- Transactions to UNDO commands.
- PDF annotations, including links, text and file attachments.
- Text rendering modes (fill, stroke and clipping).
- Multiple columns mode.
- No-write page regions.
- Bookmarks, named destinations and table of content.
- Text hyphenation.
- Text stretching and spacing (tracking).
- Automatic page break, line break and text alignments including justification.
- Automatic page numbering and page groups.
- Move and delete pages.
- Page compression (requires php-zlib extension).
- XOBject Templates.
- Layers and object visibility.
- PDF/A-1b support.
Follow the below steps to understand how to convert HTML to PDF in PHP using the TCPDF library.
Step 1: Download the TCPDF library from this link https://github.com/tecnickcom/tcpdf
Step 2: After download, you will get the zip folder, so unzip it in the folder where your project is hosted on.
Step 3: Now create a PHP file (index.php) or any other name it is up to you. Then add the following code in your that file.
Step 4: Make sure your TCPDF folder path is correct in first-line
As you see I made some changes while converting HTML to PDF using the TCPDF library. And you can also make more customization as per your requirements. You can check more customization examples here.
Convert HTML to PDF Using Dompdf
Dompdf is also one of the best PHP library to convert any HTML page to a PDF file in a simple and easy way. This library also allows modification while converting our HTML page to PDF document using the PHP code.
I’ll show you how you can implement it and convert your HTML with CSS styling into a PDF document.
- Handles most CSS 2.1 and a few CSS3 properties, including @import, @media & @page rules
- Supports most presentational HTML 4.0 attributes
- Supports external stylesheets, either local or through http/ftp (via fopen-wrappers)
- Supports complex tables, including row & column spans, separate & collapsed border models, individual cell styling
- Image support (gif, png (8, 24 and 32 bit with alpha channel), bmp & jpeg)
- No dependencies on external PDF libraries, thanks to the R&OS PDF class
- Inline PHP support
- Basic SVG support
Follow the below steps to understand how to convert HTML to PDF in PHP using the Dompdf library.
Step 1: Download the Dompdf library from GitHub repository by this link https://github.com/dompdf/dompdf.
Step 2: After downloading, extract the zip folder and place it where your project is located.
Step 3: Now create a new PHP file (index.php) and include the autoloader script for the Dompdf library to instantiate Dompdf class.
Step 4: Now use the following basic code for PDF conversion.
Some advance features to use this library more extensively.
As I said above, this library also allows you to modification and you can do it. It has more advanced features to convert HTML to PDF.
In the above example, we are converting inline HTML code. So let’s see how to convert HTML files to PDF. For this, we will use
file_get_contents() PHP function to fetch the all HTML content. And then pass this content to
loadHTML() library’s function.
There are more useful methods that you can use to convert HTML files to PDF and those methods you can check here.
Convert HTML to PDF Using mPDF
mPDF is also a PDF generation PHP library. It will convert HTML (UTF-8 encoded) to PDF with CSS style. It is based on FPDF and HTML2FPDF with a number of enhancements. It is a bit slower than the original script (HTML2PDF) but it can generate PDF from large HTML file with CSS support.
- Accepts UTF-8 encoded HTML
- Supports almost all languages including
RTL (arabic and hebrew), and
CJK – (chinese-japanese-korean)
- CSS stylesheets
- Word spacing and character spacing for justification
- Nested block-level elements (e.g. P, DIV) including margins, borders, padding, line-height, background colours etc.
- Support (partial) for floating and fixed-position block-elements
- Page layout and orientation
- Text-justification and hyphenation
- Page numbering
- Odd and even paging with mirrored margins
- Page headers & footers
- Tables – nested tables, rotated, or autosized to fit on a page
- Table of contents
- Watermarks (with customizable text angle)
- Images in JPG, GIF, PNG, SVG, BMP or WMF format
- Password protection
- Barcodes (EAN13, UPC-A/E, Code 11, 39, 93, 128, Codabar, MSI, IMB, Planet, Postnet, RM4SCC etc.)
- Import another PDF file and use as a template
- Embedded font subsets
- PDF/A-1b support (ISO 19005-1:2005)
- PDF/X-1a support
- PDF/A-3 support (along with ZUGFeRD invoices compatibility)
Follow the below steps to understand how to convert HTML to PDF in PHP using the mPDF library.
Step 1: Download the mPDF library from this link https://github.com/spipu/html2pdf
Step 2: Extract the folder and place it in your project folder.
Step 3: Create a new .php file (index.php) and import the mPDF autoloader into the file. And then create a new object of the mPDF class.
Step 4: Now it is ready to use and you can convert your HTML into a PDF file.
If you found this was helpful please share it and if you have any questions please ask me in the comment section, I’ll respond to you as soon as possible.