Creating PDFs that automatically print via CFPDF and DDX
Here's the situation. A user is viewing a report online. The business owner of the application decided to provide a nice PDF format of the data, all the user needs to do is click a button, perhaps named "print". The CF server generates the PDF, hands it off to the browser through either CFLOCATION or CFCONTENT. So now the user has the PDF loaded in a secondary window and have to go hunt for the print button again. But fear not, dear user! We'll save you by judicious use of XML, Javascript, and ColdFusion.
Some time ago (maybe Acrobat 7), Adobe added embedded Javascript to the PDF format (see the Acrobat JavaScript Scripting Guide). Among other things, this allows the creation of scripts that will execute whenever the PDF is opened in Acrobat Reader (and maybe, though I haven't tested it, with the Adobe PDF web browser plugin). And in ColdFusion 8, Adobe added the CFPDF tag, which allows access to a stripped down set of functions in the LiveCycle DDX format. DDX is an XML-based format for directing PDF manipulation. Currently the Livedocs entry for CFPDF lists a set of DDX elements that are explicitly supported and a set that are explicitly not supported. The Javascript element isn't included in either but it does work, possibly because it is a sub-element of the PDF element. So what we're going to do is to create a Javascript file that tells Reader to bring up the print document, a DDX file that will combine the Javascript file with the PDF, and a CFML file that uses CFPDF to bake it all together.
First, the Javascript file, printDocument.js:
this.print({
bUI: true
});
Second, the DDX file, embedPrintCommand.ddx:
And finally, the CFML script to bring it all home:
Hello World! The time is #timeFormat(now())#
<!--- setup the mapping for the source PDF files --->
<!--- setup the mapping for the result PDF files --->
<!--- process the PDF with the DDX file --->
#result.outputPDF#
Save all three in the same directory and hit the CFML file in your web browser. You should get a helloWorld_out.pdf file. Open that in the Adobe Reader and shortly after opening you should see a print dialog. Pretty cool huh?
About this entry
You're currently reading "Creating PDFs that automatically print via CFPDF and DDX", an entry on hofo.com
- Published:
- Apr 29, 2008 / 2:11 PM
- Category:
- Coldfusion
3 comments
Jump to comment form