问题
I need a way to dynamicly generate a pdf from the currently opened coldfusion document. But I don't have an idea how to handle this because I didn't find a way i.e. to pass the current (html) text to a function which generates the pdf by using the cfdocument tag.
Any hints or ideas, how to solve this problem?
回答1:
Rather than capture the current response body, you could use the "src" attribute of cfdocument to request the current page via an internal self-referencing request.
within Application.cfc:
<cffunction name="onRequestStart">
<cfif IsDefined("url.showAsPDF") AND
url.showAsPDF IS "true" AND
cgi.http_user_agent IS NOT "ColdFusion">
<cfset myURL =
"http" &
(IsDefined('CGI.HTTPS') AND CGI.HTTPS IS "On") ? "s" : "") &
"://#cgi.server_name#:#cgi.SERVER_PORT##cgi.script_name#?#cgi.query_string#">
<cfdocument src="#myURL#" format="PDF"></cfdocument><cfabort>
</cfif>
</cffunction>
This will look for the presence of a URL parameter named "showAsPDF". When it is defined and set to "true", then this code will take over and run the same request internally, routed through a call to cfdocument. The response will then be output as a PDF document.
回答2:
I did something like this before:
<cfsavecontent variable="pdf">
<table>
...lots of html and CF code ...
</table>
</cfsavecontent>
<cfdocument format="PDF" encryption="NONE">
<cfdocumentsection>
<cfoutput>#pdf#</cfoutput>
<cfdocumentitem type="footer">
<cfoutput>
#cfdocument.currentpagenumber# of #cfdocument.totalpagecount#
</cfoutput>
</cfdocumentitem>
</cfdocumentsection>
</cfdocument>
回答3:
You can generate a pdf from basic html by using cfpdf
You can even specify your cfdocument as source for cfpdf:
<cfpdf action="write" source="someCfDocument" destination="myBook1.pdf" overwrite="yes">
Example without a cfdocument:
<cfpdf action="write" destination="myBook1.pdf" overwrite="yes">
<p>My dynamic html goes here</p>
</cfpdf>
来源:https://stackoverflow.com/questions/9296371/generate-pdf-from-current-document