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!

ⓘ I have included a zip file with all the example code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes HTML To PDF Useful Bits & Links
Tutorial Video The End

 

 

DOWNLOAD & NOTES

First, here is the download link to the source code as promised.

 

QUICK NOTES

  • The zip file does not contain a copy of MPDF. Please download the latest version from their official website.
If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

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 is 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, just navigate to your project folder in the command line (or terminal), 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 do 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.

 

 

USEFUL 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!

Leave a Comment

Your email address will not be published.