9

プロジェクトの場合、javascript で pdfmake を使用してオンザフライでオファーと請求書の PDF を作成します。私が直面している問題は、テキストブロックが途中でページからはみ出すことです。私が望むのは、特定のテキスト ブロックまたは表がページ間で分割されるかどうかを確認し、その場合はブロックの前に改ページを追加して、テキストまたは表が完全に 1 ページに収まるようにすることです。

私の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

4 に答える 4

13

解決策を見つけました:)

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;
  },

この機能の詳細と提供される情報については、こちらをご覧ください

于 2015-12-11T10:12:47.867 に答える
1

テキスト付きの簡単な例:

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'
    ]
}
于 2021-03-14T13:36:33.967 に答える