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>

Archived Comments

Comment 1 by duncan posted on 7/31/2007 at 12:32 PM

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.

Comment 2 by Raymond Camden posted on 7/31/2007 at 4:27 PM

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.

Comment 3 by duncan posted on 7/31/2007 at 6:31 PM

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."

Comment 4 by Raymond Camden posted on 7/31/2007 at 10:48 PM

I have to say I'm surprised no one commented on my Top 10 List. ;)

Comment 5 by PP posted on 9/28/2007 at 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)

Comment 6 by Raymond Camden posted on 9/28/2007 at 6:56 PM

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.

Comment 7 by duncan posted on 9/28/2007 at 7:17 PM

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.bl...

Comment 8 by Raymond Camden posted on 9/28/2007 at 7:31 PM

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.

Comment 9 by Michael White posted on 10/1/2007 at 7:49 PM

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.

Comment 10 by Raymond Camden posted on 10/1/2007 at 11:30 PM

Not yet. The books kinda kicked my butt. Now that they are done I'm hoping to get more articles like this out.

Comment 11 by Mike posted on 3/18/2008 at 4:23 AM

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?

Comment 12 by Raymond Camden posted on 3/18/2008 at 8:18 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.

Comment 13 by Michael Jones posted on 10/1/2008 at 1:45 AM

"Interaces"???

Comment 14 by Collectonian posted on 3/13/2009 at 7:58 PM

I'm trying to generate a thumbnail of the first page of a PDF using the CFPDF tag, however the thumbnails all come up blank except for a gray line. The code isn't throwing any errors and the PDFs do have content. Any ideas? (this is on ColdFusion 8 on Linux, if that might be the problem)

<cfpdf action = "thumbnail"
source="#application.basedirectory#/pdfs/#ArticleDetails.FileName#"
destination = "#application.basedirectory#/thumbnails/"
format = "jpeg"
imagePrefix = "thumbnail_#whicharticle#"
overwrite = "yes"
pages = "1" >

Comment 15 by Raymond Camden posted on 3/14/2009 at 2:39 AM

Hmm. If you get rid of pages=1 and do all the pages of the pdf, does it show anything different?

Comment 16 by Collectonian posted on 3/14/2009 at 4:29 AM

Nope. The remaining thumbnails generated are also all blank except for a small grey line at the bottom (which isn't in the PDFs).

Comment 17 by Raymond Camden posted on 3/15/2009 at 5:01 AM

Sorrr tyen - you got me. Make sure you patched up I guess.

Comment 18 by John Manoah posted on 4/2/2009 at 11:46 AM

Great post Ray. It was very useful.

In the current application I'm creating, I tried using cfdocument to create a PDF of any file type (word, ppt, excel etc). Your solution seems to work only for PDFs and URLs. The thumbnail generated for images,word etc was hazy.

Do you have a solution to create thumbnails for any file format?

thanks in advance!
John

Comment 19 by Raymond Camden posted on 4/2/2009 at 3:19 PM

Nope, no solution for other formats. I mean I don't know of any - I'm sure they exist.

Comment 20 by Candice posted on 5/7/2009 at 10:23 PM

Thanks for the post Ray. Is there a way to specify the UNIX permissions (mode?) of the image that gets created?

Comment 21 by Raymond Camden posted on 5/8/2009 at 2:12 AM

fileSetAccessMode?

Comment 22 by Dominic O&aposConnor posted on 6/29/2009 at 10:12 PM

Has anyone had any issue with image sizes using action="thumbnail"? If I set scale="100" the image I get back is about 75% of the PDF size.

Comment 23 by Terry posted on 9/2/2009 at 11:35 PM

Just something else to put throw out there.

I started messing with pdf and cfdocument and came across these blogs. Awesome job Ray.

I noticed that when I started using cfpdf and throwing the output threw cfcontent that the saveAsName attribute in cfdocument becomes worthless and that instead had to revert back to putting the name back to cfheader.

Btw, miss the meetings up at langley with the user group. Glad to see you are making it big time.

Comment 24 by Geoffrey C Barth posted on 10/22/2009 at 10:12 PM

Anyone have any trouble taking a PDF file and using acrobat 9 standard to reduce file size? When doing so this prevents cfpdf from reading the image when using action "thumbnail". I kept doing this until reduce (make compaible with) ver 4 and they CFPDF would then again beable to return an image. Similar things happen with pages that have been rotated.

Comment 25 by Mike Demahy posted on 4/23/2010 at 11:14 PM

I want ot merge a pdf file and change the size from a custom size to legal. any ideas?

Comment 26 by Raymond Camden posted on 4/24/2010 at 12:46 AM

I haven't tested this, but what if you make an empty/virgin PDF in the right size, and then merge your other ones into it?

Comment 27 by Mike Demahy posted on 4/24/2010 at 1:30 AM

I am merging a receipt that is 5.25 x 6.30 with several other pages that are all legal. the receipt is added as another page with the original page size

Comment 28 by Raymond Camden posted on 4/24/2010 at 1:31 AM

How about the transform action? With hscale/vscale?

Comment 29 by Sam posted on 5/14/2010 at 8:39 AM

Hi Ray,
Love the PDF articles... Thanks!

Now, I was wondering if there is any way to make higher resolution JPG images from a PDF? I am using scale="100" and resolution="high" but the images are just not quite high enough rez for what I need.

Is it possible?

Also, what IS the file size going to be at scale="100" in pixles... will it always be the same max width or height no matter what PDF you use? Or is it relative to the pagesize of the PDF file you use?

One more thing... I am seeing some pretty serious color swings when using CMYK PDF files. Is the only way around this to create an RGB version of the PDF file?

Thanks again mate!

Comment 30 by Raymond Camden posted on 5/14/2010 at 3:11 PM

As far as I know, scale=100/res=high is as good as your going to get.

Size: Honestly don't quite get you there. I believe it is the same size as the page.

Color: Definitely no idea on that one.

Comment 31 by Ranga posted on 5/20/2010 at 2:45 AM

Hi Ray

I have the same problem as Sam. The thumbnails created from CMYK PDF files are very dark and very contrasty. There is no problem when I create thumbnails of RGB PDF file.

Comment 32 by Ron Rattie posted on 12/6/2011 at 6:38 AM

Just thought I'd add what I did with this.

Using what is above, I put:

<cfpdf action="thumbnail" source="orgchart" resolution="high" scale="100" overwrite="true" format="png" hires="yes">
<cflocation url="thumbnails/slide_image.cfm" addtoken="no">

at the bottom of the cfdocument page and for slide_image.cfm
it's just a simple cfm page with:

<body>
<img src="thumbnail_page_1.png" alt="this is the png file, if you can't see it your browser doesn't support png files." />
</body>

So, a link on a page takes you to the page that generates the PDF, then the thumbnail and then immediately takes you to another page with the image. This is so that users could have a high quality image of the dynamic PDF in question to use in a PowerPoint slide show.

Comment 33 by Raymond Camden posted on 12/6/2011 at 6:58 AM

You probably want to add some form of caching to that. PDF (and file ops in general) are a bit slow.