动态检查文本是否转到下一页并使用 pdfmake 在 pdf 中添加分页符
Dynamic check if text Goes to next page and add pagebreak in pdf using pdfmake
对于一个项目,我正在 javascript 中使用 pdfmake 即时制作报价和发票 pdf。我面临的问题是文本块在中间离开页面。我想要的是检查某个文本块或 table 是否将在页面之间拆分,如果是,则在该块之前添加分页符以确保文本或 table 将完全打开一页。
我的 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 之前检查文本是否会离开页面并相应地添加分页符?
提前致谢。
找到解决方案:)
在 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;
},
有关此功能的更多信息以及所提供的信息,请查看 this
pageBreakBefore
函数在确定是否需要分页符方面提供了很大的灵活性。但是,我找到了另一种解决方案,它更直接且记录更少,但会自动完成所有魔术。这是 unbreakable: true
属性 0.1.32 release. Also, it mentioned in the following thread https://github.com/bpampuch/pdfmake/issues/1228#issuecomment-354411288
如何运作?
例如,您想使 header 和它下面的一些文本牢不可破。为此,您必须将 header 和内容包装在一个堆栈中,然后在其上应用 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 :)
}
pageBreakBefore: (currentNode, followingNodesOnPage, nodesOnNextPage, previousNodesOnPage) => {
if (currentNode.id === 'yournodeid') {
if(previousNodesOnPage[0].startPosition.pageNumber != followingNodesOnPage[0].pageNumbers) {
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'
]
}
对于一个项目,我正在 javascript 中使用 pdfmake 即时制作报价和发票 pdf。我面临的问题是文本块在中间离开页面。我想要的是检查某个文本块或 table 是否将在页面之间拆分,如果是,则在该块之前添加分页符以确保文本或 table 将完全打开一页。
我的 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 之前检查文本是否会离开页面并相应地添加分页符?
提前致谢。
找到解决方案:)
在 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;
},
有关此功能的更多信息以及所提供的信息,请查看 this
pageBreakBefore
函数在确定是否需要分页符方面提供了很大的灵活性。但是,我找到了另一种解决方案,它更直接且记录更少,但会自动完成所有魔术。这是 unbreakable: true
属性 0.1.32 release. Also, it mentioned in the following thread https://github.com/bpampuch/pdfmake/issues/1228#issuecomment-354411288
如何运作?
例如,您想使 header 和它下面的一些文本牢不可破。为此,您必须将 header 和内容包装在一个堆栈中,然后在其上应用 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 :)
}
pageBreakBefore: (currentNode, followingNodesOnPage, nodesOnNextPage, previousNodesOnPage) => {
if (currentNode.id === 'yournodeid') {
if(previousNodesOnPage[0].startPosition.pageNumber != followingNodesOnPage[0].pageNumbers) {
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'
]
}