• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1573
  • Last Modified:

How to fit a PDF to page size in Coldfusion

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:

   backgroundvisible = "yes"
   encryption = "none"
   marginbottom = "0"
   marginleft = "0"
   marginright = "0"
   margintop = "0"
   format = "pdf"
   fontembed = "yes"
   mimetype = "text/xml"
   orientation = "portrait"
   pagetype = "#Userinfo.PDFpapersize#"
   unit = "in">
  • 2
2 Solutions
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" />
monosythAuthor 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?
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" />
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now