Generating PDF files

About two weeks ago I was working on a prototype for Twice IT to generate PDF files on the fly. These documents contain order confirmation info that Twice’s customers need to sign. These should not be alterable, hence a choice for the PDF format, which can be encrypted and secured. During a Christmas drinking session I already suggested a path to take. It looked like this:

  1. Generate XML data that needs to be included in the document
  2. Load an XSL Transformation document and transform the XML data to a new XML document, containing XSL-FO to format the pages to output.
  3. Convert the XSL-FO document to the PDF document.
The first two steps are pretty trivial. Apart from the fact that I’ve never done much with XSL-FO and that my working knowledge on it is minimalistic, doing a transformation of one XML document to another is a no-brainer. What I did not know is how many ways there are to perform step 3. When Googling around the Internet, I found just a few free, open source initiatives and commercial products to do this in a .NET environment. Here are a couple of hyperlinks that might come in useful:

There are some projects on SourceForge that also do things with PDF, but these either generate PDF using a Document Object Model, such as in the Report.NET project, or use the FOP implementation (which is written in Java). Somebody was smart enough to get the HUGE codebase compiled into a .NET assembly. The project NPDF has this assembly and can be found at SourceForge here.

Playing around with APOC for an hour or so, I was able to generate the PDF files using the aforementioned strategy. Step 1 was even easier than I thought, because the data was stored in an SQL Server 2000 database. As you might have guessed, I quickly installed SQLXML 3.0.

Something worth noting: when generating files that are streamed back to the server, these additions to the HTTP response header will be necessary:

HttpContext.Current.Response.ContentType = “application/pdf”; // Or any other MIME type
HttpContext.Current.Response.AddHeader(“content-disposition”,
    “filename=somefilename.pdf”);

These lines will make the PDF file download and open into the browser window. If you want a Open/Save dialog when you stream the result back, change the content-disposition as follows:

HttpContext.Current.Response.AddHeader(“content-disposition”,
    “attachment;filename=somefilename.pdf”);

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

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