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

include('MPDF57/mpdf.php');
$mpdf=new mPDF();
$mpdf->WriteHTML('<p>Hallo World</p>');
$mpdf->Output();
exit;

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

include('MPDF57/mpdf.php');
$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();
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;

Add New Page And Force File Download

include('MPDF57/mpdf.php');
$mpdf=new mPDF();
$mpdf->WriteHTML('<p>Hallo World 1</p>');
$mpdf->AddPage();
$mpdf->WriteHTML('<p>Hallo World 2</p>');
$mpdf->Output('trialbasic.pdf','D');
exit;

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

include('MPDF57/mpdf.php');
$mpdf=new mPDF();
$stylesheet = '.cls_example { background-color:#ff7f50; font-family:Calibri; font-size:15pt; }';
$html = '<div class='cls_example'>Hallo World</div>';
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($html,2);
$mpdf->Output();
exit;

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

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

	$mpdf=new mPDF(''); 
	$mpdf->setBasePath($url);
	$mpdf->WriteHTML($html);
	$mpdf->Output();
}
exit;

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()

include('MPDF57/mpdf.php');
$mpdf=new mPDF('');
$mpdf->SetImportUse(); 

$mpdf->percentSubset = 0;    
$search = array(
        '[[company]]', 
        '[[address]]'
);
$replacement = array(
        'PT Angin Ribut',
        'Angkasa Raya'
);

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

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

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s