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. </ul> 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>
Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments