node.jspdfkitnode-pdfkit

pdfkit-table doesn't add new page, writes over same text on page one


I am new with PDFKit for generate a server-side PDF with node-js, I also am using pdfkit-table for better table creation inside the PDF. My code creates a pdf with 4 tables when the size of the first two tables is to big that a new page needs to be add it doesn't it writes the 3rd and 4th table over in the same page one.

Here is my code and an image that shows how is over writing the text on the page one.

async createPdf(
    lastMetars: any[],
    lastTafs: any[],
    lastSigmets: any[],
    facu: any,
    imagesToRender: any[],
  ) {

    const doc = new PDFDocument({ size: 'A4' });
    doc.pipe(fs.createWriteStream('output.pdf'));

    doc.fontSize(25).text('CUB470 2024-07-15 MUHA LEMD', { align: 'center' });

    doc.image(
      '/media/datos/ecna.jpg',
      200,
      100,
      { fit: [200, 200], align: 'center', valign: 'center' },
    );

    doc.moveDown(7);

    const tableMetars = {
      title: 'METAR',
      headers: [''],
      rows: lastMetars.map((metar) => {
        return [metar.text];
      }),
    };
    doc.table(tableMetars, {
      hideHeader: true,
    });

    const tableTafs = {
      title: 'TAF',
      headers: [''],
      rows: lastTafs.map((taf) => {
        return [taf.text];
      }),
    };
    doc.table(tableTafs, {
      hideHeader: true,
    });

    const tableSigmets = {
      title: 'SIGMET',
      headers: [''],
      rows: lastSigmets.map((sigmet) => {
        return [sigmet.text];
      }),
    };
    doc.table(tableSigmets, {
      hideHeader: true,
    });

    if (facu) {
      const tableFacu = {
        title: 'FACU',
        headers: [''],
        rows: [[facu.text]],
      };
      doc.table(tableFacu, {
        hideHeader: true,
      });
    }
   
    doc.end();
}

enter image description here I read the PdfKit docs that a new page is created if content is bigger for one page. But I don't know how to achieve this with pdf-kit.

Can anyone help me? Thanks in adv Lazaro


Solution

  • So after searching for a while I found this was an opened issue on this library Github' project. People fixed it by modifying the source code of this module as explained here. The problem then was taht after any package install or update the fix returned to its initial state and need have to change again. I solved that using this guide.

    I hope it helps any facing this issue. Regards, Lazaro