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:
- Generate XML data that needs to be included in the document
- Load an XSL Transformation document and transform the XML data to a new XML document, containing XSL-FO to format the pages to output.
- Convert the XSL-FO document to the PDF document.
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
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: