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 />
- 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
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.
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.
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."
I have to say I'm surprised no one commented on my Top 10 List. ;)
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)
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.
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...
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.
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.
Not yet. The books kinda kicked my butt. Now that they are done I'm hoping to get more articles like this out.
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?
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.
"Interaces"???
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" >
Hmm. If you get rid of pages=1 and do all the pages of the pdf, does it show anything different?
Nope. The remaining thumbnails generated are also all blank except for a small grey line at the bottom (which isn't in the PDFs).
Sorrr tyen - you got me. Make sure you patched up I guess.
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
Nope, no solution for other formats. I mean I don't know of any - I'm sure they exist.
Thanks for the post Ray. Is there a way to specify the UNIX permissions (mode?) of the image that gets created?
fileSetAccessMode?
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.
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.
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.
I want ot merge a pdf file and change the size from a custom size to legal. any ideas?
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?
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
How about the transform action? With hscale/vscale?
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!
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.
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.
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.
You probably want to add some form of caching to that. PDF (and file ops in general) are a bit slow.