Tutorial Article on how to Create PDF from HTML on the Fly using PHP

Creating a PDF through PHP from an HTML content has always been a pain for me. Its true that PHP has an in-built extension to create a php on the fly but, you have to take care of every pixel you want to draw on the PDF file output.

But now we can easily create a PDF from an HTML page through PHP on the fly.

For advanced script to create PDF from HTML on the fly please also read- mPDF article here…

Case Study

Requirement is to create a PDF in a INVOICE format for the sales person and send that invoice through an email to the sales person and the client for whom the invoice is created.

All this has to be done on the fly; on just a click of a button.


I tried many solutions available online but was not able to find a good one before I finally landed up with a free to use code “DOMPDF” which can run on both windows and linux platform.

How This Works- DOMPDF

Its a open source library which can be get from Google Code library-

Click here to get library

(Let me know if you face any problem using this library. Fill in the comments form)

How I Used It

I had a form which sales person was required to fill in.

I submitted the form to php script which includes all the required DOMPDF library.
Then i took all the HTML content in a variable. Passed that variable to DOMPDF to create a pdf.
Saved the PDF to a directory.
Then attached the newsly created PDF to an email and finally sent an email to the concerned people.

Thats It.

Take a look on my Customized Code

(Also all the code with examples is available in the above link “Click here to get library”)

//Increase the default memory limit to create a PDF (in case its a heavy pdf like 200kb+)
$old_limit = ini_set(“memory_limit”, “80M”);
session_start(); //Required internally for my code. This can be avoided


//My HTML variable to be passed to PDF lib
$pdf_html = ‘<html>
font-family:Arial, Helvetica, sans-serif;


HELLO I am a newly created PDF


//Generating the PDF file
$dompdf = new DOMPDF();

$paper = “a3”;
$orientation = “landscape”;

//Generate the name of the output file
//Contact name and current date-time
$file_contact_name = str_replace(” “, “-“, $contact_name);
$outfile_name = $file_contact_name . “-“. $pdf_file_today_date . “.pdf”;

//Path to save the PDF
$outfile_path_name = CLIENT_QUOTE_SAVE_PATH . $outfile_name;

$save_file = true;
$base_path = “”; //required when reading a file to be saved as PDF

if ( isset($base_path) ) {

$dompdf->set_paper($paper, $orientation);


if ( $_dompdf_show_warnings ) {
foreach ($_dompdf_warnings as $msg)
echo $msg . “\n”;

if ( $save_file )
// if ( !is_writable($outfile) )
// throw new DOMPDF_Exception(“‘$outfile’ is not writable.”);
//if ( strtolower(DOMPDF_PDF_BACKEND) == “gd” )
// $outfile = str_replace(“.pdf”, “.png”, $outfile_path_name);

if(file_put_contents($outfile_path_name, $dompdf->output()))
//Send email with attachment
$file = fopen($outfile_path_name,’rb’);
$data = fread($file,filesize($outfile_path_name));

$from = “no-reply@some-domain.com”;
$to = $sales_person_email;

$bcc_email = $email_client;
$bcc_email = “”;

$subject = “Quote for: ” . $company;

$semi_rand = md5(time());
$mime_boundary = “Multipart_Boundary_x”.$semi_rand.”x”;

$fileatt_type = “application/pdf”;

$headers = “From: $from\n” .
“Bcc: $bcc_email\n” .
“Reply-To: $from\n” .
“X-Priority: 3\n” .
“X-MSMail-Priority: Normal\n” .
“X-Mailer: PHP/” . phpversion() .”\n” .
“MIME-Version: 1.0\n” .
“Content-Type: multipart/mixed;boundary=\”{$mime_boundary}\””;

$msg = “Please find personalized Quote attached for ” . $company . “.”;
$msg .= “<br><br>Thanks, <br>www.OsWebStudio.Com”;

$msg = “\n\n” .
“–{$mime_boundary}\n” .
“Content-Type:text/html; charset=UTF-8\n” .
“Content-Transfer-Encoding: 7bit\n\n” .
$msg . “\n\n”;

//Attach the file to email
$data = chunk_split(base64_encode($data));
$msg .= “–{$mime_boundary}\n” .
“Content-Type: \”{$fileatt_type}\”;\n” .
” name=\”{$outfile_name}\”\n” .
“Content-Disposition: attachment;\n” .
“Content-Transfer-Encoding: base64\n\n” .
$data . “\n\n” .


DOMPDF Documentation
(Can also be found in the link above “Click here to get library”)

dompdf is an HTML to PDF converter. At its heart, dompdf is (mostly) CSS2.1 compliant HTML layout and rendering engine written in PHP. It is a style-driven renderer: it will download and read external stylesheets, inline style tags, and the style attributes of individual HTML elements. It also supports most presentational HTML attributes.

PDF rendering is currently provided either by PDFLib (http://www.pdflib.com) or by a bundled version the R&OS CPDF class written by Wayne Munro (http://www.ros.co.nz/pdf). (Some performance related changes have been made to the R&OS class, however). In order to use PDFLib with dompdf, the PDFLib PECL extension is required. Using PDFLib improves performance and reduces the memory requirements of dompdf somewhat, while the R&OS CPDF class, though slightly slower, eliminates any dependencies on external PDF libraries.


  • handles most CSS2.1 properties, including @import, @media & @page rules
  • supports most presentational HTML 4.0 attributes
  • supports external stylesheets, either local or through http/ftp (via fopen-wrappers)
  • supports complex tables, including row & column spans, separate & collapsed border models, individual cell styling, (no nested tables yet however)
  • image support (gif, png & jpeg)
  • no dependencies on external PDF libraries, thanks to the R&OS PDF class
  • inline PHP support. See below for details.


  • PHP 5.0.0+
  • Some fonts. PDFs internally support Helvetica, Times-Roman, Courier & Zapf-Dingbats, but if you wish to use other fonts you will need to install some fonts. dompdf supports the same fonts as the underlying R&OS PDF class: Type 1 (.pfb with the corresponding .afm) and TrueType (.ttf). At the minimum, you should probably have the Microsoft core fonts (now available at: http://corefonts.sourceforge.net/). See the INSTALL file for font installation instructions.

Limitations (Known Issues)

  • tables can not be nested
  • not particularly tolerant to poorly-formed HTML input (using Tidy first may help).
  • large files can take a while to render
  • ordered lists are currently not supported

By: Gaurav Kumar
Technical Lead in Open Source Technologies

Both comments and pings are currently closed.

2 Responses to “Tutorial Article on how to Create PDF from HTML on the Fly using PHP”

  1. Thanks for one more nice article. I want to thank you for the efforts you have made in writing this article UncleCode.Com – Tutorial Article on how to Create PDF from HTML on the Fly using PHP. I am hoping for more from you sooner or later as well.

  2. I read something related to this post over at google news… I was intrigued and started looking around, then somehow landed at this page… at any rate, I think that I mostly agree with what you talk about here. But I’m going to go see what else I can look up too.