• Home
  • PHP
  • MySQL
  • Demos
  • HTML
  • CSS
  • jQuery
  • Framework
  • Social
  • Request Tutorial
PHP Lift
  • Home
  • Demos
  • Advertisement
PHP Lift
  • Home
  • PHP
  • MySQL
  • Demos
  • HTML
  • CSS
  • jQuery
  • Framework
  • Social
  • 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

June 29th, 2022 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 Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)

Related

  • Tags
  • Dompdf
  • How to
  • HTML to PDF
  • PHP Tutorial
Facebook Twitter Google+ LinkedIn Pinterest
Next article How to Extract Text from PDF using PHP
Previous article How to create a screen recorder in JavaScript

Huzoor Bux

I am a PHP Developer

Related Posts

Is PHP dead in 2021? Is PHP still relevant or worth the effort? PHP
July 10th, 2022

Is PHP dead in 2021? Is PHP still relevant or worth the effort?

8 interesting functions of Laravel Eloquent (ORM) PHP
July 9th, 2022

8 interesting functions of Laravel Eloquent (ORM)

How to Extract Text from PDF using PHP PHP
July 3rd, 2022

How to Extract Text from PDF using PHP

Leave a Reply Cancel reply

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

    Advertisement
    Like us
    Recent Posts
    • Best 10 Programming Languages that will rule in 2022
    • Top 7 Websites To Get Your First Paid Internship
    • How to Create Bar Chart Race in JavaScript
    • 9 Tricks and Tips to Search Google Like a Pro
    • Is PHP dead in 2021? Is PHP still relevant or worth the effort?
    Categories
    • API
    • Bootstrap
    • Bot
    • CSS
    • CSS 3
    • Database
    • Designing
    • Framework
    • Guide
    • HTML
    • HTML 5
    • JavaScript
    • jQuery
    • MySQL
    • Node.js
    • oAuth
    • Payment
    • PHP
    • Python
    • Social
    • Tips
    • WordPress
    Weekly Tags
    • PHP
    • How to
    • javascript
    • api
    • MYSQL
    • laravel
    • jQuery
    • HTML to PDF
    • PHP Basics
    • Programming Habits
    • About
    • Privacy Policy
    • Back to top
    © PHPLift 2021. All rights reserved.