• Home
  • PHP
  • MySQL
  • Laravel
  • Demos
  • HTML
  • jQuery
  • Framework
  • Request Tutorial
PHP Lift
  • Home
  • Demos
  • Advertisement
PHP Lift
  • Home
  • PHP
  • MySQL
  • Laravel
  • Demos
  • HTML
  • jQuery
  • Framework
  • Request Tutorial
  • Follow
    • Facebook
    • Twitter
    • Google+
    • Pinterest
    • Youtube
    • Instagram
    • RSS
How to Create PDFs from HTML with PHP and Dompdf
Home
HTML

How to Create PDFs from HTML with PHP and Dompdf

May 4th, 2025 Huzoor Bux HTML, PHP 0 comments

Facebook Twitter Google+ LinkedIn Pinterest

Adobe Systems created PDF to illustrate text and images within a fixed-layout document. The PDF file format is used for downloading data or content from the web application. The PDF file format is ideal for downloading HTML or text content. Converting HTML to PDF is required in order to download web page content as PDF files. This tutorial will demonstrate how to convert HTML to pdf and create PDF files using PHP.

Dompdf is a PHP library that allows you to easily convert HTML to PDF documents. The Dompdf library makes it easy to create PDFs from HTML pages in PHP. This example code will show you how to embed PDF generation functionality into your web application. It also makes it easy to convert HTML to PDF using Dompdf.

DEMO
DOWNLOAD CODE

To start with, let us install Dompdf. The easiest way to add Dompdf to your project is to use Composer. To see how to install Composer, do follow https://getcomposer.org/download/. Once you have it on your machine, you can navigate to the directory where your web application is located (usually using the cd command) in your Command line/Terminal and type

composer require dompdf/dompdf

This will install Dompdf and all you have to do is include it in your desired files and use it. Composer is a dependency manager similar to npm for Node.js but for PHP and it is a good idea to use it. If you do not wish to use Composer, you can directly download the Dompdf library from https://github.com/dompdf/dompdf/releases and unzip it in your desired folder.

Read Also How to Convert HTML to PDF in PHP with fpdf

Once you have Dompdf, let us create a simple HTML page to show to the user as a PDF. Below is a PHP file that serves as the HTML page of the PDF. It is a dummy page. The PHP script just displays some HTML and fills it with possibly dynamically-generated if you want to add some.

page.php

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Document</title>

</head>

<body>

     YOUR HTML CONTENT

</body>

</html>

Index.php

<?php

require 'vendor/autoload.php';

// reference the Dompdf namespace

use Dompdf\Dompdf;



// instantiate and use the dompdf class

$dompdf = new Dompdf();

ob_start();



include("page.php")



$html = ob_get_contents();

ob_get_clean();

$dompdf->loadHtml($html);

$dompdf->setPaper('A4', 'portrait');



// Render the HTML as PDF

$dompdf->render();

// Output the generated PDF to Browser

$dompdf->stream();

?>

After we have loaded Dompdf, all we have to do is pass it the HTML page and display the PDF in the user’s browser. We initialize the Dompdf class, then we pass it the HTML page (the page.php) and stream the PDF to the browser.

Now, whenever we access index.php, we will generate pdf using HTML to PDF library Dompdf

If we want to save the PDF to the server for reference – we need only 1-2 more lines of code. We get a string with the contents of the PDF file using $dompdf->output(), save it in a variable or directly put it into a file with an arbitrary name.

Adding one line is sufficient to save the PDF on our server:

file_put_contents("pdfs/file-" . mt_rand(0,99999) . ".pdf",  $dompdf->output());
DEMO
DOWNLOAD CODE

Although, you would want a strict naming convention and not naming your invoices based on randomness. Unless you are okay with PDFs being overwritten from time to time when chance brings the same numbers.

I guess that’s it. You can now generate cool PDFs without much effort, hopefully.

Conclusion

In this guide we’ve attempted to show an easy way for you to convert HTML into PDF using Dompdf with PHP. Our code example provides the most popular method of configuration to create PDF using PHP. You can easily expand the capabilities of Dompdf by configuring options to meet your requirements. To download all the required documents, such as the Dompdf library Download your source code.

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook

Related

  • Tags
  • Dompdf
  • How to
  • HTML to PDF
  • PHP Tutorial
Facebook Twitter Google+ LinkedIn Pinterest
Next article Laravel Data and Value Objects: Harnessing the Power of Immutability and Consistency
Previous article Useful JavaScript globals

Huzoor Bux

I am a PHP Developer

Related Posts

HTML based swipe Tabs for mobile / touch devices CSS
June 16th, 2025

HTML based swipe Tabs for mobile / touch devices

PHP Beyond 2023: Unfurling the Road Less Traveled PHP
June 15th, 2025

PHP Beyond 2023: Unfurling the Road Less Traveled

Create pure CSS based toggle visibility button CSS
June 15th, 2025

Create pure CSS based toggle visibility button

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe
Get new posts by email:
Powered by follow.it
Advertisement
Like us
Recent Posts
  • HTML based swipe Tabs for mobile / touch devices
  • Cool HTM5 Features, Part 2
  • Programming Languages for Better Job Opportunities
  • PHP Beyond 2023: Unfurling the Road Less Traveled
  • Create pure CSS based toggle visibility button
Categories
  • API
  • Bootstrap
  • Bot
  • CSS
  • CSS 3
  • Database
  • Designing
  • Framework
  • Guide
  • HTML
  • HTML 5
  • JavaScript
  • jQuery
  • Laravel
  • MySQL
  • Node.js
  • oAuth
  • Payment
  • PHP
  • Python
  • Social
  • Tips
  • Web 3.0
  • WordPress
Weekly Tags
  • PHP
  • How to
  • javascript
  • laravel
  • MYSQL
  • PHP framework
  • css
  • jQuery
  • HTML to PDF
  • Web Development
  • About
  • Privacy Policy
  • Back to top
© PHPLift.net. All rights reserved.