Web Software Architecture and Engineering – Life on the Bleeding Edge

Ok, so in my previous post on PDF forms with CF8, I tried really hard to figure why I couldn’t simply create a form in Acrobat, fill it out using CF, and make what I filled out like the other static text, so that the user can’t change the form.
Turns out CF8 CANNOT do this natively. Its a simple use case. In our case, we are creating nice certificates for our users when they complete and pass an exam.
To make things worse, the CF8 docs make it seem like its doable. With words like “flatten” make you think everything’s going to be all right. But it aint. Flattening obliterates all the fields, removing them all together, while the documentation says : removes interactivity. Hell yeah, its no longer interactive!
But there is hope. With a little iText (dont be scared, its simple) and Javaloader magic, I was able to get it to work.
Let’s run through the code, shall we?
Step 1:
Download Mark Mandel’s JavaLoader from RiaForge and iText (latest version).
Step 2:
Follow the code below. And that is it!

// First we load the latest iText JAR using JavaLoader. Adjust paths as necessary.
paths = arrayNew(1);

// Latest iText JAR
paths[1] = expandPath("iText-2.1.3.jar");

// Use Mark JL
loader = createObject("component", "javaloader.JavaLoader").init( paths );

// I've already named the blank cert form and the new populated cert previous. Adjust as you see fit.
newFile=expandPath(local.newPDFFile);
pdfFile=expandPath(local.PDFFile);

// Then some iText Magic
fos=createObject("java","java.io.FileOutputStream").init(newFile);
reader = loader.create("com.lowagie.text.pdf.PdfReader").init(pdfFile);
pdfStamper = loader.create("com.lowagie.text.pdf.PdfStamper").init(reader, fos);

// add some data to the form
pdfForm=pdfStamper.getAcroFields();

// We have 3 fields, first_last, orgCity_orgState, and date
// Here we are using the form scope, adjust as necessary.
pdfForm.setField("First_Last", trim(form.First_Last));
pdfForm.setField("OrgCity_OrgState", trim(form.OrgCity_OrgState));
pdfForm.setField("Date", trim(form.Date));

// Lets do a REAL flatten.
pdfStamper.setFormFlattening(true);

// Close out!
pdfStamper.close();
reader.close();

Step 3:
Last, serve up the file!

<cfcontent type="application/pdf" reset="true" file="#ExpandPath(local.newPDFFile)#" >
Advertisements

Comments on: "Filling PDF Forms with CF8 to Create a Cert – Gotchas & Tips" (5)

  1. Do you think this would work from a PDF generated from the CF Report Builder instead of using Acrobat?

  2. Michael,

    Try reading and dumping the PDF. If you see form fields, then I’m sure you can definitely fill them out.

    Sami

  3. Can you explain why Flattening doesn’t work for you?

  4. Henry,

    See my previous post for details.

  5. […] Filling PDF Forms with CF8 to Create a Cert – Gotchas & Tips « The Byte Stops Here […]

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 )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: