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

Open Source PHP Library for Processing PDF Files

Read, Write, Laod and Convert HTML and CSS Text into PDF Files using Open Source PHP Library.

What is PDFMerger Dompdf?

Creating and manipulating PDF documents dynamically is a typical web development necessity. Whether users need to create invoices, reports, or other printable papers, having a reliable PDF creation tool is critical. One such tool that stands out is Dompdf, a sophisticated PHP framework that makes it easy to convert HTML and CSS text into PDF files. The library allows developers to convert their web-based content into visually stunning, print-ready PDFs, making it an excellent choice for a variety of applications such as invoicing, reporting, and document generating. There are several important features part of the library, such as use images and vector graphics inside PDFs, accommodate various languages and character sets in PDFs, define custom page sizes and orientations, robust Unicode and font support, HTML and CSS standards support and many more.

Dompdf is an open-source PHP module that enables software developers to transform HTML information to PDF files without requiring any other dependencies. It is based on the PHP DOM extension and the TCPDF library, resulting in a powerful solution for creating PDF documents from HTML content. The library's major purpose is to provide a simple yet effective approach to generate PDFs that closely resemble the original HTML text. Its comprehensive feature set, combined with ease of use and versatility, make it a good choice for a variety of applications. Explore the docs, play around with the library, and discover Dompdf's full potential for your PHP projects.

.

Previous Next

Getting Started with Dompdf

To run Dompdf first you need to install PHP version 7.1 or higher and DOM extension. The easiest to install the Dompdf is via Composer. Please use the following command for a smooth installation.

Install Dompdf via Git command

composer require dompdf/dompdf  

It is also possible to install it manually; download the latest release files directly from GitHub repository.

Install Dompdf via composer

git clone https://github.com/dompdf/dompdf.git
cd dompdf/lib

PDF Files Creation from UTF-8 Encoded HTML

Dompdf library provides functionality for creating PDF documents from UTF-8 encoded HTML inside PHP application. UTF-8 encoded HTML is accepted as the standard input. Once the PDF file is created, you can also make changes to it according to your own needs. The library facilitates you to insert new pages, add new content, insert images, use bookmarks, add headers & footers to an existing page, and much more.

Convert HTML to PDF via PHP API

The open source Dompdf library has incorporated complete functionality for loading and converting HTML and Web contents to a PDF file inside PHP applications. It supports a significant portion of CSS2.1, allowing users to style your PDFs using familiar CSS rules. The library provides a straightforward API that allows you to load HTML content, set configuration options, and render PDFs with just a few lines of code. It provides support for setting page size, select fonts of your choice, apply formatting and so on. The following example shows how easily software developers can load an existing HTML content and convert it into PDF documents using PHP code.

How to Load HTML Content and Convert it to PDF via PHP?

 use Dompdf\Dompdf;
use Dompdf\Options;

// Include the autoloader
require 'vendor/autoload.php';

// Initialize Dompdf
$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$options->set('isPhpEnabled', true);
$dompdf = new Dompdf($options;
// Load HTML content
$html = '

Hello, Dompdf!

'; $dompdf->loadHtml($html); // Set paper size $dompdf->setPaper('A4', 'portrait'); // Render PDF (output to browser or save to file) $dompdf->render(); $dompdf->stream('output.pdf', array('Attachment' => 0));

Unicode and Font Support

Dompdf is a versatile and reliable solution for PHP developers who need to generate PDFs from HTML and CSS content. Handling internationalization is a breeze with the library, thanks to its robust Unicode and font support. This ensures that your PDFs can accommodate various languages and character sets, making it a versatile solution for global applications.

Image and Vector Graphics Support

Including images and vector graphics in your PDFs is seamless with Dompdf. It supports various image formats, making it easy to include images in your PDF documents inside PHP applications. Whether it is charts, logos, or other visual elements, software developers can easily incorporate them into their PDF documents for a more polished and professional look. You can add headers and footers to your PDF pages for a professional touch with just a couple of lines of PHP code.