ColdFusion 8: Working with PDFs (Part 8)

Time for the last entry concerning the <CFPDF> tag. This isn't the end of the series - just the last post I'll be writing about the tag. After this we move on to PDFs and Forms which should be pretty darn exciting as well.

Today's entry involves the thumbnails feature of <CFPDF>. I talked about this in the past when I created my getThumbnail UDF. This UDF used the feature that I'll be discussing today.

Let's start by talking about how the thumbnails work. ColdFusion provides you with the following main options for thumbnails:

  • For formats - you can use TIFF, JPEG, or PNEG
  • You have two resolution options - low and high. This is a bit surprising since most other image related options give you more finer control, but hey, sometimes simpler is better.
  • You can scale the image from 1 to 100% of the PDF size.
  • You can specify which page to generate thumbnails from. My getThumbnails UDF simply used the first page.

Along with these options, you have a few other settings as well I'll discuss later. Let's look at a simple example:

<cfset dir = expandPath("./thumbs")>
<cfif not directoryExists(dir)>
   <cfdirectory action="create" directory="#dir#">
</cfif>

<cfdocument format="pdf" name="mypdf">
<h1>Top 10 Reasons why ColdFusion 8 Kicks Rear</h1>

<p>
10. Ajax UI controls make my design suck less.<br />
9. 500% faster. Seriously - 500% faster. That's like... fast.<br />
8. <cfajaxproxy> - Couldn't Adobe make Ajax easier? No.<br />
7. Debugging. Not that CF developers make mistakes.<br />
6. No more d*** custom MySQL drivers.<br />
5. PDF toolset that makes PDF sexy. And that's saying a lot.<br />
4. Interaces. Yeah, I said Interfaces. Whooyah.<br />
3. Server Monitor - and it's all Flex.<br />
2. Server Monitor API - in case you don't want it all Flex.<br />
1. It's not Dot Net.<br />
</p>
</cfdocument>

<cfpdf action="thumbnail" source="mypdf" destination="#dir#" resolution="high" scale="50" overwrite="true">

Most of the code isn't terribly relevant to thumbnails. I start off creating a thumbs folder if it doesn't exist. Then a PDF document variable is created. (Go ahead. Read. Laugh. I'm going to make a tee shirt I think.) The critical line is the last one. I specify the thumbnail action for my CFPDF tag. The source is the PDF I created in memory earlier. CFPDF needs to know where to store it - so I pass in the "dir" folder I created earlier. I used a high resolution and a scale of 50 and lastly set overwrite to true. That generated this graphic (note I cropped the image a bit):

Let me point out something. The name of this file is thumbnail_page_1.jpg. Where did this name come from? ColdFusion automatically uses a name of the format:

prefix_page_N.TYPE

You can overwrite the prefix by suppling the imagePrefix attribute. If you used ray, for example, you would end up with images named ray_page_1.jpg, ray_page_2.jpg, etc. If you leave imagePrefix blank, it defaults to thumbnail, unless your source is a PDF file. In that case the filename is used instead. Consider this modification to the previous example:

<cfpdf action="thumbnail" source="mypdf" destination="#dir#" resolution="high" scale="50" overwrite="true" imagePrefix="t">

This will generate file names like: t_page_1.jpg, t_page_2.jpg, etc.

So what's cool about this? You can provide image previews of PDFs before users download them. You could even wrap a link to a PDF with a <cftooltip> tag that shows the image as a tool tip. As an example:

<cftooltip tooltip="<img src='thumbs/t_page_1.jpg'>">
<a href="mypdf.pdf">My PDF</a>
</cftooltip>

Comments

This is a very neat feature. If you don't pass in the page attribute, does it create thumbnails of all the pages in the PDF? I think the imagePrefix attribute could be essential where you're doing this for many pdfs; you don't want to create a new folder for each pdf (or do you?), so you could use the original filename for the prefix to differentiate them all.
# Posted By duncan | 7/31/07 3:32 AM
Yes, if you don't specify pages, it does them all. If I were doing thumbnails for random PDFs, imagePrefix could be used to help keep them separate. But don't forget that if you keep it blank and use a source that points to a filename, CFPDF will use the name for a prefix.
# Posted By Raymond Camden | 7/31/07 7:27 AM
aha, that's handy. I hadn't noticed the line "unless your source is a PDF file. In that case the filename is used instead."
# Posted By duncan | 7/31/07 9:31 AM
I have to say I'm surprised no one commented on my Top 10 List. ;)
# Posted By Raymond Camden | 7/31/07 1:48 PM
I have some question
May you can help me
1 auto print pdf by user don't click printer icon
2 how to use parmision for print copy (i try permision auttibue some time but don't work)
# Posted By PP | 9/28/07 4:48 AM
1) As far as I know, you can't do this. You can't force something to print on the client's machine. You _can_ use JS to show the Print dialog, but it won't auto print. I don't think the JS trick will work for a PDF though.

2) Yes, you can use CFPDF to set permissions on a PDF, including the ability to print.
# Posted By Raymond Camden | 9/28/07 9:56 AM
The first method can be done using javascript in Acrobat. Here's an example of how to do it silently using itext, so the user doesn't see the print dialogue:
http://princeoflightning.blogspot.com/2006/01/sile...
# Posted By duncan | 9/28/07 10:17 AM
That's pretty interesting. I'm trying now to see if I can do this JS in cfdocument. So far I've not been successful.
# Posted By Raymond Camden | 9/28/07 10:31 AM
Ray, have you done anything with PDF forms yet? You hinted at it in the beginning of the entry... I'm only now learning my way around the new PDF features and I have to learn how to create PDF forms, convert html to PDF forms and populate and read data from existing PDF forms.
# Posted By Michael White | 10/1/07 10:49 AM
Not yet. The books kinda kicked my butt. Now that they are done I'm hoping to get more articles like this out.
# Posted By Raymond Camden | 10/1/07 2:30 PM
Thanks for posting the new PDF features in Coldfusion 8. I've learned a lot. If I may, I have a dumb question that I would like to ask because I can't find any help with this...To view the PDF you create with the CFPDF tag does Coldfusion forms have a control available for viewing or will Acrobat Reader always execute when the PDF is called?
# Posted By Mike | 3/17/08 7:23 PM
If I read you right, my understanding is that you can't control what plugin is used - the browser (user) controls that. You can give a hint by using cfcontent, but at the end of the day, the end user decides what to do with your binary data.
# Posted By Raymond Camden | 3/18/08 11:18 AM
"Interaces"???
# Posted By Michael Jones | 9/30/08 4:45 PM