【问题标题】:PDFkit adding content on a new pagePDFkit 在新页面上添加内容
【发布时间】:2020-12-06 12:26:36
【问题描述】:

我正在使用数据库中的数据生成 pdf。它工作正常,直到有太多数据不适合第一页。当第一个页面已满时如何创建新页面并继续在新页面上添加数据?

生成内部函数

function generateInnerPdf(doc, missions) {
  generateHeader(doc, missions);
  generateTableRow(
    doc,
    tableStartPos,
    'Název mise',
    'Uav',
    'GPS',
    'Doba letu',
    'Teplota',
    'Vítr',
    'Start mise',
    'Konec mise',
    'Použité baterie',
    'Popis'
  );
  generateHr(doc, tableStartPos + 45);
  generateBody(doc, missions);
  generateFooter(doc);
}

生成身体的功能

function generateBody(doc, missions) {
  for (i = 0; i < missions.length; i++) {
    const item = missions[i];
    let position = tableStartPos + (i + 1) * 60;

    generateTableBody(
      doc,
      position,
      item.missionName,
      item.uav,
      item.gps,
      item.flightTime,
      item.tmp,
      item.wind,
      moment(item.missionStart).format('lll'),
      moment(item.missionEnd).format('lll'),
      item.usedBatteries,
      item.desc
    );
  }
}

生成表格正文

function generateTableBody(doc, y, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10) {
  doc
    .fontSize(10)
    .text(c1, 50, y)
    .text(c2, 150, y)
    .text(c3, 250, y)
    .text(`${c4} min`, 480, y)
    .moveDown()
    .text(`${c5} °C`, 50, y + 15)
    .text(`${c6} m/s`, 150, y + 15)
    .text(c7, 250, y + 15)
    .text(c8, 450, y + 15)
    .text(c9, 50, y + 30)
    .text(c10, 150, y + 30);
}

当数据过多时,它会创建大量随机数据页面。如何解决它。谢谢!

Output range每10页添加一次

编辑:尝试了这种情况

 if (position > 650) {
      doc.addPage();
      position = 130;
    }

但它会在新页面上添加每条新记录

【问题讨论】:

    标签: node.js express pdf pdfkit


    【解决方案1】:

    解决方案

    function generateBody(doc, missions) {
      let index = 1;
      for (i = 0; i < missions.length; i++) {
        const item = missions[i];
        let position = tableStartPos + index * 60;
        index++;
        if (position > 650) {
          index = 0;
          tableStartPos = 20;
          doc.addPage();
        }
        generateTableBody(
          doc,
          position,
          item.missionName,
          item.uav,
          item.gps,
          item.flightTime,
          item.tmp,
          item.wind,
          moment(item.missionStart).format('lll'),
          moment(item.missionEnd).format('lll'),
          item.usedBatteries,
          item.desc
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-25
      • 2017-07-23
      • 1970-01-01
      • 2012-09-22
      • 1970-01-01
      相关资源
      最近更新 更多