How to generate pdf file with php

by  
Share:
How to generate pdf file with php

Portable Document Format (PDF) is a file format used to represent documents in a manner independent of application software, hardware, and operating system. Almost everyone has a pdf reader software in their PC or mobiles so sharing documents becomes very easy with pdf files. In this tutorial I will walk you through how to generate pdf file with php.

View Demo

Where do I use this script?

Take a situation where you have created a school/college/employee (any other) Management System and there comes a point where you have to generate reports in pdf formats. Or most commonly you have an ecommerce site and you need to generate invoices in pdf format. The situation may be different but the requirement are the same. So this script can prove very handy at that time.

 

Let’s generate a simple pdf file

The library that I will be using is mpdf which generates pdf files from HTML. You can visit the mpdf official site here for more info.

Follow these three steps to generate a quick pdf file.

  • Download the latest zip file from the mpdf official site( click here ).
  • Extract the zip file to your server and rename it to mpdf (you can give any other name)
  • Attach the class file to your file and write down the script given below.

<?php
$html = '';
$html .= '<h1>This is my first pdf file</h1>';
$html .= '<p>This is the paragraph</p>';

//include your mpdf library here
include("./mpdf/mpdf.php");

// create an object of the class mpdf
$mpdf=new mPDF("c"); 

// write the html to the file
?>
$mpdf->WriteHTML($html);

// generate the output
$mpdf->Output();
exit;
?>

Features provide by mpdf library 5.7

  • Accept UTF-8 encoded.
  • Right-to-left languages are supported.
  • Bookmarks and Meta tag information are supported in all character sets.
  • A single stylesheet can be used for all pages.
  • Table cell padding and borders are supported.
  • Text-indent for 1st line of paragraph.
  • List indenting can be defined.
  • Custom tags added – PAGEBREAK, COLUMNBREAK, INDEXENTRY.
  • Table of contents can be generated automatically.
  • Watermark can be done.
  • Intelligent word-wrapping.
  • And many more

Commonly used methods of mpdf:

Add stylesheet to the document.

<?php
include("./mpdf/mpdf.php");
$mpdf=new mPDF(); 

// Read the css file
$stylesheet = file_get_contents('pdf_style.css');
// The parameter 1 tells that this is css/style only and no body/html/text
$mpdf->WriteHTML($stylesheet,1);	

$mpdf->WriteHTML($html);
$mpdf->Output();
exit;
?>

Select your paper size
<?php
// Define a Landscape page size/format by name
$mpdf=new mPDF('utf-8', 'A4-L');

// Define a A5 page
$mpdf=new mPDF('utf-8', 'A5');

// Define a custom size page (size in mm)
$mpdf=new mPDF('utf-8', array(250,250));

?>

Adjust page margins
<?php
// sizes are defines in millimetres
$margin_left = 20;
$margin_right = 20;
$margin_top = 10;
$margin_bottom = 10;
$header = 5;
$footer = 5;
$mpdf=new mPDF('','', 0, '', $margin_left, $margin_right, $margin_top, $margin_bottom, $header, $footer);

?>

Setting the page orientation, landscape or portrait.
<?php
// sizes are defines in millimetres
$margin_left = 20;
$margin_right = 20;
$margin_top = 10;
$margin_bottom = 10;
$header = 5;
$footer = 5;
$orientation = "L"; // can be P (Portrait) or L (Landscape)
$mpdf=new mPDF('','', 0, '', $margin_left, $margin_right, $margin_top, $margin_bottom, $header, $footer, $orientation);

?>

Summing up the above we have 11 parameters than can be passed in the constructor.
<?php
// sizes are defines in millimetres
$mode = "utf-8";
$format =" A10";
$default_font_size  = "12"; // size in pt
$default_font = "Courier";
$margin_left = 20;
$margin_right = 20;
$margin_top = 10;
$margin_bottom = 10;
$header = 5;
$footer = 5;
$orientation = "L"; // can be P (Portrait) or L (Landscape)
$mpdf=new mPDF($mode,$format, $default_font_size, $default_font, $margin_left, $margin_right, $margin_top, $margin_bottom, $header, $footer, $orientation);
?>

Adding Watermarks to the page
<?php
$mpdf=new mPDF(); 
$mpdf->SetWatermarkText('thesoftwareguy');
$mpdf->showWatermarkText = true;

// add a watermark image 
// first parameter is the source path of image
// second is the transparency value
// third is the size of the watermark (can be array)
$mpdf->SetWatermarkImage('tiger.wmf', 0.15, 'F');
?>

Set the Header and Footer text
<?php
// set the header content left center and right alignment
// values are separated by a pipe sign
$mpdf->SetHeader('{DATE j-m-Y}|{PAGENO}/{nb}|My document');

// different layout of odd and even pages
$mpdf->SetHeader(array(
	'L' => array(
		'content' => 'Text to go on the left',
		'font-family' => 'sans-serif',
		'font-style' => 'B',	/* blank, B, I, or BI */
		'font-size' => '10',	/* in pts */
	),
	'C' => array(
		'content' => '- {PAGENO} -',
		'font-family' => 'serif',
		'font-style' => 'BI',
		'font-size' => '18',	/* gives default */
	),
	'R' => array(
		'content' => 'Printed @ {DATE j-m-Y H:m}',
		'font-family' => 'monospace',
		'font-style' => '',
		'font-size' => '10',
	),
	'line' => 1,		/* 1 to include line below header/above footer */
), 'E'	/* defines header for Even Pages */
);

// Similarly you can set the footer by using SetFooter() method
$mpdf->SetFooter('{PAGENO}');

?>

To know more about mpdf class and methods get it from the manual here.

View Demo
Download from official site

Get the latest updates directly in your inbox for FREE: Subscribe here

Share your feedback / let me know your doubts regarding this tutorial in the comment box given below.

Share:
Hasan

Shahrukh Khan (Hasan)

A software engineer who's a die-hard coder, blogger, dreamer and mentor with years of expertise in web development. Know more...

Related Posts

Mohammad Javeed on

shahrukh bhai ap ny ye site bna ker bht acha kam kia hy ….is sy bht students ko help mily gy

Reply
Peter on

I too agree with Mohammad Javeed, its really awesome

Reply
Eleanor on

You are doing a very good job..I’m totally inspired

Reply
Shahrukh Khan on

Thanks a lot.

Reply
Lily on

how to upload mp3 file on the backend.. can it be updated in the same way as pdf?

Reply
vivekraj on

great, thanks for sharing.

Reply
Shahrukh Khan on

thanks vivek.

Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.