动态检查文本是否转到下一页并使用pdfmake以pdf格式添加分页

And*_*ool 4 javascript pdf pdfmake

对于一个项目我使用pdfmake在javascript中即时制作报价和发票pdf.我面临的问题是文本块在中间离开页面.我想要的是检查某个文本块或表是否将在页面之间拆分,如果是这样,在块之前添加一个分页符,以确保文本或表格完全在一个页面上.

我的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
                }
            };
Run Code Online (Sandbox Code Playgroud)

因此,在创建pdf并相应地添加分页符之前,我如何检查文本是否会离开页面?

提前致谢.

Vit*_*tok 13

pageBreakBefore函数为确定是否需要分页符提供了很大的灵活性。但是,我发现了另一种解决方案,该解决方案更直接,文档更少,但是可以自动完成所有操作。这是0.1.32版本中提供的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 :)
}
Run Code Online (Sandbox Code Playgroud)

  • 我可以确认此解决方案是否如广告中所述。 (2认同)
  • 请注意,如果牢不可破的堆栈比页面长,它就会消失......:\ (2认同)

And*_*ool 10

找到解决方案:)

在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;
  },
Run Code Online (Sandbox Code Playgroud)

有关此功能提供的信息更多信息来看看这个