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.
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.
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();
The following example shows how to use Dompdf to convert HTML and generate PDF with minimal configuration.
// 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();
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
).
$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 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) –
Convert HTML to PDF using JavaScript
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
Hi codex world. I’m able to create pdf at runtime & even saving it to server.
I don’t want to open it, just saving the data as a pdf on server.
The problem is, i can’t set default padding & margin. How to that?
Please reply as soon as possible.
Thanks in advance.
I want to download pdf on button click not on load.
Is that possible?
how to use php file at a time of view the pdf
i want to use php file instead of html file
how to enable css float in this new version of dom pdf?
how to use bootstrap library with it?
IS there any way to disable download and print option in PDF view
Hello Support Team,
I want to convert html content with QR code to pdf .Please helf me. Thanks