We help IT Professionals succeed at work.

How to fit a PDF to page size in Coldfusion

monosyth
monosyth asked
on
I currently save web pages that are PDFs as flyers for my clients, but depending on the content they input they can run long, and it saves the PDF in coldfusion on two pages. I have to manually put the scale in as a variable in cfdocument. Is there a way it can automatically fit the page of a PDF, maybe it calculates the percentage or something?

Code I'm using now:

<cfdocument
   backgroundvisible = "yes"
   encryption = "none"
   marginbottom = "0"
   marginleft = "0"
   marginright = "0"
   margintop = "0"
   format = "pdf"
   fontembed = "yes"
   filename="C:\inetpub\myserver\pdftemp\#Username#_#pdfID#.pdf"
   overwrite="yes"
   scale="#myPDFscale#"
   mimetype = "text/xml"
   orientation = "portrait"
   pagetype = "#Userinfo.PDFpapersize#"
   unit = "in">
Comment
Watch Question

Most Valuable Expert 2015
Commented:
I don't think it's possible in a single step. But this thread suggests an interesting approach to the problem.  Basically generate the pdf. Then examine the file's properties. If the number of pages is more than one, reduce the scale by -5 and regenerate the pdf.  Loop/repeat until it fits within the desired number of pages.

From http://stackoverflow.com/questions/3689219/scale-pdf-to-single-page/3691240#3691240
<cfset pdfScale = 100 />
<cfset pdfObj = "" />
<cfdocument format="pdf" name="pdfObj" scale="#pdfScale#">document contents</cfdocument>
<cfpdf action="getInfo" source="#pdfObj#" name="pdfInfo" />
<cfloop condition = "pdfInfo.TotalPages gt 1">
    <cfset pdfScale -= 5 />
    <cfdocument format="pdf" name="pdfObj" scale="#pdfScale#">document contents</cfdocument>
    <cfpdf action="getInfo" source="#pdfObj#" name="pdfInfo" />
</cfloop>

Author

Commented:
Hmm, that looks like it would work, but when tested it throws an error, and even when I just use that example. Error is: ByteArray objects cannot be converted to strings.

Any help?
Most Valuable Expert 2015
Commented:
Looks like there's a typo in the example.  Source should be a variable name, without the pound signs

ie This <cfpdf action="getInfo" source="pdfObj" name="pdfInfo" />
Not this: <cfpdf action="getInfo" source="#pdfObj#" name="pdfInfo" />