Aarrgghh!!

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.


July 15, 2008 Posted by Terrence Ryan at 2:43 PM

ColdFusion, Web Development,



Comments

You might want to revert to the iText code to get the page size, here's an example: http://cfsearching.blogspot.com/2007/12/getting-started-with-itext-part-13.html http://www.1t3xt.info/api/com/lowagie/text/pdf/PdfReader.html#getPageSize(int)

PS: replace  with a non breaking space


Posted by: duncan at July 16, 2008 4:19 AM

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




Posted by: Terrence Ryan at July 22, 2008 1:38 PM

It worked like a charm :)


Posted by: Free iMac at May 17, 2009 6:18 PM

Posted by Who at September 2, 2010 3:10 PM

Post a comment











Remember personal info?