使用PDFMake从数据创建多页pdf

时间:2019-07-16 13:01:56

标签: javascript jquery pdfmake

使用pdfMake js创建PDf,但是如果我的数据大于第一页的大小,则该过程将创建空白PDF

试图使我的代码得到保证,以便它可以等待docDef被完成

let docDefinition = {
                pageSize : 'A4',
                pageMargins : [25, 25, 25, 35],
                pageOrientation: 'landscape',
                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: 3,
                      // 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    : '80%',
                                text     : postPayload.reportName,
                                fontSize : 26,
                                bold     : true
                              }
                            ]
                          }
                        ],
                        // Second Header Row
                        [
                            {
                              columns: [
                                {
                                  width: 'auto',
                                  margin: [0,0,10,0],
                                  text: [
                                    { text: 'Report generated by [Name]\n', fontSize: 16, bold: true, color: '#63798a' },
                                    { text: `${metadata.user}`, fontSize: 14 }
                                  ]
                                },
                                {
                                  width: 'auto',
                                  margin: [0,0,10,0],
                                  text: [
                                    { text: 'Report generated by [NTID] \n', fontSize: 16, bold: true, color: '#63798a' },
                                    { text: `${metadata.ntid}`, fontSize: 14 }
                                  ]
                                },
                                {
                                  width: 'auto',
                                  margin: [0,0,10,0],
                                  text: [
                                    { text: 'Report generated on \n', fontSize: 16, bold: true, color: '#63798a' },
                                    { text: `${metadata.timestamp}`, fontSize: 14 }
                                  ]
                                },
                                {
                                    width: 'auto',
                                    margin: [0,0,10,0],
                                    text: [
                                      { text: 'Result count \n', fontSize: 16, bold: true, color: '#63798a' },
                                      { text: `${content.length}`, fontSize: 14 }
                                    ]
                                }
                              ]
                            }
                          ],

                        [{   unbreakable: true,
                              table: {
                                headerRows: 1,
                                widths: postPayload.colDefs.map(header => `${100 /  postPayload.colDefs.length}%`),
                                body: tableData
                            }
                        }]                  
                      ]
                    },
                    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;
                        }
                      }
                    }
                  },  
                footer : function(current, count) {
                 return {
                    columns: [{
                        text: 'Internal Use',
                        alignment: 'right',
                        opacity: 0.6,
                        fontSize: 14,
                    }, {
                        text: `${current.toString()} / ${count}`,
                        alignment: 'right',
                        opacity: 0.6,
                        fontSize: 14,
                        margin: [0, 10, 30, 10],
                    }]
                    }
                },


              };

并在其他函数中以

调用PDFCreate函数
 this.downloadPDF().then(function(results) { 
            const pdfDocGenerator = pdfMake.createPdf(results);
            pdfDocGenerator.open();})

希望创建的PDF包含跨多个页面的数据

0 个答案:

没有答案