对于一个项目,im提供报价和发票pdf的动态使用pdfmake在javascript。我面临的问题是在中间有文本块离开页面。我想要的是检查某个文本块或表是否要在页面之间分割,如果是的话,在块之前添加一个分页,以确保文本或表将完全放在一个页面上。
我的pdf docDefinition是这样构建的:
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之前,我如何检查文本是否会离开页面,并相应地添加分页?
提前谢谢。
发布于 2015-12-11 10:12:47
找到解决办法:)
在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;
},
有关此函数的更多信息和提供的信息,请查看这。
发布于 2018-08-29 09:39:51
在确定是否需要分页时,pageBreakBefore
函数提供了很大的灵活性。然而,我又找到了一个解决方案,这个解决方案更简单,文档也更少,但它可以自动完成所有的魔术。这是unbreakable: true
发布版中的一个0.1.32属性。另外,在下面的线程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 :)
}
发布于 2021-03-14 13:36:33
简单的文本示例:
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'
]
}
https://stackoverflow.com/questions/33996534
复制相似问题