Unit Testing PDF Page Size in ColdFusion

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.

One thought on “Unit Testing PDF Page Size in ColdFusion

Leave a Reply to Terrence Ryan Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s