Skip to content

Instantly share code, notes, and snippets.

@agronom81
Created January 13, 2023 08:10
Show Gist options
  • Save agronom81/0ba057ce50085ead19b01c65cc4234b2 to your computer and use it in GitHub Desktop.
Save agronom81/0ba057ce50085ead19b01c65cc4234b2 to your computer and use it in GitHub Desktop.

Revisions

  1. agronom81 created this gist Jan 13, 2023.
    52 changes: 52 additions & 0 deletions Dividers
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    // create document
    const doc = new PDFDocument({ margin: 30, });

    // to save on server
    doc.pipe(fs.createWriteStream("./document-5.pdf"));

    const table01 = {
    "headers" : ["A", "B", "C"],
    "rows": [
    [ "Version 0.1.74", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus id est ipsum. Fusce scelerisque maximus justo, lacinia ornare felis iaculis nec.", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus id est ipsum. " ],
    [ "Update:", "Age 2", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus id est ipsum. Fusce." ],
    [ "$ npm pdfkit-table@latest", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus id est ipsum.", "Year 3" ],
    [ "Thanks", "Age 4", "Year 4" ],
    ],
    };

    doc.table(table01, {
    columnSpacing: 10,
    padding: 10,
    columnsSize: [200, 220, 135],
    align: "center",
    prepareHeader: () => doc.fontSize(11), // {Function}
    // -----------------------------------------------------------------
    // HERE THE MAGIC:
    // -----------------------------------------------------------------
    prepareRow: (row, indexColumn, indexRow, rectRow, rectCell) => {

    const {x, y, width, height} = rectCell;

    // first line
    if(indexColumn === 0){
    doc
    .lineWidth(.5)
    .moveTo(x, y)
    .lineTo(x, y + height)
    .stroke();
    }

    doc
    .lineWidth(.5)
    .moveTo(x + width, y)
    .lineTo(x + width, y + height)
    .stroke();


    doc.fontSize(10).fillColor('#292929');

    }, // {Function}
    });

    // done
    doc.end();