Advertisement
dbudde

convertPDFPageToImage

Nov 11th, 2016
772
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="file" type="any" required="true" hint="Path of source PDF file or a byte array." />
  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.         <cfif not isArray(arguments.file) and not (isSimpleValue(arguments.file) and fileExists(arguments.file))>
  22.             <cfthrow message="Invalid path to file or binary for PDF not present." />
  23.         </cfif>
  24.  
  25.  
  26.         <!--- Validate image type --->
  27.         <cfset arguments.type = lcase(arguments.type) />
  28.         <cfset local.typesList = "gif,jpg,png,tif" />
  29.  
  30.         <cfif not find(arguments.type, local.typesList)>
  31.             <cfset arguments.type = "png" />
  32.         </cfif>
  33.  
  34.  
  35.         <!--- Destination set, file will be saved --->
  36.         <cfif len(arguments.destination)>
  37.  
  38.             <!--- Validate destination and thumbnail name --->
  39.             <!--- Path appears to contain a file --->
  40.             <cfif len(listLast(arguments.destination, ".")) eq 3>
  41.                 <cfset local.fileName = getFileFromPath(arguments.destination) />
  42.                 <cfset local.filePath = getDirectoryFromPath(arguments.destination) />
  43.  
  44.                 <cfset local.extension = lcase(right(local.fileName, 3)) />
  45.                 <cfif not find(local.extension, local.typesList)>
  46.                     <cfset local.extension = arguments.type />
  47.                 <cfelse>
  48.                     <cfset arguments.type = local.extension />
  49.                 </cfif>
  50.  
  51.                 <cfset local.fileName = left(local.fileName, len(local.fileName) - 3) & local.extension />
  52.  
  53.  
  54.             <!--- Path has no file --->
  55.             <cfelse>
  56.                 <cfset local.fileName = reReplaceNoCase(getFileFromPath(arguments.filePath), ".pdf$", "." & arguments.type) />
  57.                 <cfset local.filePath = arguments.destination />
  58.             </cfif>
  59.  
  60.  
  61.             <!--- At this point, filePath should not contain the fileName --->
  62.             <cfif right(local.filePath, 1) neq "/" and right(local.filePath, 1) neq "\">
  63.                 <cfset local.filePath &= "/" />
  64.             </cfif>
  65.  
  66.  
  67.             <!--- Verify destination exists --->
  68.             <cfif not directoryExists(local.filePath)>
  69.                 <cfset directoryCreate(local.filePath) />
  70.             </cfif>
  71.         </cfif>
  72.  
  73.  
  74.         <!--- Validate page number --->
  75.         <cfif arguments.page lt 1>
  76.             <cfset arguments.page = 1 />
  77.         </cfif>
  78.  
  79.  
  80.         <cfset local.pdfDecode = createObject("java", "org.jpedal.PdfDecoder").init(javacast("boolean", true)) />
  81.  
  82.  
  83.         <cftry>
  84.             <!--- <cfset local.pdfDecode.showAnnotations = javacast("boolean", false) /> --->
  85.             <cfset local.pdfDecode.useHiResScreenDisplay(javacast("boolean", arguments.highResRender)) />
  86.             <cfset local.pdfDecode.setExtractionMode(javaCast("int", 0)) />
  87.  
  88.             <cfif isArray(arguments.file)>
  89.                 <cfset local.pdfDecode.openPdfArray(arguments.file) />
  90.             <cfelse>
  91.                 <cfset local.pdfDecode.openPdfFile(javacast("string", arguments.file)) />
  92.             </cfif>
  93.  
  94.  
  95.             <cfif structKeyExists(arguments, "password") and len(arguments.password)>
  96.                 <cfset local.pdfDecode.setEncryptionPassword(javacast("string", arguments.password)) />
  97.             </cfif>
  98.  
  99.             <!--- The version of pdfDecode on Lucee 5 does not support getting the page as high res. --->
  100.             <cftry>
  101.                 <cfif arguments.highResImage>
  102.                     <cfif arguments.transparent>
  103.                         <cfset local.bufferedImage = local.pdfDecode.getPageAsHiRes(javacast("int", arguments.page), javacast("boolean", true)) />
  104.                     <cfelse>
  105.                         <cfset local.bufferedImage = local.pdfDecode.getPageAsHiRes(javacast("int", arguments.page)) />
  106.                     </cfif>
  107.                 </cfif>
  108.  
  109.                 <cfcatch></cfcatch>
  110.             </cftry>
  111.  
  112.  
  113.             <cfif not structKeyExists(local, "bufferedImage")>
  114.                 <cfif arguments.transparent>
  115.                     <cfset local.bufferedImage = local.pdfDecode.getPageAsTransparentImage(javacast("int", arguments.page)) />
  116.                 <cfelse>
  117.                     <cfset local.bufferedImage = local.pdfDecode.getPageAsImage(javacast("int", arguments.page)) />
  118.                 </cfif>
  119.             </cfif>
  120.  
  121.  
  122.             <cfset local.pdfDecode.closePdfFile() />
  123.  
  124.  
  125.             <cfcatch>
  126.                 <cfif local.pdfDecode.isOpen()>
  127.                     <cfset local.pdfDecode.closePdfFile() />
  128.                 </cfif>
  129.  
  130.                 <cfrethrow />
  131.             </cfcatch>
  132.         </cftry>
  133.  
  134.  
  135.         <!--- Extraction complete, we now have a buffered image to work with --->
  136.         <cfset local.pageImage = imageNew(local.bufferedImage) />
  137.         <cfset local.info = imageInfo(local.pageImage) />
  138.  
  139.  
  140.         <cfif arguments.width gt 0 and arguments.height gt 0 and (arguments.width lt local.info.width or arguments.height lt local.info.height)>
  141.             <cfset imageScaleToFit(local.pageImage, arguments.width, arguments.height, arguments.interpolation) />
  142.  
  143.         <cfelseif arguments.width gt 0 and arguments.width lt local.info.width>
  144.             <cfset imageScaleToFit(local.pageImage, arguments.width, "", arguments.interpolation) />
  145.  
  146.         <cfelseif arguments.height gt 0 and arguments.height lt local.info.height>
  147.             <cfset imageScaleToFit(local.pageImage, "", arguments.height, arguments.interpolation) />
  148.         </cfif>
  149.  
  150.  
  151.         <!--- Determine the object to be returned --->
  152.         <cfif len(arguments.destination)>
  153.             <cfif arguments.type eq "jpg">
  154.                 <cfset imageWrite(local.pageImage, local.filePath & local.fileName, arguments.quality) />
  155.             <cfelse>
  156.                 <cfset imageWrite(local.pageImage, local.filePath & local.fileName) />
  157.             </cfif>
  158.  
  159.             <cfset local.returnObject = local.filePath & local.filename />
  160.         <cfelse>
  161.             <cfset local.returnObject = local.pageImage />
  162.         </cfif>
  163.  
  164.  
  165.         <cfreturn local.pageImage />
  166.     </cffunction>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement