# html-pdf-node

> Convert any html content or html page to PDF

Latest version **1.0.8** (published 2021-12-14) · ISC license · 0 weekly downloads

## Install

```sh
npm install html-pdf-node
pnpm add html-pdf-node
yarn add html-pdf-node
bun add html-pdf-node
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.8 |
| Published | 2021-12-14 |
| First published | 2020-07-20 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | separate (@types/html-pdf-node) |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 55.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 202 |
| Author | mrafiqk |
| Maintainers | mrafiqk |
| Keywords | html, pdf, nodejs, puppeteer, handlebars |

## Links

- npm: https://www.npmjs.com/package/html-pdf-node
- Repository: https://github.com/mrafiqk/html-pdf-node
- Homepage: https://github.com/mrafiqk/html-pdf-node#readme
- Issues: https://github.com/mrafiqk/html-pdf-node/issues
- npm.io page: https://npm.io/package/html-pdf-node

## Dependencies (4)

- [bluebird](https://npm.io/package/bluebird.md) ^3.7.2
- [puppeteer](https://npm.io/package/puppeteer.md) ^10.4.0
- [handlebars](https://npm.io/package/handlebars.md) ^4.7.6
- [inline-css](https://npm.io/package/inline-css.md) ^3.0.0

## Alternatives

- [@cantoo/pdf-lib](https://npm.io/package/@cantoo/pdf-lib.md) — 297.9K weekly downloads
- [datatables.net-buttons](https://npm.io/package/datatables.net-buttons.md) — 200.1K weekly downloads
- [@ckeditor/ckeditor5-export-pdf](https://npm.io/package/@ckeditor/ckeditor5-export-pdf.md) — 167.0K weekly downloads
- [scanbot-web-sdk](https://npm.io/package/scanbot-web-sdk.md) — 15.0K weekly downloads
- [@syncfusion/ej2-angular-pdfviewer](https://npm.io/package/@syncfusion/ej2-angular-pdfviewer.md) — 8.8K weekly downloads

## Recent versions

- 1.0.8 (latest) — 2021-12-14
- 1.0.7 — 2020-12-16
- 1.0.6 — 2020-12-13
- 1.0.5 — 2020-08-13
- 1.0.4 — 2020-08-13
- 1.0.3 — 2020-07-31
- 1.0.2 — 2020-07-21
- 1.0.1 — 2020-07-20
- 1.0.0 — 2020-07-20

## README

# html-pdf-node

> Pagination plugin for converts html or url to pdf



**Note:** This plugin will convert html page or public URL into PDF. This will work with Node.js
=======

## Installation

```sh
npm install html-pdf-node
```

## Usage

To convert `HTML` page to `PDF` using `generatePdf` method:

```js
var html_to_pdf = require('html-pdf-node');

let options = { format: 'A4' };
// Example of options with args //
// let options = { format: 'A4', args: ['--no-sandbox', '--disable-setuid-sandbox'] };

let file = { content: "<h1>Welcome to html-pdf-node</h1>" };
// or //
let file = { url: "https://example.com" };
html_to_pdf.generatePdf(file, options).then(pdfBuffer => {
  console.log("PDF Buffer:-", pdfBuffer);
});
```

### html_to_pdf.generatePdf ( [file], [options], [callback] )

**Parameters**

`file` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object")> File object should have one of the following properties:

- `url` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> Any public url of the PDF.
- `content`<[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> HTML file content of the PDF.

`options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object")> Options object should have the following properties:

-   `args`  <[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array "Array")<[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")>> Additional arguments to pass to the browser instance. The list of Chromium flags can be found [here](http://peter.sh/experiments/chromium-command-line-switches/). This options will overwrite the default arguments. The default arguments are `['--no-sandbox', '--disable-setuid-sandbox']`.
-   `path`  <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> The file path to save the PDF to. If  `path`  is a relative path, then it is resolved to  [current working directory](https://nodejs.org/api/process.html#process_process_cwd). If no path is provided, the PDF won't be saved anywhere.
-   `scale`  <[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")> Scale of the webpage rendering. Defaults to  `1`. Scale amount must be between 0.1 and 2.
-   `displayHeaderFooter`  <[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean")> Display header and footer. Defaults to  `false`.
-   `headerTemplate`  <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> HTML template to print the header. Should be valid HTML markup with following classes used to inject printing values into them:
    -   `date`  formatted date
    -   `title`  document title
    -   `url`  document location
    -   `pageNumber`  current page number
    -   `totalPages`  total pages in the document
-   `footerTemplate`  <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> HTML template to print the footer. Should use the same format as the  `headerTemplate`.
-   `printBackground`  <[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean")> Print background graphics. Defaults to  `false`.
-   `landscape`  <[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean")> Paper orientation. Defaults to  `false`.
-   `pageRanges`  <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> Paper ranges to print, e.g., '1-5, 8, 11-13'. Defaults to the empty string, which means print all pages.
-   `format`  <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> Paper format. If set, takes priority over  `width`  or  `height`  options. Defaults to 'Letter'.
-   `width`  <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")|[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")> Paper width, accepts values labeled with units.
-   `height`  <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")|[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")> Paper height, accepts values labeled with units.
-   `margin`  <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object")> Paper margins, defaults to none.
    -   `top`  <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")|[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")> Top margin, accepts values labeled with units.
    -   `right`  <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")|[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")> Right margin, accepts values labeled with units.
    -   `bottom`  <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")|[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")> Bottom margin, accepts values labeled with units.
    -   `left`  <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")|[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")> Left margin, accepts values labeled with units.
-   `preferCSSPageSize`  <[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean")> Give any CSS  `@page`  size declared in the page priority over what is declared in  `width`  and  `height`  or  `format`  options. Defaults to  `false`, which will scale the content to fit the paper size.

**Return value**

Promise which resolves with PDF buffer.

### html_to_pdf.generatePdfs ( [files], [options], [callback] )

**Parameters**

`files` <[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array "Array")<[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object")>> File object should have one of the follwing properties:

- `url` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> Any public url of the PDF.
- `content`<[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> HTML file content of the PDF.

`options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object")> Options object should have the following properties:

-   `args`  <[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array "Array")<[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")>> Additional arguments to pass to the browser instance. The list of Chromium flags can be found [here](http://peter.sh/experiments/chromium-command-line-switches/). This options will overwrite the default arguments. The default arguments are `['--no-sandbox', '--disable-setuid-sandbox']`.
-   `path`  <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> The file path to save the PDF to. If  `path`  is a relative path, then it is resolved to  [current working directory](https://nodejs.org/api/process.html#process_process_cwd). If no path is provided, the PDF won't be saved anywhere.
-   `scale`  <[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")> Scale of the webpage rendering. Defaults to  `1`. Scale amount must be between 0.1 and 2.
-   `displayHeaderFooter`  <[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean")> Display header and footer. Defaults to  `false`.
-   `headerTemplate`  <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> HTML template to print the header. Should be valid HTML markup with following classes used to inject printing values into them:
    -   `date`  formatted date
    -   `title`  document title
    -   `url`  document location
    -   `pageNumber`  current page number
    -   `totalPages`  total pages in the document
-   `footerTemplate`  <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> HTML template to print the footer. Should use the same format as the  `headerTemplate`.
-   `printBackground`  <[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean")> Print background graphics. Defaults to  `false`.
-   `landscape`  <[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean")> Paper orientation. Defaults to  `false`.
-   `pageRanges`  <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> Paper ranges to print, e.g., '1-5, 8, 11-13'. Defaults to the empty string, which means print all pages.
-   `format`  <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> Paper format. If set, takes priority over  `width`  or  `height`  options. Defaults to 'Letter'.
-   `width`  <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")|[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")> Paper width, accepts values labeled with units.
-   `height`  <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")|[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")> Paper height, accepts values labeled with units.
-   `margin`  <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object")> Paper margins, defaults to none.
    -   `top`  <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")|[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")> Top margin, accepts values labeled with units.
    -   `right`  <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")|[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")> Right margin, accepts values labeled with units.
    -   `bottom`  <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")|[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")> Bottom margin, accepts values labeled with units.
    -   `left`  <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")|[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")> Left margin, accepts values labeled with units.
-   `preferCSSPageSize`  <[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean")> Give any CSS  `@page`  size declared in the page priority over what is declared in  `width`  and  `height`  or  `format`  options. Defaults to  `false`, which will scale the content to fit the paper size.

**Return value**

Promise which resolves with array of object which contains file objects with PDF buffers.

***Example:***
```js
let options = { format: 'A4' };
let file = [{ url: "https://example.com", name: 'example.pdf' }];

html_to_pdf.generatePdfs(file, options).then(output => {
  console.log("PDF Buffer:-", output); // PDF Buffer:- [{url: "https://example.com", name: "example.pdf", buffer: <PDF buffer>}]
});
```

---
_Source: https://npm.io/package/html-pdf-node · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
