Dynamic PDF via QueryString

You need Adobe Reader to view this PDF.

Return to my portfolio

What the Hell is This?

Updating PDFs Dynamically

PDF

I was recently asked to create a downloadable membership card. The card needed to be a print-ready PDF with the data populated from the user's login information. After exploring several server-side options for generating PDFs, I stumbled across a simple solution that allows me to update form fields in a PDF via QueryString. This involves using Acrobat's built-in javascript interface. The user can then print or save the card with the dynamic values intact.

Try it out

Enter some values below to update the PDF on the left.

Name:
Title:
Phone:
Email:
 

Get the Javascript

How to Make this Thing Work

Copy and paste the javascript below into your PDF Document. In Acrobat go to Advanced > Document Processing > Document JavaScripts > type in 'docOpen' > Add > paste the script.

Note: you need to make sure that your field names match the querystring names. Also, to allow users with Adobe Reader to save the dynamic values, go to Advanced > Enable Usage Rights in Adobe Reader.

			
function docOpen(){
//only run the script if the PDF file is being viewed in a browser window
if (this.external)
{
    //The whiteList defines which fields are permitted to be changed by the URL. 
    //If you want all fields to be changed, leave the array empty as in "[]"  
    whiteList = ["theName", "theTitle", "thePhone", "theEmail"]
	
    //get the parameters portion of the URL and unescape it so we get the spaces and punctuation back 
	grabParams = this.URL.substring(this.URL.indexOf("?")+1)
	//this replaces the stupid +s from the URLEncoding
	parametersString = grabParams.replace(/\+/g," ")
	//only run the script if there are parameters
	if (parametersString.length > 0)
	{
	  //create an array of key/value pairs
	  parameters = parametersString.split("&")
	  //loop through the array...
	  for each (parameter in parameters)
	  {   
		//create a 2 element array for each parameter as [key, value] 
		kvPair = parameter.split("=")
		//set the field named "key" to "value"
		fieldName = unescape(kvPair[0])
        if (whiteList.length > 0)
        {
		    if (whiteList.indexOf(fieldName) > -1)
        			{
        			this.getField(fieldName).value = unescape(kvPair[1]) 
        			}
        }
        else
        {
            this.getField(fieldName).value = unescape(kvPair[1])
        }
    	  }
	}
}
}
// call the docOpen() function
docOpen();