首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >动态检查文本是否转到下一页,并使用pdfmake在pdf中添加分页。

动态检查文本是否转到下一页,并使用pdfmake在pdf中添加分页。
EN

Stack Overflow用户
提问于 2015-11-30 10:35:05
回答 4查看 20.5K关注 0票数 11

对于一个项目,im提供报价和发票pdf的动态使用pdfmake在javascript。我面临的问题是在中间有文本块离开页面。我想要的是检查某个文本块或表是否要在页面之间分割,如果是的话,在块之前添加一个分页,以确保文本或表将完全放在一个页面上。

我的pdf docDefinition是这样构建的:

代码语言:javascript
运行
复制
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之前,我如何检查文本是否会离开页面,并相应地添加分页?

提前谢谢。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-12-11 10:12:47

找到解决办法:)

在DocDefinition中,您可以为pageBreakBefore添加如下函数:

代码语言:javascript
运行
复制
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;
  },

有关此函数的更多信息和提供的信息,请查看

票数 14
EN

Stack Overflow用户

发布于 2018-08-29 09:39:51

在确定是否需要分页时,pageBreakBefore函数提供了很大的灵活性。然而,我又找到了一个解决方案,这个解决方案更简单,文档也更少,但它可以自动完成所有的魔术。这是unbreakable: true发布版中的一个0.1.32属性。另外,在下面的线程https://github.com/bpampuch/pdfmake/issues/1228#issuecomment-354411288中也提到了

它是如何工作的?例如,,您想要使标题和它下面的一些文本不可打破。为了做到这一点,您必须将头和内容包装在堆栈中,并将unbreakable: true应用于其中。

代码语言:javascript
运行
复制
{
    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 :)
}
票数 32
EN

Stack Overflow用户

发布于 2021-03-14 13:36:33

简单的文本示例:

代码语言:javascript
运行
复制
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'
    ]
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33996534

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档