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 downloading the bunch of data or text content in the web application. PDF file format is the perfect choice to download the text or HTML content in a file. At the time of download web page content as PDF file, it requires converting HTML to PDF. In this tutorial, we will show how to convert HTML to PDF and generate PDF file using PHP.

Dompdf is a PHP library that provides a simple way to convert HTML to PDF file. Using Dompdf library you can easily generate PDF from HTML page in PHP. The example script 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 & Setup

Download stable release of dompdf from GitHub. Extract the downloaded dompdf package and rename it with dompdf. Alternatively, you can download our source code to get all the required files together.

Instantiate Dompdf Class

To using dompdf class, you need to include the autoloader in PHP. Use the following PHP code to instantiate 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

The following example shows how to use dompdf to convert to generate PDF with minimal configuration. Specify the HTML content in loadHtml() function to generate PDF and render on browser.

// 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

The following example code generates PDF from an HTML file (pdf-content.html). Also, it provides preview or download option to control the PDF output.

// 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

Some useful methods of Dompdf class are given below that are used to integrate HTML to 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

Configuration Options

Dompdf has various options to configure the PDF generation. You can see all the available options from here – Dompdf\Options

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 requirement. To get all required files including dompdf library, download the source code.

Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request

Recommended Tutorials For You

4 Comments

  1. Koushik Basu Said...
  2. Darshan Kini Said...
  3. Shelu Nagori Said...
  4. Md Hoq Said...

Leave a reply