动态检查文本是否转到下一页,并使用pdfmake在pdf中添加分页符
Dynamic check if text Goes to next page and add pagebreak in pdf using pdfmake
对于一个项目,我正在使用javascript中的pdfmake动态提供报价和发票pdf。我面临的问题是让文本块离开页面在中间。我想要的是检查某个文本块或表格是否要在页面之间分割,如果是,请在该块之前添加分页符,以确保文本或表格完全在一页上。
我的pdf文档定义是这样构建的:
return {
content: [
getOfferLogo(), //Get the logo or empty string
getHeading(), //get the customer and business data (adress etc)
//the above is always the same
getText(), //get the textblock, created by user and always different
getSpecifics(), //get a table of payment specifications
getSignature() //get last textblock contaning signature fields etc, always the same
],
styles: {
subheader: {
fontSize: 15,
bold: true,
alignment: 'center'
}
},
defaultStyle: {
columnGap: 20,
fontSize: 12
}
};
简而言之,在创建pdf之前,我如何检查文本是否会从页面上消失,并相应地添加分页符?
提前谢谢。
pageBreakBefore
函数在确定是否需要分页时提供了很大的灵活性。然而,我又找到了一个解决方案,它更简单,文档更少,但能自动实现所有功能。它是0.13.2版本中的unbreakable: true
属性。此外,它在以下线程中提到https://github.com/bpampuch/pdfmake/issues/1228#issuecomment-354411288
它是如何工作的例如,您希望使页眉及其下方的某些文本牢不可破。为了做到这一点,您必须将标头和内容包装在堆栈中,并在其上应用unbreakable: true
{
stack: [
// header
{
text: 'Lorem ipsum dolor sit amet',
bold: true,
fontSize: 13
},
// content
{
text: 'Nulla iaculis magna vitae luctus euismod. Sed arcu risus, mattis non molestie et, condimentum sit amet justo. Quisque vitae neque magna. Etiam in tellus vitae arcu volutpat bibendum. In ullamcorper ante tortor, a viverra libero cursus eu. Phasellus quis massa nec lorem feugiat ultricies. Aliquam erat volutpat. Nullam a purus tempus, feugiat elit vel, tincidunt tortor.'
}
],
unbreakable: true // that's the magic :)
}
找到解决方案:)
在DocDefinition中,您可以为pageBreakBefore添加一个函数,如下所示:
content: [{
text: getOfferClosingParagraph(),
id: 'closingParagraph'
}, {
text: getSignature(),
id: 'signature'
}],
pageBreakBefore: function(currentNode, followingNodesOnPage, nodesOnNextPage, previousNodesOnPage) {
//check if signature part is completely on the last page, add pagebreak if not
if (currentNode.id === 'signature' && (currentNode.pageNumbers.length != 1 || currentNode.pageNumbers[0] != currentNode.pages)) {
return true;
}
//check if last paragraph is entirely on a single page, add pagebreak if not
else if (currentNode.id === 'closingParagraph' && currentNode.pageNumbers.length != 1) {
return true;
}
return false;
},
有关此功能和提供的信息的更多信息,请查看此
文本的简单示例:
var dd = {
content: [
'First paragraph',
// page break before text
{text: 'Text on the next page', pageBreak: 'before'},
'Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines',
// page break after text
{text: 'Text is lastest on the page', pageBreak: 'after'},
'Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines'
]
}
pageBreakBefore: (currentNode, followingNodesOnPage, nodesOnNextPage, previousNodesOnPage) => {
if (currentNode.id === 'yournodeid') {
if(previousNodesOnPage[0].startPosition.pageNumber != followingNodesOnPage[0].pageNumbers) {
return true;
}
}
return false;
}
相关文章:
- 如何使用javascript或html下载PDF格式的填写表单
- 如何将PDF作为二进制文件传递到window.open()
- javascript.点击显示嵌入的pdf
- Javascript/Jquery Blob not showing Chrome PDF
- 在提交时打开thankyou.html+下载PDF
- 生成pdf或其他非html文件时的错误处理
- 在pdfmake中嵌入背景图像
- how to convert html <div> to pdf
- 使用pdfmake在angularjs中生成pdf时未显示图像
- 是否可以使用 pdfmake.js 在 pdf 文件中重复表格行
- 如何使用pdfmake将html表格转换为pdf
- “未捕获的类型错误:无法读取未定义的属性'嵌入'”在 pdfMake pdf 引擎中使用自定义图像时
- 第三行作为pdfMAke pdf创建器引擎中的表头
- 使用 pdfmake - 在 AngularJS 中生成 pdf 工具
- 如何在pdfmake中获取完整的网页pdf
- pdfmake:如何创建具有不同方向的多页pdf
- 使用Angularjs中的pdfMake从HTML生成PDF
- 使用pdfmake将文本固定到pdf的最后一页底部
- 动态检查文本是否转到下一页,并使用pdfmake在pdf中添加分页符
- 使用 pdfmake 生成 pdf 时出现类型错误