Monday, April 21, 2008

Populate and Merge multiple pdf forms

If you need to populate multiple PDF forms and then merge them together... Here is how to do that.

 

<cfset sourceform1 = "#ExpandPath('formsamples/expense_claim_form1.pdf')#">

<cfset sourceform2 = "#ExpandPath('formsamples/expense_claim_form2.pdf')#">
 
<cfset resultfile1 = "#ExpandPath('result1.pdf')#">
<cfset resultfile2 = "#ExpandPath('result2.pdf')#">
 
<cfset flatfile1 = "#ExpandPath('flat1.pdf')#">
<cfset flatfile2 = "#ExpandPath('flat2.pdf')#">
 
<cfset finalresultfile = "#ExpandPath('finalresult.pdf')#">
 
<cfset myname1 = "source_test1">
<cfset myempcode1 = "source_code1">
<cfset mylocation1 = "source_loc1">
<cfset mymgrname1 = "source_mgr1">
<cfset mymgremail1 = "source1@email.com">
 
<cfset myname2 = "source_test2">
<cfset myempcode2 = "source_code2">
<cfset mylocation2 = "source_loc2">
<cfset mymgrname2 = "source_mgr2">
<cfset mymgremail2 = "source2@email.com">
<!--- Fill up the pdf form1 ---> 
<cfpdfform source= "#sourceform1#" action="populate" destination="#resultfile1#" overwrite="true">
 <cfpdfsubform name="form1">
  <cfpdfsubform name="expense">
   <cfpdfformparam name="name" value="#myname#">
   <cfpdfformparam name="empcode" value="#myempcode#">
   <cfpdfformparam name="location" value="#mylocation#">
   <cfpdfformparam name="mgrname" value="#mymgrname#">
   <cfpdfformparam name="mgremail" value="#mymgremail#">
  </cfpdfsubform>
 </cfpdfsubform>
</cfpdfform>
 
<!--- Fill up the pdf form2 ---> 
<cfpdfform source= "#sourceform2#" action="populate" destination="#resultfile2#" overwrite="true">
 <cfpdfsubform name="form1">
  <cfpdfsubform name="expense">
   <cfpdfformparam name="name" value="#myname#">
   <cfpdfformparam name="empcode" value="#myempcode#">
   <cfpdfformparam name="location" value="#mylocation#">
   <cfpdfformparam name="mgrname" value="#mymgrname#">
   <cfpdfformparam name="mgremail" value="#mymgremail#">
  </cfpdfsubform>
 </cfpdfsubform>
</cfpdfform>
 
 
<!--- Flatten the filled-up pdf forms first. --->
<cfpdf action="write" source="#resultfile1#" destination="#flatfile1#" flatten="yes" overwrite="true">
<cfpdf action="write" source="#resultfile2#" destination="#flatfile2#" flatten="yes" overwrite="true">
 
<!--- Now merge the flattened forms. ---> 
<cfpdf action="merge" source="#flatfile1#,#flatfile2#" destination="#finalresultfile#" overwrite="true">
 
<cfoutput>Done...</cfoutput>

 


11 comments:

Javi said...

Ahamad,

Thanks so much for the post! The only problem though is that this is for Acrobat made only PDFs correct?

The problem we are running into is that we are trying to populate several dynamic PDFs made in LiveCycle Designer and then merge them into 1 PDF. The docs state though that you cannot flatten LiveCycle Designer made docs. Can we still achieve this by using PDFs created in LiveCycle Designer?

Note that everything works fine, even the merging, its just that when we assemble the XFA (xmlData) populated PDF's the data for each of the fields no longer shows up in Acrobat Reader. Is what I'm trying to build achievable or do I need to use DDX? Or revert my forms into Acrobat ones? Thanks again for taking the time to post about this!

ahamad said...

Hi Javi,
Can you send across the some sample forms and code so that I can test it on my side here...


Thanks,
-ahamad
ahamadk1@gmail.com

Morgan said...

Hi,

Just wanted to say that I have the same challenge as Javi.

Feels strange if we cannot flatten LiveCycle created forms...

Javi said...

Morgan,

Glad to see someone else is seeing the same challenge. The thing is CFPDF has a default value of "no" for flatten and either way you can't flatten LiveCycle forms. What does that necessarily do?

We actually aren't treating these PDFs as forms but rather as templates. The PDFs have disabled form fields that we populate with data from XML (using xmlData attr) and then merge them but once that happens the populated data no longer appears. Are you running into a similar problem?

I'm wondering if CFPDF does what we are trying to do or not. I haven't tried DDX so not sure if thats what I should be using. Right now I just populate and save the PDFs to the file system in a generated directory and then point CFPDF to merge all PDFs in that folder.

Sven Ramuschkat said...

Hi,
the possibility to flatten real LC Designer Forms would we a great feature to ColdFusion, because I makes no sense to use the old AcroForms anymore.

The only way to flatten LC forms today is to use LC Output, which is really expensive ;-(

Sven

Greg said...

Ahamad, thanks for the great blog. As already noted, flattening works only for AcroForms(not LiveCycle Designer XFA Forms).

I use CFPDF action="populate" to pre-fill Designer XFA forms.

Our problem is that not every one of our customers can view these forms because they don't have Acrobat/Reader 8. We also have a print center that is not PDF1.7 compatible. They can only handle PDF1.4 format.

I have been trying to figure out a way to flatten these pre-filled XFA forms (convert to PDF1.4) since the ColdFusion 8 pre-release program.

So far I haven't had much luck. Are their any solutions available yet like:

1) DDX
2) silent command-line call to acrobat/reader similar to File>Print>Adobe PDF>Print to file?
3) PDF Library API

Thanks for all your help.

Greg

Anonymous said...

I use cfdocument to print out personnel biographies. You select an employee, click print, and bam, a .pdf is generated. What I need to do is allow a user to pick a few biographies, and click print and generate one pdf with multiple biographies. Not sure how to accomplish this... any suggestions?

Joe Zack said...

It sounds like I'm running into the same problem as everyone else. Anyone come up with a solution yet?

cw said...

It appears that I have run into this problem as well. I reported it as a bug. How are we supposed to merge multiple LiveCycle forms together?

This seems kind of weird that they don't support this? Were they testing it on an old version of Adobe?

ahamad said...

Hi,
As such we have certain known issues with the CF8.0 build.
1. XFA Forms(Livecycle) forms cannot be flattened.
2. If you merge a PDF Form with an existing PDF, the form data is lost in the resultant pdf – I think this has been fixed for updater.(I need to verify this)
3. cfpdf – merge – You cannot merge two pdfforms; you can merge pdf files.

There are certain implementation difficulties but still we are working on these.

Anonymous said...

Good post.

Remember to flatten your files, or else the form values you enter will get stripped out.