https://github.com/alextiley/express-html-to-pdf
Convert HTML to PDF using express, the node.js framework
https://github.com/alextiley/express-html-to-pdf
Last synced: 10 months ago
JSON representation
Convert HTML to PDF using express, the node.js framework
- Host: GitHub
- URL: https://github.com/alextiley/express-html-to-pdf
- Owner: alextiley
- Created: 2019-07-31T11:49:58.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-23T23:47:44.000Z (over 3 years ago)
- Last Synced: 2025-07-24T22:32:54.889Z (10 months ago)
- Language: TypeScript
- Size: 1.66 MB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# express-html-to-pdf
Adds support for converting HTML to PDF via express.js.
### Features
* Converts HTML to PDF by exposing a handy `pdf` method on the Express Response object.
* Uses Puppeteer under the hood to generate PDF documents. Puppeteer options are exposed through configuration.
* Automatically configured to avoid font rendering issues in Linux environments. Great for containerised environments running in docker.
### 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
At the most basic, this library adds a `pdf` method to the express response object. 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.
```js
const express = require('express');
const app = express();
app.get('/some/pdf', (req, res) => {
res.pdf(`
Hello world
`);
});
app.listen(3000);
```
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
```js
const express = require('express');
const app = express();
app.get('/some/pdf', (req, res) => {
res.pdf(`
