Convert HTML to PDF in PHP with Dompdf

PDF is a file format created by Adobe Systems for illustrating text and images in a fixed-layout document. PDF is used to download a bunch of data or text content in the web application. The PDF file format is the perfect choice to download text or HTML content in a file. At the time of downloading web page content as a PDF file, it requires converting HTML to PDF. In this tutorial, we will show you how to convert HTML to PDF and generate PDF files using PHP.

Dompdf is a PHP library that provides a simple way to convert HTML to PDF documents. Using the Dompdf library you can easily generate PDF from the HTML page in PHP. The example code will help you to implement PDF generation functionality in the web application and make it simple to convert HTML to PDF in PHP with Dompdf.

Dompdf Installation and Setup

Before getting started, download stable release of the Dompdf library and include it in the project directory.

Note that: You don’t need to download the Dompdf library separately, all the required files are included in our source code package.

Instantiate Dompdf Class

To use the Dompdf class, you need to include the autoloader in the PHP script. Use the following PHP code to instantiate and use the dompdf class.

// Include autoloader 
require_once 'dompdf/autoload.inc.php';

// Reference the Dompdf namespace
use Dompdf\Dompdf;

// Instantiate and use the dompdf class
$dompdf = new Dompdf();

Basic Usage (Convert HTML to PDF)

The following example shows how to use Dompdf to convert HTML and generate PDF with minimal configuration.

  • Specify the HTML content in loadHtml() method of Dompdf class.
  • Render HTML as PDF using render() method.
  • Output the generated PDF to the Browser using stream() method.
// Load HTML content 
$dompdf->loadHtml('<h1>Welcome to CodexWorld.com</h1>');

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4''landscape');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to Browser
$dompdf->stream();

Advanced Usage

With the Dompdf library, you can easily enhance the functionality of PDF creation. The following code generates PDF from an HTML file (pdf-content.html).

  • Get content from HTML file using the file_get_contents() function in PHP.
  • Load HTML content using loadHtml() method of Dompdf class.
  • Control the PDF output using the stream() function of Dompdf class.
    • $filename – (string) Name of the PDF file.
    • $options – (array) Header options.
      • Attachment – 1=download and 0=preview
// Load content from html file 
$html file_get_contents("pdf-content.html");
$dompdf->loadHtml($html);

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4''landscape');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF (1 = download and 0 = preview)
$dompdf->stream("codexworld", array("Attachment" => 0));

Dompdf Useful Methods

Dompdf library provides various methods and options to configure PDF creation and customize PDF documents. Some of the useful methods of Dompdf class are given below that are commonly used to integrate HTML into PDF functionality.

loadHtml(): Loads HTML content.

  • $str (string) – Required. Specify HTML to load.
  • $encoding (string) – Optional. Specify encoding.

loadHtmlFile(): Loads content from an HTML file.

  • $file (string) – Required. Specify filename or url to load.

output(): Returns the PDF as a string.

  • $options (array) – Optional. Specify whether content stream compression will enable. (compress => 1 or 0)

render(): Renders the HTML to PDF.

setBasePath(): Sets the base path to include external stylesheets and images.

  • $basePath (string) – The base path to be used when loading the external resources URLs.

setPaper(): Sets the paper size & orientation.

  • $size (string|array) – 'letter', 'legal', 'A4', etc.
  • $orientation (string) – 'portrait' or 'landscape'.

stream(): Streams the PDF to the client.

  • $filename (string) – Specify name of the file to be streamed (without .pdf extension).
  • $options (array) –
    • 'compress' => 1 or 0 – enable content stream compression.
    • 'Attachment' => 1=download or 0=preview

Convert HTML to PDF using JavaScript

Conclusion

In this tutorial, we have tried to provide an easy way to convert HTML to PDF with Dompdf using PHP. Our example code shows the most used configuration option to generate PDF in PHP. You can easily extend the functionality using Dompdf configuration options as per your needs. To get all required files including the Dompdf library, download the source code.

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

7 Comments

  1. Debabrata Roy Said...
  2. Arpit Shah Said...
  3. Parth Said...
  4. Koushik Basu Said...
  5. Darshan Kini Said...
  6. Shelu Nagori Said...
  7. Md Hoq Said...

Leave a reply

keyboard_double_arrow_up