Advertisement
Guest User

convertPDFPageToImage

a guest
Jun 15th, 2012
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     <!---
  2.         Adapted from Dan's Blog
  3.         http://blog.pengoworks.com/index.cfm/2008/4/8/UDF-for-converting-a-PDF-page-to-Images-using-CF8-and-Java
  4.     --->
  5.     <cffunction     name="convertPDFPageToImage" access="public" returntype="any" output="false"
  6.                     hint="Creates a thumbnail from a PDF file. Returns image path or Coldfusion image based on if destination was set.">
  7.         <cfargument name="filePath" type="string" required="true" hint="Path of source PDF file.">
  8.         <cfargument name="page" type="numeric" required="true" default="1" hint="Page to convert to a thumbnail.">
  9.         <cfargument name="destination" type="string" default="" hint="Path to save image file. It may be a directory or contain the file name.">
  10.         <cfargument name="type" type="string" default="png" hint="The type of image to write. If destination includes file, the file extension overrides this." />
  11.         <cfargument name="width" type="numeric" default="-1" hint="Uses scale-to-fit and will be constrained by (width, height or both) depending on what is set." />
  12.         <cfargument name="height" type="numeric" default="-1" hint="Uses scale-to-fit and will be constrained by (width, height or both) depending on what is set." />
  13.         <cfargument name="highResRender" type="boolean" default="true" hint="Whether or not to use high quality rendering." />
  14.         <cfargument name="highResImage" type="boolean" default="false" hint="Whether or not to extract a high quality image." />
  15.         <cfargument name="transparent" type="boolean" default="false" hint="Should the image contain transparencies?" />
  16.         <cfargument name="interpolation" type="string" default="highestQuality" hint="Specify the interpolation type when scaling the image." />
  17.         <cfargument name="quality" type="numeric" default="0.85" hint="The JPG quality (if writing to JPG.)" />
  18.         <cfargument name="password" type="string" hint="Password to unlock PDF file." />
  19.  
  20.  
  21.         <cfset var local = {} />
  22.  
  23.  
  24.         <!--- Validate image type --->
  25.         <cfset arguments.type = lcase(arguments.type) />
  26.         <cfset local.typesList = "gif,jpg,png,tif" />
  27.  
  28.         <cfif not find(arguments.type, local.typesList)>
  29.             <cfset arguments.type = "png" />
  30.         </cfif>
  31.  
  32.  
  33.         <!--- Destination set, file will be saved --->
  34.         <cfif len(arguments.destination)>
  35.  
  36.             <!--- Validate destination and thumbnail name --->
  37.             <!--- Path appears to contain a file --->
  38.             <cfif len(listLast(arguments.destination, ".")) eq 3>
  39.                 <cfset local.fileName = getFileFromPath(arguments.destination) />
  40.                 <cfset local.filePath = getDirectoryFromPath(arguments.destination) />
  41.  
  42.                 <cfset local.extension = lcase(right(local.fileName, 3)) />
  43.                 <cfif not find(local.extension, local.typesList)>
  44.                     <cfset local.extension = arguments.type />
  45.                 <cfelse>
  46.                     <cfset arguments.type = local.extension />
  47.                 </cfif>
  48.  
  49.                 <cfset local.fileName = left(local.fileName, len(local.fileName) - 3) & local.extension />
  50.  
  51.  
  52.             <!--- Path has no file --->
  53.             <cfelse>
  54.                 <cfset local.fileName = reReplaceNoCase(getFileFromPath(arguments.filePath), ".pdf$", "." & arguments.type) />
  55.                 <cfset local.filePath = arguments.destination />
  56.             </cfif>
  57.  
  58.  
  59.             <!--- At this point, filePath should not contain the fileName --->
  60.             <cfif right(local.filePath, 1) neq "/" and right(local.filePath, 1) neq "\">
  61.                 <cfset local.filePath &= "/" />
  62.             </cfif>
  63.  
  64.  
  65.             <!--- Verify destination exists --->
  66.             <cfif not directoryExists(local.filePath)>
  67.                 <cfset directoryCreate(local.filePath) />
  68.             </cfif>
  69.         </cfif>
  70.  
  71.  
  72.         <!--- Validate page number --->
  73.         <cfif arguments.page lt 1>
  74.             <cfset arguments.page = 1 />
  75.         </cfif>
  76.  
  77.  
  78.         <cfset local.pdfDecode = createObject("java", "org.jpedal.PdfDecoder").init(javacast("boolean", true)) />
  79.         <!--- <cfset local.pdfDecode.showAnnotations = javacast("boolean", false) /> --->
  80.         <cfset local.pdfDecode.useHiResScreenDisplay(javacast("boolean", arguments.highResRender)) />
  81.         <cfset local.pdfDecode.setExtractionMode(javaCast("int", 0)) />
  82.         <cfset local.pdfDecode.openPdfFile(javacast("string", arguments.filePath)) />
  83.  
  84.  
  85.         <cfif structKeyExists(arguments, "password") and len(arguments.password)>
  86.             <cfset local.pdfDecode.setEncryptionPassword(javacast("string", arguments.password)) />
  87.         </cfif>
  88.  
  89.  
  90.         <cfif arguments.highResImage>
  91.             <cfif arguments.transparent>
  92.                 <cfset local.bufferedImage = local.pdfDecode.getPageAsHiRes(javacast("int", arguments.page), javacast("boolean", true)) />
  93.             <cfelse>
  94.                 <cfset local.bufferedImage = local.pdfDecode.getPageAsHiRes(javacast("int", arguments.page)) />
  95.             </cfif>
  96.         <cfelse>
  97.             <cfif arguments.transparent>
  98.                 <cfset local.bufferedImage = local.pdfDecode.getPageAsTransparentImage(javacast("int", arguments.page)) />
  99.             <cfelse>
  100.                 <cfset local.bufferedImage = local.pdfDecode.getPageAsImage(javacast("int", arguments.page)) />
  101.             </cfif>
  102.         </cfif>
  103.  
  104.         <cfset local.pdfDecode.closePdfFile() />
  105.  
  106.  
  107.         <!--- Extraction complete, we now have a buffered image to work with --->
  108.         <cfset local.pageImage = imageNew(local.bufferedImage) />
  109.  
  110.  
  111.         <cfif arguments.width gt 0 and arguments.height gt 0>
  112.             <cfset imageScaleToFit(local.pageImage, arguments.width, arguments.height, arguments.interpolation) />
  113.  
  114.         <cfelseif arguments.width gt 0>
  115.             <cfset imageScaleToFit(local.pageImage, arguments.width, "", arguments.interpolation) />
  116.  
  117.         <cfelseif arguments.height gt 0>
  118.             <cfset imageScaleToFit(local.pageImage, "", arguments.height, arguments.interpolation) />
  119.         </cfif>
  120.  
  121.  
  122.         <!--- Determine the object to be returned --->
  123.         <cfif len(arguments.destination)>
  124.             <cfif arguments.type eq "jpg">
  125.                 <cfset imageWrite(local.pageImage, local.filePath & local.fileName, arguments.quality) />
  126.             <cfelse>
  127.                 <cfset imageWrite(local.pageImage, local.filePath & local.fileName) />
  128.             </cfif>
  129.  
  130.             <cfset local.returnObject = local.filePath & local.filename />
  131.         <cfelse>
  132.             <cfset local.returnObject = local.pageImage />
  133.         </cfif>
  134.  
  135.  
  136.         <cfreturn local.pageImage />
  137.     </cffunction>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement