Raymond Camden's Blog Rss

Using DDX and CFPDF to set the initial page for a PDF

10

Posted in ColdFusion | Posted on 12-23-2011 | 1,954 views

Earlier this morning a reader asked about how to send a user a PDF that opened at a particular page. Apparently it is possible to link to a PDF and pass a URL parameter for the page you want to open to, but in his case, he was serving up the PDF via cfcontent. I thought it might be possible to do via DDX, and after searching, I found that this use case is actually documented in the ColdFusion docs, but unfortunately, the XML isn't complete and I had to struggle a bit to get it work. Here's how our docs show it:

view plain print about
1<?xml version="1.0" encoding="UTF-8"?>
2<DDX xmlns="http://ns.adobe.com/DDX/1.0/"
3xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4xsi:schemaLocation="http://ns.adobe.com/DDX/1.0/ coldfusion_ddx.xsd">

5<PDF result="Out1" initialView="firstView">
6...
7<InitialViewProfile name="firstView" show="BookmarksPanel" magnification="FitPage"
8openToPage="2"/>

9...
10</DDX>

Not only is that DDX not complete, it isn't right either. Here's the proper DDX:

view plain print about
1<?xml version="1.0" encoding="UTF-8"?>
2<DDX xmlns="http://ns.adobe.com/DDX/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ns.adobe.com/DDX/1.0/ coldfusion_ddx.xsd">
3<InitialViewProfile name="firstView" openToPage="2"/>
4<PDF result="Out1" initialView="firstView">
5    <PDF source="In1" />
6</PDF>
7</DDX>

Note that the InitialViewProfile tag is outside of the PDF tag block. Also note we have to specify an input field. So let's put it together in a full example:

view plain print about
1<cfset pdfFile = "C:\Users\Raymond\Documents\My Dropbox\Misc Docs\rc120-010d-solr.pdf">
2
3<cfsavecontent variable="ddxString">
4<?xml version="1.0" encoding="UTF-8"?>
5<DDX xmlns="http://ns.adobe.com/DDX/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ns.adobe.com/DDX/1.0/ coldfusion_ddx.xsd">
6<InitialViewProfile name="firstView" openToPage="2"/>
7<PDF result="Out1" initialView="firstView">
8    <PDF source="In1" />
9</PDF>
10</DDX>
11</cfsavecontent>
12
13<cfset input = {"In1"=pdfFile}>
14<cfset output = {"Out1" = "c:\temp.pdf"}>
15<cfpdf ddxfile="#ddxString#" action="processddx" name="result"
16     inputfiles="#input#" outputfiles="#output#">

17<cfdump var="#result#">

If you've never seen CFPDF/DDX before, then this may be seem a bit weird, but basically, ColdFusion passes the XML as instructions to an embedded Livecycle Doohicky within ColdFusion. These instructions expect inputs and outputs (exactly what depends on the DDX being run), so we pass in an input struct and output struct. I've got hard coded values here but normally it would be dynamic.

And that's it. As I told the person in the other thread, DDX operations are binary and going to be slower than normal. You may want to consider caching this PDF so you can use it next time instead of generating it every time. (In general, any time you do file operations you probably want to store and cache the result. Disk space is cheap. S3 even cheaper.)

Comments

[Add Comment] [Subscribe to Comments]

Thanks Ray, this example works like a charm. I was wondering if its possible to use the same technique to open a pdf to a specific 'Destination' within the PDF. I've looked at the documentation, and don't see how to do this using the openToPage value. The CoverPage attribute looks like it maybe a way to open to a destination. Any ideas?
Actually it looks like the destination can be accessed by simpy adding #destinationName to the end of the URL. This works even when the document is returned using cfcontent.
Glad you figured it out. My job here is done. ;)
Trying this out, but the DDXis not valid because the schema, coldfusion_ddx.xsd, can't be located. Am I missing something?
You get that error? Where? While the xsd does not exist, I'm able to run the code just fine.
Thanks for the response. I am a newbie so I assumed I must have missed some important detail. I created the DDX file in Visual Studio assuming since it is an xml file the editor would work out. After posting here, I re-created it in notepad and now it is working fine -- go figure!
Hi Ray,

Thanks for this post.
I managed to set magnification level using your sample code.
I only had a problem with the ddx which was not valid unless i wrote it all in a single line into the cfsavecontent tag.
It also doesn't work when you open the pdf in Chrome but that's a problem with Chrome, not with cf, i guess.
How does it fail in Chrome? I generate PDFs w/ Chrome via CF all the time. (Ok, not all the time, but I have in the past.)
Ok sorry it seems it was some cache issue, it opens now correctly to the specified magnification percentage.
Thanks again
Great post!
When I open the file created, you can open "print dialog" on pdf merged immediately? If possible, how do I change the DDX?

[Add Comment] [Subscribe to Comments]