2012-02-15 88 views
1

我需要一种从当前打开的Coldfusion文档动态生成pdf的方法。但我不知道如何处理这个问题,因为我没有找到一种方法,即将当前(html)文本传递给使用cfdocument标记生成pdf的函数。从当前文档生成pdf

任何提示或想法,如何解决这个问题?

+0

你是否在使用任何特定的框架(Fusebox,车轮上的CF等等)? – 2012-02-15 17:14:10

回答

1

您可以使用cfdocument的“src”属性通过内部自引用请求请求当前页面,而不是捕获当前响应正文。

内的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> 

这将寻找名为 “showAsPDF” URL参数的存在。当它被定义并设置为“true”时,则此代码将在内部接管并运行相同的请求,并通过调用cfdocument进行路由。该响应将作为PDF文档输出。

1

您可以通过使用cfpdf

生成基本的HTML PDF文档,你甚至可以指定你的cfdocument作为源cfpdf:

<cfpdf action="write" source="someCfDocument" destination="myBook1.pdf" overwrite="yes"> 

例无cfdocument:

<cfpdf action="write" destination="myBook1.pdf" overwrite="yes"> 
    <p>My dynamic html goes here</p> 
</cfpdf> 
3

我之前做过这样的事情:

<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>