【发布时间】:2017-04-11 05:04:20
【问题描述】:
我有一个连续的 HTML 页面,可以根据用户输入进入任何高度。当用户完成插入内容后,我会生成 HTML 文档的 PDF。对于 HTML 到 PDF 的对话,我使用的是 wkhtmltopdf。
整个 html 由小部分组成。我希望当一个新部分即将开始并且控件几乎位于页面末尾时,该部分应该从下一页开始,而不是从第 1 页的一部分开始,并保留在第 2 页。
这是我的相关代码:
HML:
<div id="page-content">
<div class="section-item></div><!---This is of variable height based on content-->
<div class="section-item></div><!---This is of variable height based on content-->
<div class="section-item></div><!---This is of variable height based on content-->
<div class="section-item></div><!---This is of variable height based on content-->
</div>
JavaScript:
$(window).load(function() {
var pageSize = 1300;
var currrentPageSpace = pageSize;
$(".section-item").each(function() {
var sectionPosition = $(this).position().top;
var sectionHeight = $(this).outerHeight(true); //Height, including the padding and margin
var availabePageSpace = currrentPageSpace - sectionPosition;
if (sectionHeight < availablePageSpace) {
$(this).css("margin-top", availabePageSpace+"px"); //Give this section margin top equal to amount of remaining page space so that this section is shown on next page
currentPageSpace += pageSize; //Now we are on new page and its available space is adjusted
}
});
});
但此代码具有基于页面内容的随机行为。对于某些内容,它可以完美运行。有时它根本不会增加边距。有时部分项目会转到下一页并在顶部添加了很多边距。
在生成 PDF 时,是否有其他方法可以实现连续 HTML 的分页符? 任何帮助或意见将不胜感激。
【问题讨论】:
标签: javascript html css pdf-generation wkhtmltopdf