Skip to main content

Icepdf Alternative to CFPDF for High Resolution PDF Thumbnails

I ran into the same problem that many have with generating high resolution thumbnails from a PDF file.  Using CFPDF, the options for scale and high resolution do nothing.  The reason is that the included jpedal java library doesn't have the included support for high resolution export.  For those of you who have this problem and are as frustrated as I was, here is an option that worked for me.

I tried every which way to leverage the built-in jpedal and in the end I downloaded the trial version from jpedal and using that directly was able to generate higher quality output.  It takes some trickery to load the java class so it doesn't conflict with the built in jpedal and isn't nearly as easy as it should be.  The problem with this of course is that it is trial software that you need to purchase.  Between $800 and $9000 to buy, so not a viable option for the indie coders out there.

I looked deeper into the Railo java libaries and found that IcePdf is included.  I suspect it is used to generate PDFs with cfdocument and other methods, however IcePdf has support built in to do what we want.  Here's a step-through of accomplishing the same high res thumbnail output using the icepdf included with Railo, and I suspect is in Adobe's flavor.  If not, download it for free.

First we instantiate the icepdf Document class:
<cfset objPdf = createobject("java","org.icepdf.core.pobjects.Document")>

We will need two other classes as well.  Don't even bother reading up on them unless you really want to understand the underlying logic :
<cfset Hints = createobject("java","org.icepdf.core.util.GraphicsRenderingHints")>
<cfset Page = createobject("java","org.icepdf.core.pobjects.Page")>


Now initialize the document object passing in your PDF's path and filename:
<cfset objPdf.setFile("#expandpath("/your/path/to")#/your.pdf")>

Now create a java buffered image containing the rendering of the requested page
<cfset myBuffImage = objPdf.getPageImage(pageNumber, Hints.PRINT, Page.BOUNDARY_CROPBOX, 0, zoomFactor)>

Where "pageNumber" contains the number of the page you want, and zoomFactor is an integer representing the scale you want.  1 = 100%;  2 = 200%, 2.5 = 250% and so on.  These end up being *approximate* so experiment until you get the scale you want.

Now we use ImageNew() to convert the BufferedImage into a CFIMAGE:
<cfset myImage = ImageNew(myBuffImage)>

Now write the image out to disk:
<cfimage action="WRITE" source="#myImage#" destination="thumbnail.jpg" />

There you go, output considerably better than CFPDF action="thumbnail" output for $0.

If this was helpful, give a shout out, or hug a baby or something nice like that.

Popular posts from this blog

Making Macbook Air with 128GB SSD usable with Bootcamp

I recently got a new Macbook Air 11" (the 2012 version) and loaded it with goodies like 8GB ram and 2GHz Core i7.  What I DIDN'T upgrade was the internal SSD.  My config came with 128GB SSD and I refused to pay $300+ to upgrade it to 256GB.  Yeah I know, some call me cheap, but SSds cost $75-$150 for 240GB, so adding another 128GB for $300 seemed way too steep for me.  I figured "ok, I'm going to make 128G work!"

Here is the story of how that went...

Installing python 3.4.x on OSX El Capitan

I love "brew" package manager, but sometimes being too progressive breaks things.  I have several python apps that I maintain that get deployed to AWS using Elastic Beanstalk.  AWS eb can deploy with python 2.7 or 3.4.  Any recent 'brew install python3" will get 3.5.1. #annoying

Dell XPS M1330 + Snow Leopard Hackintosh

I have been working with a Dell XPS M1330 laptop for a few years now.  It doesn't quite match up to the newest notebooks in terms of performance, but it certainly still has some life in it.  I had previously installed OSX 10.5.x on it as an experiment, and had moderate success.  I decided to revisit this idea again to install Snow Leopard (OSX 10.6) on the Dell M1330, and keep some notes for those of you brave enough to Hackintosh your own machine...