I have an application for customer and product management that has many screens/pages. I would like certain pages to be printable and exportable to PDF.
To print a document, its a simple case of providing a CSS stylesheet for @Print media which strips a lot of the regular styling. However, when generating the PDF I noticed the <cfdocument>
and <cfhtmltopdf>
tags both want the an entire HTML page to render (including <head>
tags).
This means that it doesn't recognise the purpose of the PDF as being for print and doesn't pick up the @Print styles. Currently I'm having to make a seperate makepdf.cfm
page and repeat the HTML of the page I want to export along with including ONLY the print CSS stylesheet.
This seems a bit crazy because I'll have to update the makepdf.cfm
page each and everytime I change a main application screen that can be printed/exported to pdf.
Is there a better way to achieving what I'm trying to do?
I am not sure how your CSS is organized, but I had a similar situation where I needed to print various content off the screen. My solution was to use an AJAX call (CFAJAXPROXY) in Javascript to a CFC in order to set a CF session variable with the DIV content and then call my print page which will output the content with the cfhtmltopdf tag. My experience has been that the CSS must be included in within whichever PDF generation tag you are using.
For example:
And a button to print
The onclick calls the following in the Javascript:
The "_cfc" is defined in a cfajaxproxy call.
The CF function that's going to get called looks something like the following:
And the printPage.cfm might look like this (I use Bootstrap as my CSS):
This is obviously a bare bones solution, but it works. I would imagine that if you needed different CSS pages, you could set the HREF value in the link tag as a session parameter as well