0

I need to generate a pdf of the packing list with the data of the notes and the volume of the box, and I want it to be added to a new page when it exceeds 40 pages, I'm using pdfKit

I'm using this link https://pdfkit.org/demo/browser.html

I need to generate a pakingList several pages

I tried that way but it doesn't work.

var doc = new PDFDocument({ size: 'A4'});
var stream = doc.pipe(blobStream());
 
const layoutX = { align: 'center' }
const layoutY = 20
let tableStartPos = 20
  
function generateInnerPdf(doc) {
    const notes = [
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 01 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 02 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 03 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 04 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 05 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 06 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 07 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 08 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 09 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 10 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 11 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 12 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 13 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 14 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 15 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 16 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 17 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 18 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 19 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 20 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 21 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 22 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 23 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 24 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 25 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 26 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 27 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 28 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 29 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 30 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 31 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 32 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 33 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 34 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 35 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 36 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 37 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 38 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 39 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 40 },
        { vehiclePlate: 'MTG7200', logisticName: 'PAC', numNf: 41 }
       ]
    generateHeader();
    generateBody(doc, notes);
    generateFooter(doc);

}

function generateHeader() {
    doc
    .fontSize(11)
    .text(
      'Lupo S.A Notas fiscais liberadas para despacho em ${data}',
      layoutX,
      layoutY
    )
    .text(
      'Transportador: ${vehiclePlate} - ${logisticName}',
      layoutX,
      layoutY + 20
    )
}

function generateBody(doc, notes) {
    let index = 1;
    for (i = 0; i < notes.length; i++) {
      const item = notes[i];
      let position = tableStartPos + index * 20;
      index++;
      if (nfs.length > 40) {
        index = 0;
        tableStartPos = 20;
        doc.addPage();
      }
      generateTableBody(
        doc,
        position,
        item.numNf,
        item.logisticName
      );
    }
}

function generateTableBody(doc, y, numNf, logisticName) {
    doc
      .fontSize(10)
      .text(numNf, 50, y)
      .text(logisticName, 150, y)
  }

 function generateFooter(doc){
  doc
    .fontSize(11)
    .text(
      'Declaro ter recebido as notas fiscais acima relacionadas e suas respectivas mercadorias em perfeitas condições',
      layoutX,
      layoutY + 660
    )
    .text('Araraquara, ${data}', layoutX, layoutY + 690)
    .text(
      '____________________________________________________________',
      layoutX,
      layoutY + 720
    )
    .text('Assinatura', layoutX, layoutY + 735)
    
}

doc.end();
stream.on('finish', function() {
  iframe.src = stream.toBlobURL('application/pdf');
});
  • All of your code is in a string (it's all between backticks). You are not executing anything. Furthermore, writing, "I tried that way but it doesn't work" tells us nothing about what's not working or what error messages you're seeing. – Marc Apr 18 '23 at 15:13
  • I tried to follow the pattern of this link, I just separated it into functions, but it returns a blank pdf : https://pdfkit.org/demo/browser.html – Wilton Santos Apr 18 '23 at 16:58

0 Answers0