Thursday, 21 January 2010 08:46

Creating a PDF document from HTML using asp.net and php

Written by Adrian Fitchet

I have always struggled to find a simple and easy solution to generate PDF documents in ASP.net. pdf_image

There certainly are solutions out there but at a cost. There are also the open source developments of iTextSharp. However, the PDF generation is not simple and comes with a learning curve. I was in search of a solution that would take HTML I had already generated to a produce a PDF document.

I found a simple and easy solution, dompdf. However, it used the server technology php. Now there have always been debates between myself and my php developers as to which is the better server side technology, asp.net or php. When it comes to simple and inexpensive PDF generation I must give it to the php world, they have the better solution. dompdf is a style-driven renderer; thus it takes in the html and CSS stylesheets and passes the PDF rendering to another library called PDFlib. So what can I do since I want to program in asp.net? Combine the two? That is exactly what I did…

I had already generated the HTML code. Now all I wanted to do is generate an easy and simple PDF document. dompdf was the perfect solution. It was easy to use (no learning curves) and generates a PDF document from simple HTML.

This is what I did (download solution here):


Setup my asp.net server to accept php scripts. This is relatively easy. There is plenty of information about this online. Many asp.net hosting companies allow for both technologies to run at the same time.

Generated HTML in my asp.net code and wrote it to a text file. Then called the php script / file to read my text file and generate a PDF document.

Asp.net code

// create my html document to generate into PDF

string html = "";

html += " ;

 

html += "";

// create the file name

string filename = string.Format(this.Server.MapPath("temp//data{0}.txt", System.Guid.NewGuid().ToString());

// create a writer and open the file

TextWriter tw = new StreamWriter(filename, false, System.Text.Encoding.ASCII);

// write a line of text to the file

tw.WriteLine(data);

// close the stream

tw.Close();

// redirect to the php file that will generate the PDF

Response.Redirect("pdf.php?pdfID=" + filename);

 
Php code:

 require_once("dompdf-0.5.1/dompdf_config.inc.php");

 

if(isset($_GET['pdfID']))

$pdfID = $_GET['pdfID'];

$filename=$pdfID;

$output="";

$file = fopen($filename, "r");

while(!feof($file)) {

 

//read file line by line into variable

$output = $output . fgets($file, 4096);

 

}

fclose ($file);

//unlink($filename);

 

$html = $output;


$dompdf = new DOMPDF();

$dompdf->load_html($html);

$dompdf->render();

$dompdf->stream("pdf.pdf");


Issues one may face:

The php scripting has to have various functions allowed to run domPDF. If you have php error logging on you can quickly identify which functions and settings need to be enabled in the php.ini file.

Last modified on Thursday, 21 January 2010 11:01
Adrian Fitchet

Adrian Fitchet

E-mail: This e-mail address is being protected from spambots. You need JavaScript enabled to view it

Add comment



Call Us: +27 11 612 7460