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

html-to-pdf-pup

v2.2.0

Published

An event-driven optimized system to convert HTML to PDF using Puppeteer in node js

Downloads

27

Readme

html-to-pdf-pup

NPM

A package to convert html+css to pdf in node js.

Features:-

  1. Highly scalable event driven system.
  2. Can asynchronously convert multiple html to pdf using multiple tabs or can use same tab for queue pdf generation.
  3. A message queue system to manage the conversion process.
  4. A simple and easy to use API for begineers.
  5. Comes with full config mode and a dev mode for advanced users.
  6. User can configure the number of tabs to be used for conversion to suit their backend server.
  7. Full control over the conversion process and pdf options in the config mode.
  8. Uses puppeter under the hood for pdf conversion.
  9. Returns a pdf buffer .
  10. Use dynamic height for pdfs by default.
  11. Have a built in cron job to make browser open in hotspots of your api.

Optimizations techniques used:-

  1. Uses a message queue system to manage the conversion process.
  2. Only a single browser is opened no matter what.
  3. Browser only closes itself when all requests are done.
  4. Multiple tabs opens for concurrent pdf conversion.
  5. If a tab has done pdf conversion then it starts processing another request and only closes itself when all requests are done.
  6. Tight integration of max tab system and single broswer for better resource management.
  7. Full control of api in hands of user with build in cron jobs

Installation

npm i html-to-pdf-pup

Usage

Basic usage

const { create_pdf } = require("html-to-pdf-pup");

let htmlData = "<html><body><h1>Hello World</h1></body></html>";

create_pdf(htmlData)
  .then((pdfBuffer) => {
    console.log(pdfBuffer);
  })
  .catch((err) => {
    console.log(err);
  });

Config Mode

It also have a config mode to configure the puppeter , pdf , and the conversion process.

const { create_pdf, configure_module } = require("html-to-pdf-pup");

configure_module({
  DEV_MODE: true,

  MAX_TABS: 5,

  puppeteerConfig: {
    headless: true,
    args: ["--no-sandbox", "--disable-setuid-sandbox"],
  },

  pdfConfig: {
    format: "A4",
    printBackground: true,
  },
});

let htmlData = "<html><body><h1>Hello World</h1></body></html>";

create_pdf(htmlData)
  .then((pdfBuffer) => {
    console.log(pdfBuffer);
  })
  .catch((err) => {
    console.log(err);
  });

configure_module takes an object with the following keys:-

  1. DEV_MODE: boolean

    //Default value
    DEV_MODE: false;

    If true , then whole steps will be console logged showing the conversion process.

  2. MAX_TABS: number (default: 2)

    It controls the maximum number of tabs that are allowed to open for pdf conversion. Increasing this will increase the async conversion speed as more pdf will convert simultaneously but it will also increase the resource usage.

    User can adjust it according to their server resources.

  3. pdfConfig: object

    //Default values
    pdfConfig : {
        printBackground: true ,
        width:'796px',
        height: (dynamic height according to html content)
    }

    It is the pdf options that are passed to the puppeter pdf function.

    Refer here for all the options Pdf options

  4. puppeteerConfig: object

    //Default values
    puppeteerConfig : {
        headless: true,
        args: [
             '--no-sandbox',
             '--disable-setuid-sandbox',
             '--disable-dev-shm-usage',
             '--disable-accelerated-2d-canvas',
             '--no-first-run',
             '--no-zygote',
             '--disable-gpu',
             '--disable-infobars',
             '--hide-scrollbars',
             '--disable-notifications',
             '--disable-extensions',
             '--disable-web-security',
             '--disable-background-networking',
             '--disable-default-apps',
             '--disable-translate',
             '--disable-sync',
             '--disable-logging',
             '--disable-background-timer-throttling',
             '--disable-client-side-phishing-detection',
             '--disable-popup-blocking',
             '--disable-component-extensions-with-background-pages',
             '--metrics-recording-only',
             '--ignore-certificate-errors',
             '--proxy-server="direct://"',
             '--proxy-bypass-list=*'
        ],
    }
    

    It is the puppeteer options that are passed to the puppeter launch function.

    Refer here for all the options puppeteer launch options , puppeteer chrome specific options

  5. browserConfig: object

    //Default values
    browserConfig : {
        coolDownTime:0,
        alwaysKeepOpen:false,
    }

    coolDownTime : It is the time in milliseconds that the browser will wait before closing itself after all the requests are done.

    alwaysKeepOpen : If true then the browser will never close itself and will always be open.

  6. cronConfig = object

    //Default values
    cronConfig : {
        browserStartTime : null,
        duration : null,
    }

    browserStartTime : It is a object with keys hour and minute to set the time at which the browser will open itself. e.g {hour: 13 , minute: 30} will open the browser at 1:30 PM.

    duration : It is the time in minutes after which the browser will close itself after opening , it should be atleast 30 minutes.

Note

if you are using your own chromium then pass the executable path to puppeteer config and set PUPPETEER_SKIP_DOWNLOAD=true as env to skip chromium download by puppeteer. Refer this for more info here