Easy Creating Or Convert To PDF File Programmatically Using PHP mPDF

We can create PDF file using PHP easily. I choose mPDF in order to do this. Why mPDF ? That because the method is to use html string for writing PDF content and then produce PDF as an output.
We can format PDF content like an HTML page i.e using bold <b/>, table <table/>, etc which is easy for Web developer off course.

PHP.Net has own recommended PDF creation class which are Haru PDF and PDFLib. They are great but many lines and methods to do simple ‘Hello World’ example. I feel easier using mPDF since it uses HTML string.

This blog post will demonstrate how to install and gives some basic examples so reader can have brief ideas how to create PDF programmatically.

Download & Install MPDF

Download PHP source code of MPDF from http://www.mpdf1.com/mpdf/index.php?page=Download
Copy to a folder in your web server directory. I am using MPDF57 for this demonstration.

Basic ‘Hello World’ Example

$mpdf=new mPDF();
$mpdf->WriteHTML('<p>Hallo World</p>');

Very simple and only few lines to create PDF file. WriteHTML() function converts HTML string as PDF content.
The pdf file will be shown inline with browser.

HTML ‘Table’ Example

$html = '<b>Table Example</b><br>';
$html = $html.'<table border=1>';
$html = $html.'<tr><th>Header 1</th><th>Header 2</th></tr>';
$html = $html.'<tr><td>Data</td><td>Data</td></tr>';
$html = $html.'</table>';
$mpdf=new mPDF();

Add New Page And Force File Download

$mpdf=new mPDF();
$mpdf->WriteHTML('<p>Hallo World 1</p>');
$mpdf->WriteHTML('<p>Hallo World 2</p>');

Use $mpdf->AddPage() to add new PDF page and $mdpf->Output('yourfilename.pdf','D'); to have force download to a file ‘yourfilename.pdf’.

Using CSS

$mpdf=new mPDF();
$stylesheet = '.cls_example { background-color:#ff7f50; font-family:Calibri; font-size:15pt; }';
$html = '<div class='cls_example'>Hallo World</div>';

Use this $mpdf->WriteHTML(...,1); when you want to set a CSS stylesheet.
$mpdf->WriteHTML(...,2); to write HTML code without the information.

Capture Web Url Content To PDF

$url = 'http://www.google.com';
if (ini_get('allow_url_fopen')) {
	$html = file_get_contents($url);

	$mpdf=new mPDF(''); 

file_get_contents($url) used to gather html content of Url.
With good HTML & CSS design, your Website content can become nice pdf brochure.

Search & Replace string in PDF using OverWrite()

$mpdf=new mPDF('');

$mpdf->percentSubset = 0;    
$search = array(
$replacement = array(
        'PT Angin Ribut',
        'Angkasa Raya'

$mpdf->OverWrite('ToOverWrite.pdf', $search, $replacement, 'I', 'OverWrited.pdf' ) ;

OverWrite() used to over write string inside PDF. With this feature you can make your own template and change the content dynamically for automatic mailing.
Note: OverWrite() has only been tested to work on PDF files produced by mPDF.

For more documentation please refer to http://mpdf1.com/manual/index.php

Best regards,
Agung Gugiaji

One response to “Easy Creating Or Convert To PDF File Programmatically Using PHP mPDF

  1. Nemaram Punavat

    pdf library for .net

    Create high quality PDFs with SelectPdf Html To Pdf Converter from the best PDF library. HTML to PDF API also available

    to get more – https://selectpdf.com/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s