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 —>
<cfset assertTRUE(validatePaperSize(width=pInfo.width, height=pInfo.height, pageType=‘legal’))/>
var ratios = structNew() />
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 —>
Abs(ratioToTest – ratios[arguments.pageType]) lt .005
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”
@Duncan thanks for the tips. I’m going to give the Itext one a try, and have already corrected the spacing issue.