Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/myunisoft/html-pdf
- Owner: MyUnisoft
- License: mit
- Created: 2021-12-14T01:27:47.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-11-27T14:26:06.000Z (12 months ago)
- Last Synced: 2023-11-28T14:53:03.521Z (12 months ago)
- Topics: browser, nodejs, pdf, puppeteer
- Language: TypeScript
- Homepage:
- Size: 495 KB
- Stars: 4
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
HTML-PDF
Node lib to converts HTML with property binding or url to PDF files.## 🚧 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