【问题标题】:Increase header size pdfMake增加标题大小 pdfMake
【发布时间】:2016-05-03 10:18:12
【问题描述】:

我正在尝试使用 pdfmake 增加 pdf 的标题大小。

目前能够在页面的左右两边都得到一个标题,这正是我想要的,但是当高度超过 26 时,图像消失了,因为标题的空间有限。

  • 可以减小边距以增加大小,但我想要 留有余量。
  • 我在 pdfMake github 上搜索过类似的问题,但没有成功。

如果你需要测试任何东西,试试我目前的代码 pdfmake playground

请记住,您需要将所有这些代码复制并粘贴到“操场”上才能正常工作。

var dd = {
  pageSize: 'LEGAL',
  pageOrientation: 'landscape',
  header: {
    margin: 8,
    columns: [{
      table: {
        widths: ['50%', '50%'],
        body: [
          [{
            image: 'sampleImage.jpg',
            width: 80,
            height: 26,
          }, {
            image: 'sampleImage.jpg',
            width: 80,
            height: 26,
            alignment: 'right'
          }]
        ]
      },
      layout: 'noBorders'
    }]
  },
  content: [
    'First paragraph',
    'Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines'
  ]
}

【问题讨论】:

    标签: javascript pdfmake


    【解决方案1】:

    您需要添加一个 pageMargins 并将第二个参数(上边距)调整为您的总标题大小。您可以尝试使用值,直到所有标题内容都可见。

    例如:

    在这种情况下,我使用 80 (pageMargin: [40,80,40,60]) 来获取高度为 60 的图像

    var dd = {
    
        pageSize: 'LEGAL',
        pageOrientation: 'landscape',
        pageMargins: [40, 80, 40, 60],
    
        header: {
            margin: 8,
            columns: [
                {
                    table: {
                        widths: [ '50%','50%'],
    
                        body: [
                            [
                                { image: 'sampleImage.jpg',
    
                                    width: 80, height: 60,
    
                                },
    
                                { image: 'sampleImage.jpg',
    
                                    width: 80, height: 60,
                                    alignment:'right'}
                            ]
                        ]
                    },
                    layout: 'noBorders'
                }
    
            ]
        },
    
        content: [
            'First paragraph',
            'Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines'
        ]
    }
    

    【讨论】:

    • 救命答案。谢谢
    【解决方案2】:

    对此已接受的答案是一个很好的答案,但我认为既然我找到了一个我认为可能对其他人更有效的答案,尤其是如果标题内容长度可能会有所不同,我会分享。

    pdfmake 中的表格有一个很好的功能,其中标题行在表格跨越的每一页上重复。因此,您可以创建一个包含整个文档的表格,并添加任意数量的标题行,它们将在所有页面中保持粘性。这是一个示例文档定义。

    var docDefinition = {
    
      pageSize : 'LETTER',
      pageMargins : [25, 25, 25, 35],
    
      defaultStyle : {
        fontSize  : 12,
        columnGap : 20
      },
    
      // Page Layout
    
      content : {
    
        // This table will contain ALL content
        table : {
          // Defining the top 2 rows as the "sticky" header rows
          headerRows: 2,
          // One column, full width
          widths: ['*'],
          body: [
    
    
            // Header Row One
            // An array with just one "cell"
            [
              // Just because I only have one cell, doesn't mean I can't have
              // multiple columns!
              {
                columns : [
                  {
                    width    : '*',
                    text     : 'Delivery Company, Inc.',
                    fontSize : 12,
                    bold     : true
                  },
                  {
                    width     : '*',
                    text      : [
                      { text: 'Delivery Slip\n', fontSize: 12 },
                      { text: 'Date ', bold: true },
                      '2/16/2015   ',
                      { text: 'Arrived ', bold: true },
                      '11:05 AM   ',
                      { text: 'Left ', bold: true },
                      '11:21 AM'
                    ],
                    fontSize  : 10,
                    alignment : 'right'
                  }
                ]
              }
            ],
    
    
            // Second Header Row
    
            [
              {
                columns: [
                  {
                    width: 'auto',
                    margin: [0,0,10,0],
                    text: [
                      { text: 'CUSTOMER\n', fontSize: 8, bold: true, color: '#bbbbbb' },
                      { text: 'John Doe', fontSize: 12 }
                    ]
                  },
                  {
                    width: 'auto',
                    margin: [0,0,10,0],
                    text: [
                      { text: 'INVOICE #\n', fontSize: 8, bold: true, color: '#bbbbbb' },
                      { text: '123456', fontSize: 12 }
                    ]
                  }
                ]
              }
            ],
    
            // Now you can break your content out into the remaining rows.
            // Or you could have one row with one cell that contains
            // all of your content
    
            // Content Row(s)
    
            [{
              fontSize: 10,
              stack: [
    
                // Content
    
                { text:'this is content', pageBreak: 'after' },
                { text:'this is more content', pageBreak: 'after' },
                { text:'this is third page content' }
              ]
            }],
    
            [{
              fontSize: 10,
              stack: [
    
                // Content
    
                { text:'this is content', pageBreak: 'after' },
                { text:'this is more content', pageBreak: 'after' },
                { text:'this is third page content' }
              ]
            }]
    
    
          ]
        },
    
    
        // Table Styles
    
        layout: {
          hLineWidth: function(i, node) { return (i === 1 || i === 2) ? 1 : 0; },
          vLineWidth: function(i, node) { return 0; },
          hLineColor: function(i, node) { return (i === 1 || i === 2) ? '#eeeeee' : 'white'; },
          vLineColor: function(i, node) { return 'white' },
          paddingBottom: function(i, node) {
            switch (i) {
              case 0:
                return 5;
              case 1:
                return 2;
              default:
                return 0;
            }
          },
          paddingTop: function(i, node) {
            switch (i) {
              case 0:
                return 0;
              case 1:
                return 2;
              default:
                return 10;
            }
          }
        }
      },
    
    
      // Page Footer
    
      footer : function(currentPage, pageCount) {
        return {
          alignment : 'center',
          text      : currentPage.toString() + ' of ' + pageCount,
          fontSize  : 8
        }
      },
    
    };
    

    【讨论】:

    • 更好的解决方案。尽管您的实际代码太多特定于应用程序。一个更简洁的例子会更好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-26
    相关资源
    最近更新 更多