Tuesday, October 13, 2009

Update on PDF Form Issues in ColdFusion

I posted previously on an odd problem I've run into with PDF forms populated by CFPDFFORM on ColdFusion 8 and the resultant PDF not being searchable via Ctrl-F in a PDF reader. Initially I thought it was a font encoding problem since I found some promising evidence to support that notion, so after spending literally an entire day changing hundreds of font encoding settings in PDF form fields individually in Acrobat (because you can't select them all and change them globally ... nice feature to add to Acrobat 10 maybe? YA THINK?), I thought I had the problem licked. I was able to open the PDF form in Acrobat, fill in the form field, save the PDF, and have the text I typed into the form field come up in searches.

Wrong. Turns out the only reason it was searchable was that I manually typed into the form field. When ColdFusion 8 populates the form fields with CFPDFFORM the form field still isn't searchable. Same problem with ColdFusion 9.

I submitted the issue to Adobe Support Center of Excellence, and a week and a half later they're saying they can reproduce the issue and it's getting escalated.

Since I have no clue when I'll get a response from Adobe and I'm putting my money on a "can't/won't fix" response (based on previous experience), this afternoon I grabbed the latest version of iText which is an open source Java PDF library. Among the numerous open source libraries bundled into ColdFusion, iText is one that's potentially used to power CFPDFFORM. (I'm not saying this is what's used since I can't know that for sure; I'm just saying it's a likely candidate if they didn't roll something themselves.)

Anyway, with iText in hand I wrote a simple little Java application to populate said PDF form. This way I could narrow down further if the problem is with iText (if that is in fact what ColdFusion uses for CFPDFFORM) or if it's something ColdFusion is doing that's causing the issue.

The result? iText by itself works perfectly. So at least now I have it narrowed down to ColdFusion being the culprit. I even replaced the iText instance in ColdFusion with the latest version to make sure that wasn't the issue, and either the version doesn't matter or iText is not in fact what ColdFusion is using to populate PDF forms.

You might be thinking, "But isn't CFPDFFORM so much simpler than Java?" Short answer is absolutely not . Longer answer is absolutely not and holy crap is using iText directly fast as hell.

Here's some simple CFML code that populates a PDF form:


<cfpdfform action="populate" source="mypdfform.pdf" destination="filledpdfform.pdf">
    <cfpdfformparam name="myPDFField" value="Foo" />
    <cfpdfformparam name="myOtherPDFField" value="Bar" />
    ... etc. ...
</cfpdfform>

<!--- now if you want the data in the pdf form to actually be saved to disk,
        you have to do this little dance to flatten it --->
<cfpdf action="read" source="filledpdfform.pdf" name="myPDFData" />
<cfpdf action="write" source="myPDFData" destination="filledpdfform.pdf" flatten="true" overwrite="true" />

And here's that same thing in Java--all I'm leaving out is some import statements that would be at the top of the class in which this code resides:


PdfReader reader = new PdfReader("mypdfform.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("filledpdfform.pdf"));
AcroFields form = stamper.getAcroFields();

form.setField("myPDFField", "Foo");
form.setField("myOtherPDFField", "Bar");
... etc. ...

stamper.close();

As you can see, the Java code is about the same as the CFML code. You open the PDF form, you set the field values, and you write the file out, although with iText I don't have to write/read/write again to get it flattened, which is one of my earlier "can't/won't fix" responses from Adobe.

What's my next step in all of this? Well, I'm faced with a pretty easy decision really. This is for an application the sole purpose of which is to process mountains of XML and generate flattened PDF forms containing the data extracted from XML. iText lets people actually search the PDFs as they should be able to (which is important since some of these PDFs are hundreds of pages long), iText is incredibly fast at cranking out PDFs in my limited testing thus far (which will be important when I'm re-generating three years of PDFs since none of the existing ones are searchable), and the Java code to achieve a better end result isn't any more complex than the CFML code.

It's an easy decision to use iText for this portion of the application. But as you can glean from the tone of this post I'm annoyed. CF 8 was new when this app was first built and one of the reasons we bought another CF 8 Enterprise license (since this app runs on its own server) was for this very feature because I figured it would be easier than using iText. It might have saved me some time initially, but boy am I paying dearly for it now.

So I'll start by reworking the PDF generation portion of the app to use iText, and then I'm going to start implementing CFPDFFORM in Open BlueDragon. Frankly given that there's literally no user interface for this application CF wasn't a great choice to begin with, but at the point when I was making the decision CFPDFFORM is what sold me on using CF instead of writing the app in Java. But three years later, here I am. My other option I'm pondering is rewriting the entire application in Java or Groovy since that's probably better suited to the no-UI aspects of the app anyway.

Nothing like having an itch to scratch to get motivated I guess, so at least the problems I'm having may lead to a nice new (and fully working!) feature in Open BlueDragon at some point, and maybe I'll dig into Groovy more to rewrite my existing app. Interesting times ahead.

6 comments:

Anonymous said...

Good example of the K.I.S.S. principle at work. Somewhere along the way every continuing, evolving "project" has this happen. It happens in computer programs, home improvement, and government. The "designers" inevitably add so much to their "project" that it ceases to function in the way it was originally designed or achieve the orignal intended purpose. When this happens, and the "designers" are too invested in the "project" to allow it to "get back to basics" then often times the only solution is to chuck the whole thing and start something new.I apologize if I am waxing too metaphorical.Glad you were able to make your own solution, Matt. I hope it means better days ahead for you with this project.

Matthew Woodward said...

Update on the update--it's officially a bug, and Adobe's workaround is to ... wait for it ... use iText. Maybe it'll work properly when CF 10 comes out.

YouthPastor said...

CF10 is available in beta and still apparently using iText 2.1.0 from 2008. Are you able to install the developer version and see whether this issue persists?

Here's the current ColdFusion bug database:
https://bugbase.adobe.com/

Matt Woodward said...

Since I don't use Adobe CF anymore I don't really have a need to test it. :-) Hopefully they got it resolved.

Reynaldo Rivas said...

Hi. This service here allows you to easily edit your PDF documents.
http://goo.gl/wdh5gV

You can fill out PDF form, save it, fax it, and email it.

Lacy Brown said...

Adobe is one of the most popular graphics/images editing applications, with excellent features, if you have any issues for adope ,our company provide online help and support for adope.
Adobe Support