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

Swiftmend

Nurture strength of spirit to shield you in sudden misfortune.

2005-09-13

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.

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

BuildFDFString
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.

SendPDFDocumentToClient
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
SendPDFDocumentToClient(sFDF);
}


/*
* 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[
<2b443507f97e8d5b407ddd36b3912858>
<5187fc178a9fc04a93e4ab16b53b798b>]/Fields\n[\n");

// Insert ShortDateTime
sbFDF.Append("<</T(dateField)/V(");
sbFDF.Append(dtDate.ToShortDateString());
sbFDF.Append(")>>\n");

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;" +
"filename=filename.fdf");

// Write the pdf document to the Response Stream
Page.Response.Write(strPDFDocument);

// Send it to the client
Page.Response.Flush();

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

2 comments:

Anonymous said...

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

Anonymous 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.