我知道之前已经问过这个问题但解决方案是在2.5多年前提出来的,所以我问是否有人设计或知道使用CF9更优雅的问题解决方案。任何人都可以确认CF10是否支持“page-break-inside:avoid”规则?
How can I prevent page-break in CFDocument from occuring in middle of content?
COLDFUSION: cfdocument and forcing a pagebreak
这就是我正在做的事情。我估计,根据它的页面类型,我可以在强制分页之前容纳9或11行数据。当然,这很容易破裂,所以如果有人知道解决方案的任何演变,我将不胜感激。
答案 0 :(得分:2)
我相信我找到了一个伪解决方案。这基本上就是我在上面的评论中所说的。我做了最好的猜测,看看它是否适合使用cfpdf的getInfo.totalPages的值。如果它很合适,很好,将它合并到最终文档中,如果不合适,请再少尝试一行。
这样做的缺点是它减慢了一点,你不能使用一些cfdocument很容易就像搞乱页眉和页脚一样。话虽这么说,这个解决方案的第2部分可能是记录数组中页面上的行数而不是合并页面并使用cfdocument再次重建整个文档,并将这些值作为循环约束强制分页后。实际上,下面的解决方案已经耗费一些时间,因此在cfdocument标记内再次构建它可能无法在高流量站点中运行。
错误解决方法:使用name属性将文档保存到内存时,cfdocument的错误似乎会删除背景颜色。解决方法是将cfdocument标记删除到外部文件。我看到一个程序员将它放入cfc中,我发现可以使用简单的cfinclude。
我希望有人觉得这很有帮助,如果你知道更好的方法,请发表评论。
<cfset reviewText = "Lorem ipsum dolor sit amet, + lots of characters.">
<cfset estimatedRowsPerPage = 7> <!--- This is the max number of records you want to try on each page. The larger the gap between max and actual will slow down the process. Used to reset attemptedRowsPerPage if the value changes --->
<cfset attemptedRowsPerPage = estimatedRowsPerPage> <!---- number of rows attempted to add to the page --->
<cfset totalRowsOutput = 0><!--- this is the number of records successfully saved to the final PDF --->
<cfset recordCount = 20> <!--- this is the query's record count --->
<!--- cfpdf cannot create a file from scratch and cfdocument requires some content so a container object cannot be created without at least one page. This page will be deleted later --->
<cfdocument format="pdf" marginbottom=".25" margintop=".25" marginleft=".25" marginright=".25" name = "finalDocument">Delete me</cfdocument>
<cfloop condition="totalRowsOutput lt recordCount">
<!--- create what *should* be a single page document --->
<cfdocument format="pdf" marginbottom=".25" margintop=".25" marginleft=".25" marginright=".25" name = "testDocument">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>A title</title>
</head>
<body>
<table border="1">
<tr>
<td>Row:</td>
<td>Title:</td>
<td>Author:</td>
<td>Price:</td>
<td>Average Rating:</td>
<td>Reviews:</td>
</tr>
<cfoutput>
<cfloop from = "1" to = "#attemptedRowsPerPage#" index = "i">
<tr>
<td>
#i#
</td>
<td nowrap="nowrap">
#mid(reviewText,1,randRange(4,10))#
</td>
<td nowrap="nowrap">
#mid(reviewText,20,randRange(8,20))#
</td>
<td>
$10.00
</td>
<td>
#randRange(1,5)#
</td>
<td>
#mid(reviewText,1,randRange(10,700))#
</td>
</tr>
</cfloop>
</cfoutput>
</table>
</body>
</html>
</cfdocument>
<!--- get the document info to see if the page count = 1 --->
<cfpdf action="getinfo" source="testDocument" name="testInfo">
<cfif testInfo.totalPages gt 1>
<!--- if the page count is greater than 1 we need to try again with one less record. --->
<cfset attemptedRowsPerPage -= 1>
<cfelse>
<!--- merge the new single page to the final document --->
<cfpdf action = "merge" name = "finalDocument">
<cfpdfparam source="finalDocument">
<cfpdfparam source="testDocument">
</cfpdf>
<cfset totalRowsOutput += attemptedRowsPerPage>
<!--- if the page count = 1, we need to increment the startAttempt and reset the attemptedRowsPerPage unless attemptedRowsPerPage = recordCount --->
<cfif totalRowsOutput lt recordCount>
<!--- don't try to output more than exist --->
<cfset attemptedRowsPerPage = estimatedRowsPerPage+totalRowsOutput lt recordCount ? estimatedRowsPerPage : recordCount-totalRowsOutput>
</cfif>
</cfif>
</cfloop>
<!--- delete the manditory page needed to create our final document --->
<cfpdf action="deletePages" pages="1" source="finalDocument" name="finalDocument">
<!--- see "http://www.raymondcamden.com/index.cfm/2007/7/12/ColdFusion-8-Working-with-PDFs--A-Problem" to see why you need toBinary --->
<cfcontent type="application/pdf" variable="#toBinary(finalDocument)#">
答案 1 :(得分:0)
特拉维斯 - 我不知道另一种方法。通常我创建一个HTML表作为每个“页面”的核心,并在关闭表之前具有特定的行数,打破页面并打开一个新表。