gpt4 book ai didi

coldfusion - cfdocument 防止在行中分页

转载 作者:行者123 更新时间:2023-12-04 21:20:51 25 4
gpt4 key购买 nike

我知道以前有人问过这个问题,但解决方案是 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 行数据。当然,这很容易破裂,所以如果有人知道解决方案的任何演变,我将不胜感激。

最佳答案

我相信我找到了一个伪解决方案。这基本上就是我在上面的评论中所说的。我做了一个最好的猜测,看看它是否适合使用 cfpdf 的 getInfo.totalPages 的值。如果合适,很好,将其合并到最终文档中,如果不合适,请再试一次少一行。

这样做的缺点是它会减慢速度,并且您不能使用 cfdocument 使之变得简单的某些东西,例如弄乱页眉和页脚。话虽如此,该解决方案的第 2 部分可能是记录适合页面中的行数,而不是合并页面并使用 cfdocument 和这些值作为循环约束再次重建整个文档,并在之后强制分页.事实上,下面的解决方案已经有点耗时,因此在 cfdocument 标签内再次构建它可能不适用于高流量站点。

错误解决方法:看起来 cfdocument 有一个错误,当使用 name 属性将文档保存到内存时,它会删除背景颜色。解决方法是将 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)#">

关于coldfusion - cfdocument 防止在行中分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12061281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com