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
Maintainers
Readme
html-to-pdf-pup
A package to convert html+css to pdf in node js.
Features:-
- Highly scalable event driven system.
- Can asynchronously convert multiple html to pdf using multiple tabs or can use same tab for queue pdf generation.
- A message queue system to manage the conversion process.
- A simple and easy to use API for begineers.
- Comes with full config mode and a dev mode for advanced users.
- User can configure the number of tabs to be used for conversion to suit their backend server.
- Full control over the conversion process and pdf options in the config mode.
- Uses puppeter under the hood for pdf conversion.
- Returns a pdf buffer .
- Use dynamic height for pdfs by default.
- Have a built in cron job to make browser open in hotspots of your api.
Optimizations techniques used:-
- Uses a message queue system to manage the conversion process.
- Only a single browser is opened no matter what.
- Browser only closes itself when all requests are done.
- Multiple tabs opens for concurrent pdf conversion.
- If a tab has done pdf conversion then it starts processing another request and only closes itself when all requests are done.
- Tight integration of max tab system and single broswer for better resource management.
- 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:-
DEV_MODE: boolean
//Default value DEV_MODE: false;
If true , then whole steps will be console logged showing the conversion process.
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.
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
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
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.
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