# @isneezy/pdf-generator

> A powerful and versatile library for converting HTML pages, templates, and URLs into high-quality PDF documents

Latest version **0.0.1** (published 2023-01-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install @isneezy/pdf-generator
pnpm add @isneezy/pdf-generator
yarn add @isneezy/pdf-generator
bun add @isneezy/pdf-generator
```

## Health

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

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

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2023-01-23 |
| First published | 2023-01-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 22.4 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 43 |
| Author | Ivan Vilanculo |
| Maintainers | isneezy |
| Keywords | PDF, table, of, contents, html, to, pdf, html2pdf, toc, generator |

## Links

- npm: https://www.npmjs.com/package/@isneezy/pdf-generator
- Repository: https://github.com/isneezy/pdf-generator-service
- Homepage: https://github.com/isneezy/pdf-generator-service/tree/main/packages/pdf-generator#readme
- Issues: https://github.com/isneezy/pdf-generator-service/issues
- npm.io page: https://npm.io/package/@isneezy/pdf-generator

## Dependencies (8)

- [axios](https://npm.io/package/axios.md) ^1.2.3
- [jsdom](https://npm.io/package/jsdom.md) ^21.0.0
- [vitest](https://npm.io/package/vitest.md) ^0.27.3
- [uid-safe](https://npm.io/package/uid-safe.md) ^2.1.5
- [pdf-parse](https://npm.io/package/pdf-parse.md) ^1.1.1
- [puppeteer](https://npm.io/package/puppeteer.md) ^19.5.2
- [handlebars](https://npm.io/package/handlebars.md) ^4.7.7
- [inline-css](https://npm.io/package/inline-css.md) ^4.0.1

## 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

- 0.0.1 (latest) — 2023-01-23
- 1.0.1 (next) — 2023-01-23
- 1.0.0 — 2023-01-23

## README

# `@isneezy/pdf-generator`

`@isneezy/pdf-generator` is a powerful and versatile library that allows you to convert HTML pages, templates, and URLs
into high-quality PDF documents using Chromium and Puppeteer.

With @isneezy/pdf-generator, you can easily create PDFs with support for table of contents and various page
configurations such as page size, margins, and more. The library is designed to be easy to use and integrate into your
existing projects, making it the perfect solution for developers looking to add PDF functionality to their applications.

## Installation

To install the package, you can use npm or yarn.

```shell
npm install @isneezy/pdf-generator
```

```shell
yarn add @isneezy/pdf-generator
```

## Usage
The package exports a single class `PdfGenerator` that you can use to create a new instance of the library

```js
import PdfGenerator from '@isneezy/pdf-generator'

const instance = PdfGenerator.instance()
```

The class has the following methods:

### generate(options: Options): Promise&lt;Buffer&gt;
This method generates a PDF from the provided options and returns a promise that resolves
to a buffer with the PDF content.

The options object has the following shape:

| Option          | Type	   | Description                                                                                                                                |
|-----------------|---------|--------------------------------------------------------------------------------------------------------------------------------------------|
| goto	           | string  | URL to the HTML content/handlebars template to be converted to PDF. If provided, it takes priority over the template option.               |
| template	       | string  | HTML content/Handlebars template to be converted to PDF. If a goto option is provided, this option will be ignored.                        |
| context         | object  | The data that will be passed to the handlebars template for rendering.                                                                     |
| headerTemplate	 | string  | Handlebars template to be rendered as the page header                                                                                      |
| footerTemplate  | string  | Handlebars template to be rendered as the page footer. If a footer template is provided in the document, this option will take priority.   |
| format          | string  | Sets the paper format to be used when printing a PDF. Accepted values include: letter, legal, tabloid, ledger, a0, a1, a2, a3, a4, a5, a6. |
| landscape       | boolean | If set to true, the PDF will be generated in landscape orientation, otherwise it will be generated in portrait orientation.                |
| margin          | object  | An object with the properties top, bottom, left, right which sets the page margins. The values should be strings with units, e.g. "10mm".  |


### close(): Promise&lt;void&gt;

This method releases the resources used by the instance of the `PdfGenerator` class, it should be called after using the generate method.

```js
instance.close()
```

## Example

Here is an example of how to use the library to generate a PDF from a handlebars template and context data:

```js
import { writeFileSync } from 'fs'
import PdfGenerator from '@isneezy/pdf-generator'

const template = '<p>Hello from {{ name }}</p>'
const context = { name: 'PDF Generator' }
// Creates a new instance of PdfGenerator
const instance = PdfGenerator.instance()

// generate the PDF file
instance.generate({ template, context })
  .then(buffer => {
// write the file to disk
    writeFileSync('path/to/the/output.pdf', buffer)
  })
  .finally(instance.close)
```

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