1. Products
  2.   HTML
  3.   PHP
  4.   Dompdf

Free PHP Library for Creating PDF Files from HTML

Open Source PHP API for Creating PDF Documents from HTML Content. It supports HTML and CSS rendering into PDF via PHP code.

In today's digital age, the ability to generate PDF documents dynamically has become a crucial requirement for many web applications. Whether it's generating invoices, reports, or printable content, having a reliable and efficient library for PDF generation is essential. One such library that has gained popularity among software professionals and programmers is Dompdf. It has built-in support for Unicode, enabling the generation of PDFs in various languages and character sets. It can handle complex scripts like Arabic, Chinese, and Cyrillic, ensuring accurate rendering of multilingual content.

Dompdf is a very easy to handle and it simplifies the process of generating PDF documents from HTML content. The library has provided many advanced features to customize and enhance your PDF generation process. There are several important features part of the library, such as handling most CSS 2.1 and a few CSS3 properties, adding images into PDF, presentational HTML 4.0 attributes support, adding headers and footers to PDF files, controlling page breaks using CSS, inserting table of contents, using bookmarks in PDFs, inline PHP support and many more.

Dompdf is a PHP library that allows software developers to generate PDF documents from HTML content. It leverages the power of the PHP DOM extension and CSS2.1 selectors to accurately render HTML and CSS into PDF format. It is an open source project and is actively maintained on GitHub, making it a reliable choice for PDF generation in PHP applications. It offers various configuration options to fine-tune the PDF generation process, such as settings like page size, margins, header and footer content, compression, and more, according to their specific requirements. Its robust feature set, including support for HTML and CSS rendering, dynamic content generation, multilingual support, and customization options, makes it a popular choice among software developers and engineers.

Previous Next

Getting Started with Dompdf

The recommended and easiest way to install Dompdf is using Composer, the dependency management tool for PHP. Please use the following command a smooth installation.

Install Dompdf via Composer

composer require dompdf/dompdf 

You can also install it manually; download the latest release files directly from GitHub repository.

Render HTML and CSS via PHP API

The open source Dompdf library makes it easy for software developers to load and render HTML and CSS files without any external dependencies. It supports a wide range of HTML and CSS features, making it capable of rendering complex layouts and styling. It supports most CSS2.1 selectors, including advanced features like CSS media queries, inline-block, table rendering, and more. It also supports complex tables, including row & column spans, separate & collapsed border models, individual cell styling and so on. The following example shows how to load HTML content and convert it into PDF file and save it at the place of your choice.

How to Load HTML Content & Convert It into PDF via PHP Code?

require_once 'vendor/autoload.php';

use Dompdf\Dompdf;

$dompdf = new Dompdf();

// Load the HTML content you wish to convert to a PDF

$html = '

Hello, dompdf!

'; $dompdf->loadHtml($html); // Customize the PDF generation settings: $dompdf->setPaper('A4', 'portrait'); // Render the PDF $dompdf->render(); // Save the PDF to a file $dompdf->stream('document.pdf'); // Output the PDF directly to the browser $dompdf->stream();

Add Headers and Footers into PDF via PHP

Headers and footers are very important part of the PDF documents, allowing software developers to insert text, images, and other PDF files into one or more pages in a PDF document. A header and footer are usually utilized to declare the document's ownership. The open source Dompdf library makes it easy for software developers to insert Headers and Footers inside their PHP applications with just a couple of lines of code. Developers can add headers and footers to their PDF documents using the setHtmlHeader() and setHtmlFooter() methods. These sections can contain HTML content, and you can use CSS styling to customize their appearance.

PDF Customization, TOC & Bookmarks Support

The Dompdf library provides several advanced features allows software developers to customize and enhance their PDF generation process. The library has provided supports for including images and fonts into their PDF, use absolute URLs or provide the local file path. Make sure the images are accessible to the server. For custom fonts, dompdf supports embedding TrueType (.ttf) and OpenType (.otf) font files. It is also very easy to create a table of contents or add bookmarks to your PDF documents.