Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alextiley/convert-html-to-pdf
Convert HTML to PDF
https://github.com/alextiley/convert-html-to-pdf
Last synced: 22 days ago
JSON representation
Convert HTML to PDF
- Host: GitHub
- URL: https://github.com/alextiley/convert-html-to-pdf
- Owner: alextiley
- Created: 2019-08-04T10:15:08.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T06:14:07.000Z (about 2 years ago)
- Last Synced: 2024-12-06T21:42:51.267Z (about 1 month ago)
- Language: TypeScript
- Size: 1.25 MB
- Stars: 12
- Watchers: 3
- Forks: 6
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# html-to-pdf
A library for converting HTML with CSS to PDF.
### Dependencies
This library uses Puppeteer to generate PDF's.
Some systems may work out of the box, others may not. To allow Puppeteer to spin up correctly, you may need to install some additional system dependencies.
For pointers on how to make puppeteer work in your environment, see https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md#running-puppeteer-in-docker
### Basic usage
This library exports a method allowing conversion of HTML to PDF. When invoked with a valid HTML template as the first argument, a Buffer is returned containing a PDF document, along with the `application/pdf` mime type. The second argument is a configuration object, allowing customisation of Puppeteer, or the output of the PDF.
```javascript
const HTMLToPDF = require('convert-html-to-pdf').default;const htmlToPDF = new HTMLToPDF(`
Hello world
`);
htmlToPDF.convert()
.then((buffer) => {
// do something with the PDF file buffer
})
.catch((err) => {
// do something on error
});
```Or, if using async await:
```javascript
const HTMLToPDF = require('convert-html-to-pdf').default;const getPDF = async () => {
const htmlToPDF = new HTMLToPDF(`
Hello world
`);
try {
const pdf = await htmlToPDF.convert();
// do something with the PDF file buffer
} catch (err) {
// do something on error
}
};
```For PDF's that should contain imagery or web fonts, it's recommended to base 64 encode any assets and embed within the markup using a data URI. This will yield faster results. If you'd rather request assets over HTTP, you can do this by enabling the `waitForNetworkIdle` configuration option.
##### Example of an asset encoded as base 64
```javascript
const HTMLToPDF = require('convert-html-to-pdf').default;
const htmlToPDF = new HTMLToPDF(`
`);
htmlToPDF.convert()
.then((buffer) => {
// do something with the PDF file buffer
})
.catch((err) => {
// do something on error
});
```##### Example of an asset loaded over HTTP
```javascript
const HTMLToPDF = require('convert-html-to-pdf').default;const htmlToPDF = new HTMLToPDF(`
`, {
waitForNetworkIdle: true,
});
htmlToPDF.convert()
.then((buffer) => {
// do something with the PDF file buffer
})
.catch((err) => {
// do something on error
});
```### Configuration
| property | type | description |
|--------------------|-------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| browserOptions | LaunchOptions | The Puppeteer launch option configuration. See https://pptr.dev/#?product=Puppeteer&version=v1.19.0&show=api-puppeteerlaunchoptions for a full list. |
| pdfOptions | PDFOptions | The Puppeteer PDF option configuration. See https://pptr.dev/#?product=Puppeteer&version=v1.19.0&show=api-pagepdfoptions for a full list. |
| waitForNetworkIdle | boolean | By default, we assume the PDF document supplied will not contain requests over HTTP (for fastest possible PDF generation). Typically you can embed resources by base64 encoding as data URI's. If you have HTML that pulls assets or other resources over HTTP, set this to `true`. |### Example Dockerfile
If you're using this in a production Debian environment (e.g. Docker), here's an extract from a Dockerfile with the necessary dependencies needed to get up and running.
```Dockerfile
FROM node:10.15.3-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends gconf-service libasound2 libatk1.0-0 libatk-bridge2.0-0 libc6 libcairo2 libcups2 \
libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 \
libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxi6 libxrandr2 libxrender1 \
libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget libxft-dev libfreetype6 \
&& rm -rf /var/lib/apt/lists/*
```