2 Steps To Convert HTML To PDF In PHP (Simple Example)

Welcome to a quick tutorial and example on how to convert HTML to PDF in PHP. Need to generate a PDF report or document from HTML? Unfortunately, PHP does not support the PDF file format natively.

In order to convert HTML to PDF in PHP, we have to download and use a third-party library. MPDF is a good choice, and the easiest way is to get it via Composercomposer require mpdf/mpdf. Thereafter, a short code snippet to create PDF file from HTML:

  • require "/vendor/autoload.php";
  • $mpdf = new \Mpdf\Mpdf();
  • $mpdf->WriteHTML("<p>HTML</p>");
  • $mpdf->Output("FILE.PDF");

That covers the basics, but let’s walk through a detailed example in this guide – Read on!

 

 

TLDR – QUICK SLIDES

Fullscreen Mode – Click Here

 

TABLE OF CONTENTS

 

CONVERT HTML TO PDF WITH PHP

All right, let us now get into the example of how to convert HTML to PDF in PHP.

 

STEP 1) DOWNLOAD MDPF

  • The easiest way to get MDPF is through a package manager called Composer. Quite a hassle to download and install, but a one-time effort nonetheless.
  • Once when you have Composer installed, navigate to your project folder in the command line cd HTTP/FOLDER/.
  • Run composer require mpdf/mpdf.
  • Composer will automatically download the latest version into the vendor/ folder.

Take note, MPDF is not perfect. It is not able to replicate all HTML elements and CSS styles perfectly, but it should work well enough for your basic needs. Please check out the MPDF official documentation (it’s massive) on the supported HTML and CSS.

 

 

STEP 2) HTML TO PDF CONVERSION

2-html-pdf.php
<?php
// (A) LOAD MPDF
require "vendor/autoload.php";
$mpdf = new \Mpdf\Mpdf();
// PORTRAIT BY DEFAULT, WE CAN ALSO SET LANDSCAPE
// $mpdf = new \Mpdf\Mpdf(["orientation" => "L"]);
 
// (B) OPTIONAL META DATA + PASSWORD PROTECTION
$mpdf->SetTitle("Document Title");
$mpdf->SetAuthor("Jon Doe");
$mpdf->SetCreator("Code Boxx");
$mpdf->SetSubject("Demo");
$mpdf->SetKeywords("Demo", "Testing");
// $mpdf->SetProtection([], "user", "password");
 
// (C) THE HTML
$html = "<html>
<head>
  <style>
    #test{ background:#ff0000; }
    #atable{ border:1px solid #00ff00; }
  </style>
</head>
<body>
  <h1>Title</h1>
  <p id='test'>Hello world!</p>
  <table id='atable'>
    <tr><td>A Table</td></tr>
  </table>
</body>
</html>";
 
// OR WE CAN JUST READ FROM A FILE
// $html = file_get_contents("PAGE.HTML");
 
// (D) WRITE HTML TO PDF
$mpdf->WriteHTML($html);
 
// (E) OUTPUT
// (E1) DIRECTLY SHOW IN BROWSER
$mpdf->Output();

// (E2) FORCE DOWNLOAD
// $mpdf->Output("demo.pdf", "D");

// (E3) SAVE TO FILE ON SERVER
// $mpdf->Output("demo.pdf");

This conversion script should be pretty straightforward. Not going to explain line-by-line, but a quick summary:

  1. Include the MPDF library, create an MPDF object.
  2. Optional. Set the PDF metadata and password-protect it if required.
  3. Write some HTML code or read from an HTML file.
  4. Finally, just use the MPDF object to convert the HTML to PDF.
  5. Directly show the PDF file in the browser, force a download, or save it on the server.

 

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

SUPPORT

600+ free tutorials & projects on Code Boxx and still growing. I insist on not turning Code Boxx into a "paid scripts and courses" business, so every little bit of support helps.

Buy Me A Meal Code Boxx eBooks

 

EXAMPLE CODE DOWNLOAD

Click here for the source code on GitHub gist, just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

EXTRA BITS & LINKS

That’s all for the very simple script, and here are a few small extras that may be useful to you.

 

ALTERNATIVE – PRINT TO PDF

If you don’t want to write any code, here is a possible alternative “manual” solution. That is to access the webpage that you want to convert, then use the browser’s print function, and select “save as PDF”.

 

RELATED LINKS

 

 

TUTORIAL VIDEO

 

INFOGRAPHIC CHEAT SHEET

How To Convert HTML To PDF In PHP (click to enlarge)

 

THE END

Thank you for reading, and we have come to the end of this very short guide. I hope it has solved your PDF woes, and if you have stuff you want to add to this guide, please feel free to comment below. Good luck and happy coding!

2 thoughts on “2 Steps To Convert HTML To PDF In PHP (Simple Example)”

  1. From the ‘Comments’, it is obvious your code works. But I downloaded your package and installed as per directions – it did not work. Dreamweaver CS6 is telling me “There is a syntax error on line 14 ” [3-ajax-search.html] . This the code portion.
    I was hoping to gain an understanding of how this process (php “LIKE”) operates. BUT not know enough to determine if it is something I have done wrong or if in fact the code is defective, I am stuck.
    Thanks
    Warren

    1. There is nothing wrong with the code. Don’t use Dreamweaver – It is outdated and does not support PHP 8 properly; Adobe is pretty much pulling out of web development.

Leave a Comment

Your email address will not be published. Required fields are marked *