puppeteer-html-pdf
HTML to PDF converter for Node.js
Installation
npm install puppeteer-html-pdf
Signature
htmlPDF.create(content, options, callback)
Usage
Example 1
const fs = require('fs');
const util = require('util');
const hbs = require('handlebars');
const htmlPDF = require('puppeteer-html-pdf');
const writeFile = util.promisify(fs.writeFile);
const readFile = util.promisify(fs.readFile);
const pdfData = {
invoiceItems: [
{ item: 'Website Design', amount: 5000 },
{ item: 'Hosting (3 months)', amount: 2000 },
{ item: 'Domain (1 year)', amount: 1000 },
],
invoiceData: {
invoice_id: 123,
transaction_id: 1234567,
payment_method: 'Paypal',
creation_date: '04-05-1993',
total_amount: 141.5,
},
baseUrl: 'https://ultimateakash.com'
}
const options = {
format: 'A4',
args: {
browserWSEndpoint: 'wss://chrome.browserless.io?token=YOUR_TOKEN'
}
}
const html = await readFile(__dirname + '/sample.html','utf8');
const template = hbs.compile(html);
const content = template(pdfData);
try {
const buffer = await htmlPDF.create(content, options);
await writeFile('sample.pdf', buffer);
} catch (error) {
console.log('htmlPDF error', error);
}
Example 2
const htmlPDF = require('puppeteer-html-pdf');
const options = {
format: 'A4',
path: `${__dirname}/sample.pdf`, // you can pass path to save the file
args: {
browserWSEndpoint: 'wss://chrome.browserless.io?token=YOUR_TOKEN'
}
}
const content = "<style> h1 {color:red;} </style> <h1>Welcome to puppeteer-html-pdf</h1>";
try {
await htmlPDF.create(content, options);
} catch (error) {
console.log('htmlPDF error', error);
}
Example 3
const htmlPDF = require('puppeteer-html-pdf');
const options = {
width: '219mm',
height: '297mm',
margin: {
left: '25px',
right: '25px',
top: '20px'
},
path: `${__dirname}/sample.pdf`, // you can pass path to save the file
args: {
browserWSEndpoint: 'wss://chrome.browserless.io?token=YOUR_TOKEN'
}
}
const content = 'https://www.google.com';
try {
await htmlPDF.create(content, options);
} catch (error) {
console.log('htmlPDF error', error);
}
Options
Property | Modifiers | Type | Description | Default |
---|---|---|---|---|
displayHeaderFooter | optional |
boolean | Whether to show the header and footer. | false |
footerTemplate | optional |
string | HTML template for the print footer. Has the same constraints and support for special classes as PDFOptions.headerTemplate. | |
format | optional |
PaperFormat |
letter . |
|
headerTemplate | optional |
string |
HTML template for the print header. Should be valid HTML with the following classes used to inject values into them: - - - - - |
|
height | optional |
string | number | Sets the height of paper. You can pass in a number or a string with a unit. | |
landscape | optional |
boolean | Whether to print in landscape orientation. | false |
margin | optional |
PDFMargin | Set the PDF margins. |
undefined no margins are set. |
omitBackground | optional |
boolean | Hides default white background and allows generating pdfs with transparency. | false |
pageRanges | optional |
string | Paper ranges to print, e.g. 1-5, 8, 11-13 . |
The empty string, which means all pages are printed. |
path | optional |
string | The path to save the file to. |
undefined , which means the PDF will not be written to disk. |
preferCSSPageSize | optional |
boolean | Give any CSS @page size declared in the page priority over what is declared in the width or height or format option. |
false , which will scale the content to fit the paper size. |
printBackground | optional |
boolean | Set to true to print background graphics. |
true |
scale | optional |
number | Scales the rendering of the web page. Amount must be between 0.1 and 2 . |
1 |
timeout | optional |
number | Timeout in milliseconds. Pass 0 to disable timeout. |
30_000 |
width | optional |
string | number | Sets the width of paper. You can pass in a number or a string with a unit. | |
headless | optional |
boolean | string | Sets Chromium launch mode. | new |
args | optional |
array | Sets Chromium flags mode. | ['--no-sandbox', '--disable-setuid-sandbox'] |
authorization | optional |
string | HTTP header to be sent with every request. |
Format
The sizes of each format are as follows:
-
Letter
: 8.5in x 11in -
Legal
: 8.5in x 14in -
Tabloid
: 11in x 17in -
Ledger
: 17in x 11in -
A0
: 33.1in x 46.8in -
A1
: 23.4in x 33.1in -
A2
: 16.54in x 23.4in -
A3
: 11.7in x 16.54in -
A4
: 8.27in x 11.7in -
A5
: 5.83in x 8.27in -
A6
: 4.13in x 5.83in
Margin
Property | Modifiers | Type |
---|---|---|
bottom | optional |
string | number |
left | optional |
string | number |
right | optional |
string | number |
top | optional |
string | number |
Linux Troubleshooting
Install chromium
sudo apt update
sudo apt install chromium
After chromium installation, if you still get missing dependencies issue. Install below dependencies.
sudo apt install ca-certificates fonts-liberation libasound2 libatk-bridge2.0-0 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgbm1 libgcc1 libglib2.0-0 libgtk-3-0 libnspr4 libnss3 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 lsb-release wget xdg-utils
https://pptr.dev/troubleshooting#chrome-headless-doesnt-launch-on-unix