Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/myunisoft/html-pdf

Node lib to converts HTML with property binding or url to PDF files.
https://github.com/myunisoft/html-pdf

browser nodejs pdf puppeteer

Last synced: about 8 hours ago
JSON representation

Node lib to converts HTML with property binding or url to PDF files.

Awesome Lists containing this project

README

        


HTML-PDF


Node lib to converts HTML with property binding or url to PDF files.


npm version
license
size

## 🚧 Requirements

- [Node.js](https://nodejs.org/en/) version 14 or higher

## 🚀 Getting Started

This package is available in the Node Package Repository and can be easily installed with [npm](https://doc.npmjs.com/getting-started/what-is-npm) or [yarn](https://yarnpkg.com)

```bash
$ npm i @myunisoft/html-to-pdf
# or
$ yarn add @myunisoft/html-to-pdf
```

## 📚 Usage

```js
async function main() {
const browser = await initBrowser();
let readable;

try {
readable = Readable.from(generatePDF(browser, [{ content: html }], pdfOptions));
}
finally {
await terminateBrowser(browser);
}

return readable;
}

main().catch(console.error);
```

## API

initBrowser(options?: PuppeteerLaunchOptions): Promise< Browser >

```js
import { initBrowser } from "@myunisoft/html-to-pdf";

const browser = await initBrowser();
```

Options payload is described by the following TypeScript interface:
```ts
import {
LaunchOptions,
BrowserLaunchArgumentOptions,
BrowserConnectOptions,
Product
} from "puppeteer";

type PuppeteerLaunchOptions = LaunchOptions & BrowserLaunchArgumentOptions & BrowserConnectOptions & {
product?: Product;
extraPrefsFirefox?: Record;
}
```

generatePDF(browser: Browser, files: pdfFile[], options?: PuppeteerPDFOptions): AsyncIterableIterator< Buffer >

`files` and `options` arguments are described with the following TypeScript interface/type:
```ts
interface pdfFile {
content?: string,
url?: string
}

type PuppeteerPDFOptions = PDFOptions & { paginationOffset?: number };
```

```js
import * as pdf from "@myunisoft/html-to-pdf";

const browser = await pdf.initBrowser();

const pdfOptions = {};
const content = "..HTML HERE..";

const readable = Readable.from(
pdf.generatePDF(browser, [{ content }], pdfOptions)
);
// Do the work with readable (pipe to an http response for example).
```

terminateBrowser(browser: Browser): Promise< void >

```js
import * as pdf from "@myunisoft/html-to-pdf";

const browser = await pdf.initBrowser();

try {
// DO THE WORK HERE
}
finally {
await pdf.terminateBrowser(browser);
}
```

## Contributors ✨

[![All Contributors](https://img.shields.io/badge/all_contributors-3-orange.svg?style=flat-square)](#contributors-)

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):



Nicolas Hallaert

💻 📖 ⚠️

Gentilhomme

🛡️ 👀 📖

PierreD

💻

## License
MIT