froadie
froadie

Reputation: 83053

How to output a pdf variable?

I have a cfdocument tag that looks something like this:

<cfdocument name="myPDF" format="PDF" ... >
   ...
</cfdocument>

I save the PDF in a variable using the name attribute, as I'm going to be using it in multiple places for multiple uses. (Attach to an email, display on the screen, etc.)

I want to display this pdf in the browser, just output it as if I had left out the name attribute in the cfdocument, but I can't figure out how to do this. Using a cfoutput ( <cfoutput>#myPDF#</cfoutput> ) gives me an error - "ByteArray objects cannot be converted to strings." Using a cfdump gives me a binary object. It seems the pdf is stored as binary... How can I force it to display on the screen as a pdf?

Upvotes: 2

Views: 5303

Answers (2)

Mike Causer
Mike Causer

Reputation: 8314

As nice as ColdFusion's PDF support is, we couldn't get the generated file sizes down to something reasonable, which is why we implemented wkhtmltopdf.

http://wkhtmltopdf.org/

eg. 1mb CF PDFs vs 20kb wkhtmltopdf PDFs. (wtf??) When you have a site like ours, with 1,000s of pdfs (invoices), 1mb is just unacceptable.

wkhtmltopdf is awesome. It converts html pages to pdfs using webkit. Yes, you can have embeded fonts, transparent images, css and javascript in your html! Embedding Google web fonts doesn't blow out the file size either, like + 2-4kb, and they look sexy.

You simply craft a html page and use this external binary to convert the html to a pdf. In your cf script, <cfexecute> it, then fileRead() the contents.

You'd think Acrobat, being and Adobe product, and ColdFusion, also an Adobe product, would produce the best PDFs... but no

Upvotes: 0

froadie
froadie

Reputation: 83053

Found a solution (by looking elsewhere on our site where this was done) - used a cfcontent:

<cfcontent type="application/pdf">
<cfoutput>#tostring(myPDF)#</cfoutput>

Update:

I actually discovered a slightly more straightforward way to do this:

<cfcontent type="application/pdf" variable="#myPDF#">

Upvotes: 8

Related Questions