Friday, April 24, 2009

Embedding PDF form in CFDOCUMENT - java.lang.NullPointerException

I came across an interesting post on the adobe forums, about embedding a pdf form in cfdocument. Never having that exact need before, I was a little surprised to discover it was possible.

But when I tested it, I did encounter one issue. As mentioned in the documentation, you must use at least one cfdocumentsection tag alongside the cfpdfform tag. But apparently it also needs to be placed before the cfpdform tag. When I placed it after cfpdfform tag and tried to write the output to a file, an error occurred

An exception occurred when performing document processing. The cause of this exception was that:

java.lang.NullPointerException at com.adobe.internal.pdftoolkit.services.manipulations.impl.PMMPages.propagateAnnots(Unknown Source)


Save pdf to a file:

<!---
This FAILS
--->
<cfdocument format="pdf" filename="#pathToOutputFile#" overwrite="true">
<cfpdfform action="populate" source="#pathToForm#">
<cfpdfformparam name="firstName" value="Cookie">
<cfpdfformparam name="lastName" value="Monster">
</cfpdfform>
<cfdocumentsection>
<p>Some content</p>
</cfdocumentsection>
</cfdocument>

<!---
This works
--->
<cfdocument format="pdf" filename="#pathToOutputFile#" overwrite="true">
<cfdocumentsection>
<p>Some content</p>
</cfdocumentsection>
<cfpdfform action="populate" source="#pathToForm#">
<cfpdfformparam name="firstName" value="Cookie">
<cfpdfformparam name="lastName" value="Monster">
</cfpdfform>
</cfdocument>

When I attempted to display the pdf in the browser, no output appeared in Firefox. Though when I checked the CF logs they contained a java.lang.NullPointerException error.

Display in browser:

<!---
This FAILS
--->
<cfdocument format="pdf">
<cfpdfform action="populate" source="#pathToForm#">
<cfpdfformparam name="firstName" value="Cookie">
<cfpdfformparam name="lastName" value="Monster">
</cfpdfform>
<cfdocumentsection>
<p>Some content</p>
</cfdocumentsection>
</cfdocument>

<!---
This works
--->
<cfdocument format="pdf">
<cfdocumentsection>
<p>Some content</p>
</cfdocumentsection>
<cfpdfform action="populate" source="#pathToForm#">
<cfpdfformparam name="firstName" value="Cookie">
<cfpdfformparam name="lastName" value="Monster">
</cfpdfform>
</cfdocument>

The one exception was when I saved the output to a variable. That did work without error.

Save output to variable:

<!---
This works
--->
<cfdocument format="pdf" name="pdfOutput">
<cfpdfform action="populate" source="#pathToForm#">
<cfpdfformparam name="firstName" value="Cookie">
<cfpdfformparam name="lastName" value="Monster">
</cfpdfform>
<cfdocumentsection>
<p>Some content</p>
</cfdocumentsection>
</cfdocument>

So I guess the options are either move the cfdocumentsection before the cfpdfform tag, or write the output to a variable.

2 comments:

Ranno August 27, 2009 at 12:52 AM  

Hi, I saw your blog. Its nice. I would like to exchange links with you. If you are interested, then post a comment at http://javacodeonline.blogspot.com

cfSearching October 17, 2009 at 1:02 AM  

Hi @Ranno,

While I do not normally post promotional only links, I took a look at that site and it has a number of good introductory entries on java. Since I often cover java in this blog, I thought others might find the site helpful ;)

-Leigh

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Header image adapted from atomicjeep