[ACCEPTED]-How do I turn a ColdFusion page into a PDF download?-coldfusion
You should use the cfdocument tag (with 15 format="PDF") to generate the PDF by placing 14 it around the page you are generating. You'll 13 want to specify a filename attribute, otherwise 12 the document will just stream right to your 11 browser.
After you have saved the content 10 as a PDF, use cfheader and cfcontent in 9 combination to output the PDF as an attachment 8 ("Save As") and add the file to the response 7 stream. I also added deletefile="Yes" on 6 the cfcontent tag to keep the file system 5 clean of the files.
<cfdocument format="PDF" filename="file.pdf" overwrite="Yes"> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Hello World</title> </head> <body> Hello World </body> </html> </cfdocument> <cfheader name="Content-Disposition" value="attachment;filename=file.pdf"> <cfcontent type="application/octet-stream" file="#expandPath('.')#\file.pdf" deletefile="Yes">
As an aside: I'm just 4 using file.pdf for the filename in the example 3 below, but you might want to use some random 2 or session generated string for the filename 1 to avoid problems resulting from race conditions.
If you want to avoid storing the PDF at 18 all, using cfdocument without a filename 17 will send the pdf (of flashpaper) directly 16 to the browser without using the cfheader 15 and cfcontent.
Caveat: Like with using cfheader/cfcontent, you 14 need to do this before the cache gets flushed 13 to the browser, since it's basically doing 12 the same thing without having to store the 11 file.
To get the content, I would probably 10 use cfsavecontent wrapped around the same 9 calls/includes/etc. that generate the page, with 8 two major exceptions. cfdocument seems 7 to have issues with external stylesheets, so 6 using an include to put the styles directly 5 into the document is probably a good idea. You 4 can try using an @import instead -- it works 3 for some people. Also, I'd be careful about 2 relative links to images, as they can sometimes 1 break.
<cfdocument> approach is the sanctioned way to get 29 it done, however it does not offer everything 28 possible in the way of manipulating existing 27 PDF documents. I had a project where I 26 needed to generate coupons based using a 25 pre-designed, print-resolution PDF template.
<cfdocument> would 24 have let me approximate the output, but 23 only with bitmap images embedded in HTML. True, I 22 could fake print-resolution by making a 21 large image and scaling it in HTML, but 20 the original was a nice, clean, vector-image 19 file and I wanted to use that instead.
I 18 ended up using a copy of
<cfx_pdf> to get the job 17 done. (Developer's Site, CF Tag Store) It's a CF wrapper around a 16 Java PDF library that lets you manipulate 15 existing PDF documents, including filling 14 out PDF forms, setting permissions, merging 13 files, drawing vector graphics, tables, and 12 text, use custom fonts, etc, etc. If you 11 are willing to work with it, you can get 10 some pretty spectacular results.
The one 9 drawback is that it appears the developer 8 has left this product out to pasture for 7 a long time. The developer site is still 6 copyright 2003 and doesn't mention anything 5 past ColdFusion MX 6.1. I ended up having 4 to break some of the encrypted templates 3 in order to fix a couple of bugs and make 2 it work as I needed it to. Nontheless, it 1 is a powerful tool.
I'm not that familiar with ColdFusion, but 5 what you need to do is set the Content-Type 4 of the page when the user requests it to 3 be application/octet-stream. This will prompt 2 them for a download every time.
Hope this 1 helps!
More Related questions