【问题标题】:Generate pdf from current document从当前文档生成 pdf
【发布时间】:2012-03-06 23:18:20
【问题描述】:

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

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

【问题讨论】:

  • 您是否使用任何特定框架(Fusebox、CF on Wheels 等...)?

标签: coldfusion pdf-generation


【解决方案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 文档输出。

【讨论】:

    【解决方案2】:

    您可以使用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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-09
        • 2011-08-29
        • 1970-01-01
        • 2011-03-14
        • 1970-01-01
        • 1970-01-01
        • 2011-02-14
        • 2020-08-09
        相关资源
        最近更新 更多