This class can be used to convert an HTML document or Web page using the ABCPDF or EasySW Web services. It can communicate with these services to tell to convert a given URL to PDF. It may also convert a local HTML file by serving it as a remote page passing it as an argument of the current script. 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.
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. 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 ( 'Welcome to CodexWorld.com' ); // (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 – Conclusion In this tutorial, we have tried to provide an easy way to convert HTML to PDF with Dompdf using PHP.