Wilton Santos
Wilton Santos

Reputation: 1

Generate pdf with pdfKit packingList

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');
});

Upvotes: 0

Views: 35

Answers (0)

Related Questions