我不确定你的 CSS 是如何组织的,但我遇到了类似的情况,我需要在屏幕上打印各种内容。我的解决方案是在 Javascript 中对 CFC 使用 AJAX 调用 (CFAJAXPROXY),以便使用 DIV 内容设置 CF 会话变量,然后调用我的打印页面,该页面将使用 cfhtmltopdf 标签输出内容。我的经验是 CSS 必须包含在您使用的任何 PDF 生成标签中。
例如:
<div id="myContent">content on my page to print</div>
还有一个打印按钮
<button onclick="printContent('myContent')">Print</button>
onclick 在 Javascript 中调用如下:
var printContent = function(_contentDiv){
var _content = $('#' + _contentDiv).html();
var _setContent = _cfc.setContent(_content);
window.location.href = 'printPage.cfm';
}
“_cfc”在 cfajaxproxy 调用中定义。
将被调用的 CF 函数如下所示:
<cfcomponent>
<cffunction access="remote" name="setContent" returntype="string">
<cfargument name="_content" required="yes" type="string">
<cfset session.content = _content>
<cfreturn "whateveryouwant">
</cffunction>
</cfcomponent>
而 printPage.cfm 可能看起来像这样(我使用 Bootstrap 作为我的 CSS):
<cfhtmltopdf>
<link href="css/bootstrap.css" rel="Stylesheet" type="text/css"></link>
<cfoutput>#session.content#</cfoutput>
</cfhtmltopdf>
这显然是一个简单的解决方案,但它确实有效。我想如果您需要不同的 CSS 页面,您也可以将链接标签中的 HREF 值设置为会话参数