TerrenceRyan.com

I'm a 35 year old redhead geek from Philly.
I'm currently a Developer Evangelist for Adobe.
Also the author of Driving Technical Change

Unit Testing PDF Page Size in ColdFusion

1 Comment

I ran into a difficult Unit testing problem the other day. I had trouble determining the correct test to write, and finally did so. I thought that perhaps someone else might benefit from an exploration of testing strategy.

As you might know, we create pdf's using ColdFusion for ExportReports.com. One of the new features we are adding is the ability to set the page size (legal, letter, A4… etc.) As page size is a feature, I need to test it. So I need to create a pdf file then determine what its page size is. At first I explored <cfpdf> and its ability to get at PDF metadata. No joy - it turned out that page size isn't one of the things returned in the metadata. Then I looked at <cfpdf>'s ability to get thumbnails of the pages in the pdf. I could use <cfimage> to get the information about height and width of the image files, put that was in pixels and hard to translate to inches. (I tried assuming 300dpi, it did not yield good results.) Then it dawned on me that ratio of height to width could yield helpful answers. I found the dimensions in millimeters for the various page types, created ratios from them, and compared them to the ratio of the dimensions of the thumbnails. Then I had to correct a bit for precision, and it yielded me a solution. Here's what it looked like in code:

<!--- Grab a processable view of the pdf --->
<cfpdf action="thumbnail"
       source="#pdfQuery.directory#/#pdfQuery.name#"
      
destination="#dirreports#tn"
      
scale="100" overwrite="TRUE"/>

<cfdirectory action="list"
             name="thumbnailQuery"
            
directory="#dirreports#tn"
            
filter="*.jpg" />

<cfimage action="info"
         source="#thumbnailQuery.directory#/#thumbnailQuery.name#"
        
structName="pInfo">

<cfset assertTRUE(validatePaperSize(width=pInfo.width, height=pInfo.height, pageType='legal'))/>

<cffunction name="validatePaperSize" access="private" output="false" returntype="boolean" >
     <cfargument
name="width" type="string" required="yes" />
     <cfargument
name="height" type="string" required="yes" />
     <cfargument
name="pageType" type="string" required="yes" />

     <cfset var ratios = structNew() />
     <cfset
var ratioToTest = NumberFormat(height/width, "_.___") />

<!--- These ratios were derived by using sizes in millimeters
listed here: http://en.wikipedia.org/wiki/Paper_size --->

     <cfset ratios['letter'] = NumberFormat(279/216, "_.___") />
     <cfset ratios['legal'] = NumberFormat(356/216, "_.___") />
     <cfset ratios['A4'] = NumberFormat(297/210, "_.___") />
     <cfset ratios['A5'] = NumberFormat(210/148, "_.___") />
     <cfset ratios['B4'] = NumberFormat(353/250, "_.___") />
     <cfset ratios['B5'] = NumberFormat(250/176, "_.___") />

     <!--- Fudge precision --->
    
<cfif Abs(ratioToTest - ratios[arguments.pageType]) lt .005 >
    
     <cfreturn true />
    
<cfelse>
    
     <cfreturn false />
    
</cfif>    

</cffunction>

So it might not be perfect, but it works for me. Of course, I could just file a feature request with Adobe to add page size to PDF metadata.

1 response so far ↓

  • 1 Terrence Ryan

    @Duncan thanks for the tips. I'm going to give the Itext one a try, and have already corrected the spacing issue.

Leave a Comment









Categories

Monthly Archives

Tag Cloud

coldfusion web development flex coldfusion builder appearances squidhead coldfusion builder extensions higher ed flash builder air mobile android adobe apptacular html5 driving technical change running a coldfusion shop adobemax06 movable type flash catalyst flash blackberry adobemax07 adobemax08 hero finicky css adobemax09 holy crap i’m a mobile developer centaur basecamp cfc unfuddle motorola metablog irrational characters ios git evangelism devices code reviews ant wharton subversion security phonegap philly philadelphia multidevice knowledge@wharton jobs browserlab adobemax10 adobe tv unfuddlecfc svnauth.cfc semantic html semantic html responsive web design qnx nlb linux jquery mobile java it github flexorg fireworks edge eclipse dreamweaver apps apple adobemax11