--Niniel EU-Kilrogg --Niniel EU-Kilrogg


Nurture strength of spirit to shield you in sudden misfortune.


Generate a PDF document in the browser with dynamic content

How about generating a complete PDF-document filled out with dynamic content and have it open up in the browser as an attachment with the push of a button?

First, read these two great articles:
Dynamically generate a MS Word document using HTML & CSS

Printing using FDF forms

Now you should know how to send a document from a server to a web browser
as an attachment and you know how to extract the FDF syntax from a PDF file and what you can do with it.

Put these two techniques together and have a look at my example below.

I have written three functions in C# as an example. I'm using a very simple PDF with only one field where I want to set the the current date. This might as well be multiple values from a DataGrid or XML file.

This function calls the other two helper functions and could be called from the click event of a button.

This function creates an FDF string with a StringBuilder and appends the variables into the FDF syntax where they should be. This is where you would substitute my fdf syntax and the path to the PDF template as this differs per PDF document.

Using MIME settings this function forces the supplied string to be downloaded to the client as a PDF/FDF attachment. Here you also specify which CharSet you would like.

Here is the code:

* Name : GeneratePDFReport
* Created : 2005-06-16
* Created by : Johan Andersson
/// <summary>
/// Generate a PDF report with data to user
/// </summary>
/// <param name="dtDate">Date for report</param>
public void GeneratePDFReport(DateTime dtDate) {

// Generate FDF string
string sFDF = BuildFDFString(dtDate);

// send document as FDF through Response Stream with MIME to user

* Name : BuildFDFString
* Created : 2005-06-16
* Created by : Johan Andersson
/// <summary>
/// Generate an FDF file which in turn results in a PDF report
/// </summary>
/// <param name="dtDate">Date for report</param>
public string BuildFDFString(DateTime dtDate) {

// Generate FDF string
StringBuilder sbFDF = new StringBuilder("");

sbFDF.Append("%FDF-1.2%âãÏÓ" + "\n" +
"1 0 obj<</FDF<</F(PathAndFileName.pdf)/ID[

// Insert ShortDateTime

sbFDF.Append("]>>>>endobj trailer<
</Root 1 0 R>>%%EOF\n");

return sbFDF.ToString();

* Name : SendPDFDocumentToClient
* Created : 2005-06-16
* Created by : Johan Andersson
/// <summary>
/// Using MIME settings, force the supplied string to be downloaded
/// to the client as a PDF .fdf file attachment.
/// </summary>
private void SendPDFDocumentToClient(string strPDFDocument) {
Page.Response.Charset = "ISO-8859-1";
Page.Response.ContentType = "application/pdf";
Page.Response.AddHeader("Content-disposition", "attachment;" +

// Write the pdf document to the Response Stream

// Send it to the client

// Stop the Response stream from sending the rest of the Page on the postback
Page.Response.SuppressContent = true;


Kimmo said...

This is so cool I might just've wet my pants!

Jill said...

Good ideas very helpful. I was able to fill out a form online yesterday (http://goo.gl/VSj4n6) you might want to try. It was easy and it works for me.