Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!---
- Adapted from Dan's Blog
- http://blog.pengoworks.com/index.cfm/2008/4/8/UDF-for-converting-a-PDF-page-to-Images-using-CF8-and-Java
- --->
- <cffunction name="convertPDFPageToImage" access="public" returntype="any" output="false"
- hint="Creates a thumbnail from a PDF file. Returns image path or Coldfusion image based on if destination was set.">
- <cfargument name="file" type="any" required="true" hint="Path of source PDF file or a byte array." />
- <cfargument name="page" type="numeric" required="true" default="1" hint="Page to convert to a thumbnail." />
- <cfargument name="destination" type="string" default="" hint="Path to save image file. It may be a directory or contain the file name." />
- <cfargument name="type" type="string" default="png" hint="The type of image to write. If destination includes file, the file extension overrides this." />
- <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." />
- <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." />
- <cfargument name="highResRender" type="boolean" default="true" hint="Whether or not to use high quality rendering." />
- <cfargument name="highResImage" type="boolean" default="false" hint="Whether or not to extract a high quality image." />
- <cfargument name="transparent" type="boolean" default="false" hint="Should the image contain transparencies?" />
- <cfargument name="interpolation" type="string" default="highestQuality" hint="Specify the interpolation type when scaling the image." />
- <cfargument name="quality" type="numeric" default="0.85" hint="The JPG quality (if writing to JPG.)" />
- <cfargument name="password" type="string" hint="Password to unlock PDF file." />
- <cfif not isArray(arguments.file) and not (isSimpleValue(arguments.file) and fileExists(arguments.file))>
- <cfthrow message="Invalid path to file or binary for PDF not present." />
- </cfif>
- <!--- Validate image type --->
- <cfset arguments.type = lcase(arguments.type) />
- <cfset local.typesList = "gif,jpg,png,tif" />
- <cfif not find(arguments.type, local.typesList)>
- <cfset arguments.type = "png" />
- </cfif>
- <!--- Destination set, file will be saved --->
- <cfif len(arguments.destination)>
- <!--- Validate destination and thumbnail name --->
- <!--- Path appears to contain a file --->
- <cfif len(listLast(arguments.destination, ".")) eq 3>
- <cfset local.fileName = getFileFromPath(arguments.destination) />
- <cfset local.filePath = getDirectoryFromPath(arguments.destination) />
- <cfset local.extension = lcase(right(local.fileName, 3)) />
- <cfif not find(local.extension, local.typesList)>
- <cfset local.extension = arguments.type />
- <cfelse>
- <cfset arguments.type = local.extension />
- </cfif>
- <cfset local.fileName = left(local.fileName, len(local.fileName) - 3) & local.extension />
- <!--- Path has no file --->
- <cfelse>
- <cfset local.fileName = reReplaceNoCase(getFileFromPath(arguments.filePath), ".pdf$", "." & arguments.type) />
- <cfset local.filePath = arguments.destination />
- </cfif>
- <!--- At this point, filePath should not contain the fileName --->
- <cfif right(local.filePath, 1) neq "/" and right(local.filePath, 1) neq "\">
- <cfset local.filePath &= "/" />
- </cfif>
- <!--- Verify destination exists --->
- <cfif not directoryExists(local.filePath)>
- <cfset directoryCreate(local.filePath) />
- </cfif>
- </cfif>
- <!--- Validate page number --->
- <cfif arguments.page lt 1>
- <cfset arguments.page = 1 />
- </cfif>
- <cfset local.pdfDecode = createObject("java", "org.jpedal.PdfDecoder").init(javacast("boolean", true)) />
- <cftry>
- <!--- <cfset local.pdfDecode.showAnnotations = javacast("boolean", false) /> --->
- <cfset local.pdfDecode.useHiResScreenDisplay(javacast("boolean", arguments.highResRender)) />
- <cfset local.pdfDecode.setExtractionMode(javaCast("int", 0)) />
- <cfif isArray(arguments.file)>
- <cfset local.pdfDecode.openPdfArray(arguments.file) />
- <cfelse>
- <cfset local.pdfDecode.openPdfFile(javacast("string", arguments.file)) />
- </cfif>
- <cfif structKeyExists(arguments, "password") and len(arguments.password)>
- <cfset local.pdfDecode.setEncryptionPassword(javacast("string", arguments.password)) />
- </cfif>
- <!--- The version of pdfDecode on Lucee 5 does not support getting the page as high res. --->
- <cftry>
- <cfif arguments.highResImage>
- <cfif arguments.transparent>
- <cfset local.bufferedImage = local.pdfDecode.getPageAsHiRes(javacast("int", arguments.page), javacast("boolean", true)) />
- <cfelse>
- <cfset local.bufferedImage = local.pdfDecode.getPageAsHiRes(javacast("int", arguments.page)) />
- </cfif>
- </cfif>
- <cfcatch></cfcatch>
- </cftry>
- <cfif not structKeyExists(local, "bufferedImage")>
- <cfif arguments.transparent>
- <cfset local.bufferedImage = local.pdfDecode.getPageAsTransparentImage(javacast("int", arguments.page)) />
- <cfelse>
- <cfset local.bufferedImage = local.pdfDecode.getPageAsImage(javacast("int", arguments.page)) />
- </cfif>
- </cfif>
- <cfset local.pdfDecode.closePdfFile() />
- <cfcatch>
- <cfif local.pdfDecode.isOpen()>
- <cfset local.pdfDecode.closePdfFile() />
- </cfif>
- <cfrethrow />
- </cfcatch>
- </cftry>
- <!--- Extraction complete, we now have a buffered image to work with --->
- <cfset local.pageImage = imageNew(local.bufferedImage) />
- <cfset local.info = imageInfo(local.pageImage) />
- <cfif arguments.width gt 0 and arguments.height gt 0 and (arguments.width lt local.info.width or arguments.height lt local.info.height)>
- <cfset imageScaleToFit(local.pageImage, arguments.width, arguments.height, arguments.interpolation) />
- <cfelseif arguments.width gt 0 and arguments.width lt local.info.width>
- <cfset imageScaleToFit(local.pageImage, arguments.width, "", arguments.interpolation) />
- <cfelseif arguments.height gt 0 and arguments.height lt local.info.height>
- <cfset imageScaleToFit(local.pageImage, "", arguments.height, arguments.interpolation) />
- </cfif>
- <!--- Determine the object to be returned --->
- <cfif len(arguments.destination)>
- <cfif arguments.type eq "jpg">
- <cfset imageWrite(local.pageImage, local.filePath & local.fileName, arguments.quality) />
- <cfelse>
- <cfset imageWrite(local.pageImage, local.filePath & local.fileName) />
- </cfif>
- <cfset local.returnObject = local.filePath & local.filename />
- <cfelse>
- <cfset local.returnObject = local.pageImage />
- </cfif>
- <cfreturn local.pageImage />
- </cffunction>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement