# node-hbs-to-pdf

> Small NodeJS library to convert handlebars files to pdf

Latest version **1.1.2** (published 2024-12-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install node-hbs-to-pdf
pnpm add node-hbs-to-pdf
yarn add node-hbs-to-pdf
bun add node-hbs-to-pdf
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2024-12-04 |
| First published | 2023-08-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=18.0.0 |
| Dependencies | 2 |
| Unpacked size | 15.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Diego P. Bernal |
| Maintainers | dbernal |
| Keywords | pdf, hbs |

## Links

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

## Dependencies (2)

- [puppeteer](https://npm.io/package/puppeteer.md) ^23.7.1
- [handlebars](https://npm.io/package/handlebars.md) ^4.7.8

## 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.1.2 (latest) — 2024-12-04
- 1.1.1 — 2024-08-29
- 1.1.0 — 2024-08-14
- 1.0.6 — 2024-02-01
- 1.0.5 — 2023-08-14
- 1.0.4 — 2023-08-11
- 1.0.3 — 2023-08-04
- 1.0.2 — 2023-08-04
- 1.0.1 — 2023-08-04
- 1.0.0 — 2023-08-04

## README

# Handlebar template to PDF

This library converts hbs templates to pdf.

And that's all.

### How to use

only call the "create" function

```js
import { create, registerHelpers } from "node-hbs-to-pdf";
import myTemplate from "./myTemplate.hbs";

// create(props: CreateProps): Promise<Buffer>
const PDF = await create({
  template: myTemplate,
  context: {
    any: "context or data",
    key: "value",
  },
});
const headers = new Headers({ "Content-Type": "application/pdf" });
return new Response(body, { status: 200, headers });

// OR
await create({
  template: myTemplate,
  context: {
    any: "context or data",
    key: "value",
  },
  path: "/path/to/save/file.pdf",
});
```

> **Any problem importing the ".hbs" file?**
> You can pass the file as ".txt" extension but having inside the handlebar template
>
> ```js
> import myTemplate from "./myTemplate.txt";
> ```

### Props

| Key                             | type             | Description                                                           |
| ------------------------------- | ---------------- | --------------------------------------------------------------------- |
| **template**                    | Buffer / string  | handlebar template (.hbs). It can be a Buffer or a string.            |
| **context**                     | object           | any data that you want to pass to handlebar template.                 |
| **path** _(optional)_           | string           | the path where you want to save the PDF file.                         |
| **margin** _(optional)_         | **MarginObject** | the margin of the pdf                                                 |
| **headerTemplate** _(optional)_ | Buffer / string  | handlebar template for document **header**                            |
| **footerTemplate** _(optional)_ | Buffer / string  | handlebar template for document **footer**                            |
| puppeteer                       | object           | puppeteer config, for now it only accepts **noSandbox** key (boolean) |

**MarginObject**
| Key | type
| ------ | ------ |
| top | string / number |
| bottom | string / number |
| left | string / number |
| right | string / number |

### How it works

This library uses the handlebar compiler to convert hbs template to html code and then, with puppeteer creates the pdf file.

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