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

时间:2015-11-30 10:35:05

标签: javascript pdf pdfmake

对于在javascript中使用pdfmake即时制作优惠和发票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
                }
            };

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

提前致谢。

4 个答案:

答案 0 :(得分:10)

找到解决方案:)

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

with member [Version].[Version subgroup].[x] as
      [Version].[Version subgroup].&[AVG 2012]
     +[Version].[Version subgroup].&[AVG 2013]
     +[Version].[Version subgroup].&[AVG 2014]
     +[Version].[Version subgroup].&[AVG 2015]
     +[Version].[Version subgroup].&[AVG 2016]
     +[Version].[Version subgroup].&[AVG 7-10] 

    member a as
         ([Version].[Version subgroup].[x]
         ,[Measures].[User count])

    member a1 as
         ([Product].[Finance Group].&[1],a)

    member[Product].[Finance Group].[x] as
          [Product].[Finance Group].&[3]
         +[Product].[Finance Group].&[6]
         +[Product].[Finance Group].&[8]
         +[Product].[Finance Group].&[2]
         +[Product].[Finance Group].&[9]

    member b1 as
         ([Product].[Finance Group].[x]
         ,[Measures].[User count])

    member final as a1+b1

 SELECT
 NON EMPTY 
 {
  [Measures].[User count], 
  [Measures].[User count oM], 
  [Measures].[User count oQ], 
  [Measures].[User count oY]
 } ON COLUMNS
  FROM [User count]
  WHERE ([Subscription tier].[Subscription tier].&[1]
   , StrToMember("[Fordate].[YQMD].[Date].["+Format(Now()-1,"yyyy-MM-dd")+"]") 
   ,[User type].[User type].&[1]
     )

有关此功能及所提供信息的详细信息,请查看this

答案 1 :(得分:8)

const Textura = ({handler}) => ( <button onClick={handler} / > ) 函数为确定是否需要分页符提供了很大的灵活性。但是,我发现了另一种解决方案,该解决方案更直接,文档更少,但是可以自动完成所有操作。这是0.1.32版本中的pageBreakBefore属性。另外,它在以下线程https://github.com/bpampuch/pdfmake/issues/1228#issuecomment-354411288

中提到

它如何工作? 例如,您要使标题和标题下面的一些文本牢不可破。为此,您必须将标头和内容包装在堆栈中,并在其上应用unbreakable: true

unbreakable: true

答案 2 :(得分:0)

带有文本的简单示例:

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'
    ]
}

答案 3 :(得分:-1)

pageBreakBefore: (currentNode, followingNodesOnPage, nodesOnNextPage, previousNodesOnPage) => {
    if (currentNode.id === 'yournodeid') {
        if(previousNodesOnPage[0].startPosition.pageNumber != followingNodesOnPage[0].pageNumbers) {
            return true;
        }
    }
    return false;
}