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

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

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

Election day plus 1: Social Media Nightmare

It is election day plus 1, and a very emotional day with no way to win for many.

Social media was a force to be reckoned with this election year.  No matter what position you take, the world is ready to pounce on you and tell you why YOU are evil, intolerant or a deplorable -- and the reason for all their woes.  This isn't a new problem, though...  All of the liberal vs conservative issues are causing rifts between otherwise civil people, and it seems to be the worst on Facebook and twitter.

If I say I am for Hillary:
i am part of the "corrupt system."

If I say I am against Hillary:
I am stupid and uneducated

If I say I am for Trump:
I am against women,
I am religiously intolerant,
I am a bigot

If I say I am against Trump:
I am against the 2nd amendment,
I don't have any idea how government works,
I am part of the problem with American society,
I am against the police

Stay with me now, this isn't meant to make people angry, but it is mean to shine some light on a social …