mPDF: HTML to PDF introduction

mPDF is a simple and popular tool for shared hosting users to create and convert UTF-8 encoded HTML pages to PDF files. In other words mPDF is a PHP classes based on FPDF and HTML2PDF tools which purpose is the same – to create and manage PDF documents using simple PHP. It’s worth mentioning that on some basis mPDF is slower and can produce bigger PDF files than its predecessors, however it has many capabilities and simpler to use for daily basis.

By using mPDF you have to create an HTML file structure which will be converted to PDF format file. You can also use some CSS coding and improve your files. You can find all the supported CSS attributes in this classes documentation, but I can say that you can securely use most popular and common CSS attributes for your elements. However, there are some restrictions using HTML tables and styling text inside cells.

With mPDF you will be able to create rich PDFs with headers and footers, CSS styling, included pictures, password protection, watermarks, pages numbering, justification and more….

mPDF Installation and setup

You can download latest mPDF version from the official website. After you have downloaded the necessary files just extract zipped file content to your defined folder and rename it to mpdf for better usage in the future. Lowercased folder name prevents you from foolish mistakes including the class and etc. It’s a good practice to keep this kind of tools in special folder called library, modules or plugins.

In addition, you should allow three catalogs to be written. Folders list:

  • /ttfontdata/
  • /tmp/
  • /graph_cache/

You now successfully installed mPDF to your website.

Initializing mPDF and a first try

You now can produce all kinds of different PDF’s generated from HTML. The easiest way to ensure that you done everything right is to initialize mPDF class after including it to your PHP file and generating a simple line in PDF.

Here’s a code snippet that will help you:


require_once('library/mpdf.php');

$mpdf = new mPDF();

$mpdf->WriteHTML('<p>Your first taste of creating PDF from HTML</p>');

$mpdf->Output();
exit;

You should get a downloadable PDF file with your specified line in it. If you do not get it, ensure you are seeing PHP errors and try to eliminate them or make sure you installed it right.

More complex usage

I think you have already understood that WriteHTML method enables you to write HTML code to the PDF file and output it. You should consider looking all available and supported HTML tags in documentation. However, you should not have any problems using common HTML tags, for example, creating divs, tables, headings, all kinds of lists, text and etc.

It’s a good practice to put CSS and HTML codes in separate files and you can keep up with this rule when using mPDF too. So imagine that you have all your file styling in file “stylesheet.css” and want to include everything to your PDF. By using the same function WriteHTML and adding second parameter you can easily do this. Second parameter tells how to parse the contents. For example, if you specified 1, it would only parse content as CSS styles. If you specified 2, the content would only be parsed as HTML elements. Default value is zero which orders to parse the file as HTML document including both elements and styles.

We are trying to parse only CSS contents from file and add  some div to the file. View the example here:


$mpdf = new mPDF();
$stylesheet = file_get_contents('stylesheet.css');
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML('<div id="mydiv"><p>HTML content goes here...</p></div>', 2);
$mpdf->Output();
exit;

If you want to be more specific about encoding and page size, you can put those parameters when initializing mPDF class like this:


$mpdf = new mPDF('utf-8', 'A4');

You can even specify actual page size in mm. In the example below the page size will be 190mm wide x 236mm height:


$mpdf = new mPDF('utf-8', array(190,236));

If you want full functionality, you can even specify margins and if the page is landscape like this:


$mpdf = new mPDF('',    // mode - default ''
 '',    // format - A4, for example, default ''
 0,     // font size - default 0
 '',    // default font family
 15,    // margin_left
 15,    // margin right
 16,     // margin top
 16,    // margin bottom
 9,     // margin header
 9,     // margin footer
 'L');  // L - landscape, P - portrait

In later articles and tutorials we will see how to set headers and make complex structure of our PDF file.

, , , , , , ,

About Richard Smaizys

I am a freelance PHP developer working mostly on projects using PrestaShop for ecommerce (modules, theme integration, projects from 0 etc.) and WordPress as CMS solution.

47 Responses to “mPDF: HTML to PDF introduction”