We help IT Professionals succeed at work.

Using JPedal and ColdFusion

This is my first attempt at using a JAVA tool within ColdFusion and could really use a hand with this.

Can someone give me step by step, if possible, on how to install and use this tool to generate a thumbnail image from a PDF file from within ColdFusion?

They say its possible, but I can  not find any help on the matter.  Thank you in advance for anything you can do to assist me.
Comment
Watch Question

Most Valuable Expert 2015

Commented:
I don't use it myself. But installing a java jar is usually just placing it in the CF class path, like in the {cf_root}\wwwroot\WEB-INF\lib directory.  Then restarting CF.  Then you call the java obj's from CF code.  The rest is translating whatever java code the lib. uses into CF.  But it looks like they might have a utility you can use.  At least for the commercial version.  

http://www.jpedal.org/support_egEPT.php

Author

Commented:
Got the java installed and at least somewhat working.  I didnt realize I needed to restart CF,  so dumb move there by me.

Now that I have it installed , I am trying to pass the three parameters, but am getting errors.  This is what I currently have in place.


<cfobject action="Create" type="java" class="org.jpedal.examples.images.ExtractPagesAsThumbnails" name="jpedal">
<cfset ret=jpedal.init("\\dcsh245wb\Test.pdf", "jpg", 50)>

This is based on the examples I have seen so far, but I get the error:

Unable to find a constructor for class org.jpedal.examples.images.ExtractPagesAsThumbnails that accepts parameters of type ( java.lang.String, java.lang.String, java.lang.String ).

Any ideas?
Most Valuable Expert 2015

Commented:
From the example page, my guess would be it's calling the standard main() method, not the constructor.  Dump the object first. Does it have a method called main()?
         <cfdump var="#jpedal#">

If it does, try calling main() instead. You pass in the arguments as a string array.  Something like

<cfobject action="Create" type="java" class="org.jpedal.examples.images.ExtractPagesAsThumbnails" name="jpedal">
<cfset args = arrayNew(1)>
<cfset arrayAppend(args , "\\dcsh245wb\Test.pdf")>
<cfset arrayAppend(args , "jpg")>
<cfset arrayAppend(args , "50")>
<cfset jpedal.main( args )>



Author

Commented:
CFDUMP returns the following:

object [unknown type]
OutputDir [undefined value]  
isTest NO  
outputMessages NO  

I am using the jpedal_demo.jar, not sure if that matters though
Most Valuable Expert 2015

Commented:
It might.  I'm not sure if the demo jar does everything the commercial version does.  Try the command line example in the link first.  (Depending on your config, you might also have to provide the full path to java.exe...).  If that doesn't work, then it won't work from CF either.

     http://www.jpedal.org/support_egEPT.php

If it does, you could try calling main() anyway.  It probably won't work, but you never know ;-)  
Most Valuable Expert 2015

Commented:
> I am using the jpedal_demo.jar, not sure if that matters though

Unfortunately, the demo jar won't work. I downloaded it and it doesn't include the ExtractPagesAsThumbnails class. So that's why it's not working for you.  

Author

Commented:
Is there a way to see ANY class from the demo work?  Its a $950 plug in that I am unwilling to buy unless I know it does what I need.  According to the download pge, the demo should work, but show a big red X on the resulting image.
Most Valuable Expert 2015
Commented:
Totally understandable.  I found a small example that will work (ie PdfDecoder) here:
http://www.jpedal.org/simple_image_example.php


CF code:
<cfset sourceFile = "c:\mySampleFile.pdf">
<cfset imageName  = "page1.jpg">
<cfset imageExt   = listLast(imageName, ".")>

<cfset PdfDecoder = createObject("java", "org.jpedal.PdfDecoder")>
<cfset decode_pdf = PdfDecoder.init(true)>
<cfset PdfDecoder.setFontReplacements(decode_pdf)>
<cfset decode_pdf.openPdfFile(sourceFile)>
<!--- get page 1 as an image. see example for ranges --->
<cfset img = decode_pdf.getPageAsImage(1)>
<!--- close the pdf file --->
<cfset decode_pdf.closePdfFile()>

<!--- create the image in current directory --->
<cfset imagePath = ExpandPath( imageName )>
<cfset imageFile = createObject("java", "java.io.File").init( imagePath )>
<cfset ImageIO = createObject("java", "javax.imageio.ImageIO")>
<cfset ImageIO.write(img, imageExt, imageFile)>

<cfoutput>
   <img src="#imageName#">
</cfoutput>


Anything more involved is going to involve delving into their examples (ie java code). But if quality is important, you really should test the ExtractPagesAsThumbnails and HiResThumbnailExtraction  classes. I don't know if they offer any precompiled versions, but they do provide the source code. So you could always compile it yourself, add it to a jar, and install it on CF just like the jpedal jar.


Author

Commented:
Returns something, although not the full document, so this process at least works.  Will purchase the full version and see how it goes!   Thank you for all your help!
Most Valuable Expert 2015

Commented:
Yes, the example is just for a single page.  The java code says you can extract all pages using a loop:

     // ....
    //page range if you want to extract all pages with a loop
    //int start = 1,  end = decode_pdf.getPageCount();

So you'd put the extract portion a cfloop. Not tested, but something like:

<cfloop from="1" to="#decode_pdf.getPageCount()#" index="p">
      <cfset img = decode_pdf.getPageAsImage(p)>
      <!--- use whatever naming convention you want here --->
      <cfset imageFile = ExpandPath("yourPDF_page"& p &".jpg")>
      <cfset imageExt  = listLast(imageFile, ".")>

      <!--- create the image in current directory --->
     <cfset imagePath = ExpandPath( imageName )>
     <cfset imageFile = createObject("java", "java.io.File").init( imagePath )>
     <cfset ImageIO = createObject("java", "javax.imageio.ImageIO")>
     <cfset ImageIO.write(img, imageExt, imageFile)>
</cfloop>

<!--- close the pdf file --->
<cfset decode_pdf.closePdfFile()>


>> Will purchase the full version

     If you're planning on upgrading to CF8+ anytime soon, you might try the demo in that
     version first.  I _think_ later versions come some version of jpedal. So there might be
     conflicts if you try and run both.  Just something to keep in mind when choosing a tool :)

Most Valuable Expert 2015

Commented:
Correction:  (Again, not tested ...)

<!--- open and decode the pdf --->
<cfset PdfDecoder = createObject("java", "org.jpedal.PdfDecoder")>
<cfset decode_pdf = PdfDecoder.init(true)>
<cfset PdfDecoder.setFontReplacements(decode_pdf)>
<cfset decode_pdf.openPdfFile(sourceFile)>

<cfloop from="1" to="#decode_pdf.getPageCount()#" index="p">
      <cfset img = decode_pdf.getPageAsImage(p)>
      <!--- use whatever naming convention and path you want here --->
      <cfset imageName = ExpandPath("yourPDF_page"& p &".jpg")>
      <cfset imageExt  = listLast(imageFile, ".")>

      <!--- create the image in current directory --->
     <cfset imagePath = ExpandPath( imageName )>
     <cfset imageFile = createObject("java", "java.io.File").init( imagePath )>
     <cfset ImageIO = createObject("java", "javax.imageio.ImageIO")>
     <cfset ImageIO.write(img, imageExt, imageFile)>

     <cfoutput>
         <img src="#imageName#">
     </cfoutput>
</cfloop>

<!--- close the pdf file --->
<cfset decode_pdf.closePdfFile()>