npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pdfkit-construct

v1.2.3

Published

PdfKit class that helps creating and styling tables

Downloads

354

Readme

Pdfkit-Construct

PdfkitConstruct is a pdfkit helper for simplifying the creation of tables.

Getting Started

Prerequisites

pdfkit-construct depends on pdfkit package, make sure its installed.

npm install pdfkit

Installing

Installation uses the npm package manager. Just type the following command after installing npm.

npm install pdfkit-construct

Usage

const PdfkitConstruct = require('pdfkit-construct');
const app = require('express')();


app.get("/", (req, res) => {

    getDbData()
        .then(products => {

            for (let i = 0; i < products.length; i++) {
                products[i].amount = (products[i].price * products[i].quantity).toFixed(2);
                products[i].price = products[i].price.toFixed(2);
            }

            // Create a document
            const doc = new PdfkitConstruct({
                size: 'A4',
                margins: {top: 20, left: 10, right: 10, bottom: 20},
                bufferPages: true,
            });

            // set the header to render in every page
            doc.setDocumentHeader({}, () => {


                doc.lineJoin('miter')
                    .rect(0, 0, doc.page.width, doc.header.options.heightNumber).fill("#ededed");

                doc.fill("#115dc8")
                    .fontSize(20)
                    .text("Hello world header", doc.header.x, doc.header.y);
            });

            // set the footer to render in every page
            doc.setDocumentFooter({}, () => {

                doc.lineJoin('miter')
                    .rect(0, doc.footer.y, doc.page.width, doc.footer.options.heightNumber).fill("#c2edbe");

                doc.fill("#7416c8")
                    .fontSize(8)
                    .text("Hello world footer", doc.footer.x, doc.footer.y + 10);
            });


            // add a table (you can add multiple tables with different columns)
            // make sure every column has a key. keys should be unique
            doc.addTable(
                [
                    {key: 'name', label: 'Product', align: 'left'},
                    {key: 'brand', label: 'Brand', align: 'left'},
                    {key: 'price', label: 'Price', align: 'right'},
                    {key: 'quantity', label: 'Quantity'},
                    {key: 'amount', label: 'Amount', align: 'right'}
                ],
                products, {
                    border: null,
                    width: "fill_body",
                    striped: true,
                    stripedColors: ["#f6f6f6", "#d6c4dd"],
                    cellsPadding: 10,
                    marginLeft: 45,
                    marginRight: 45,
                    headAlign: 'center'
                });


            // render tables
            doc.render();

            // this should be the last
            // for this to work you need to set bufferPages to true in constructor options 
            doc.setPageNumbers((p, c) => `Page ${p} of ${c}`, "bottom right");

            doc.pipe(res);
            doc.end();
        })
        .catch(error => {
            res.status(200).send(error.stack);
        })
});

function getDbData() {

    return new Promise((resolve, reject) => {
        resolve([
            {
                "id": 7631,
                "SKU": "HEH-9133",
                "name": "On Cloud Nine Pillow On Cloud Nine Pillow On Cloud Nine Pillow On Cloud Nine Pillow",
                "price": 24.99,
                "brand": "FabDecor",
                "quantity": 1,
                "created_at": "2018-03-03 17:41:13"
            },
            {
                "id": 7615,
                "SKU": "HEH-2245",
                "name": "Simply Sweet Blouse",
                "price": 42,
                "brand": "Entity Apparel",
                "quantity": 2,
                "created_at": "2018-03-20 22:24:21"
            },
            {
                "id": 8100,
                "SKU": "WKS-6016",
                "name": "Uptown Girl Blouse",
                "price": 58,
                "brand": "Entity Apparel",
                "quantity": 3,
                "created_at": "2018-03-16 21:55:28"
            }]);
    })
}

let port = process.env.PORT || 3330;
app.listen(port, () => console.log(`Server listening on port ${port}...`));

Api

If you are not familiar with PDFKIT package please consider checking this website.

  • addTable

    • columns : array of column object
      • key (string & unique & not null)
      • label (string)
      • align 'left' | 'right' | 'center'. [default : 'left']
    • rows : array of objects with properties name matching the value set in columns key
    • options : table general options
      • width : "auto", // auto | fill_body
      • marginLeft : 0,
      • marginRight : 0,
      • marginTop : 0,
      • marginBottom : 5,
      • border : {size: 0.1, color: '#cdcdcd'},
      • striped : false,
      • stripedColors : ['#fff', '#f0ecd5'],
      • headBackground : '#abc6f0',
      • headAlign : 'center' // left | right | center,
      • headColor : '#000',
      • headFont : "Helvetica-Bold",
      • headFontSize : 10,
      • headHeight : 10,
      • cellsFont : "Helvetica",
      • cellsFontSize : 9,
      • cellsAlign : 'center' // left | right | center,
      • cellsColor : "#000",
      • cellsPadding : 5,
      • cellsMaxWidth : 120

NOTE THAT YOU CAN ADD MULTIPLE TABLES !

  • setDocumentHeader

    • options
      • height : "10%" // accepts only percentage
    • renderer // callback function
  • setDocumentFooter

    • options
      • height : "5%" // accepts only percentage
    • renderer // callback function
  • addPageDoc

    Adds a page with header / footer rendered if they are set.

  • render

    Call this to render tables

  • setPageNumbers

    • templateRenderer = (p, count) => `${current} of ${count}` // function, returns string
      • p (current page)
      • count (number of pages in the doc)
    • position = "bottom" // accepts values : "top","top left","top right","bottom","bottom left","bottom right"

CALL THIS AFTER FINISHING ALL THE RENDERING!

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

This project is licensed under the MIT License.