html-to-pdf-converter
HTML to PDF converter with support for HEADERS, FOOTERS and page numbers.
Using headless chrome via puppeteer and than modifying generated pdf via HummusJS to add headers and footers with page numbers
Install
npm install html-to-pdf-converter
Usage
All fonts that are used inside passed html document need to be installed on the machine where the conversion is going to happen.
Headers and footers could be generated by passing either as a separate html document or as a single line that can be customized to left
, center
and right
sections.
There are two special placeholders {page}
and {pages}
that could be used inside your document footers and headers, which are going to be replaced by current page number and total pages count respectively.
mkCompoundPdf
accepts array of html documents that would be concatenated. Total page count is going to be the sum of the final generated pdf.
All documents had to be proper HTML documents.
For usage checkout ./src/compound-pdf.ts
file. For JS strip all types.
// Initialize converter, puppeteer browser launch options could be passed in// When finished don't forget to release resources// await converter.destroy() // Converter signature
Roadmap
- add html headers
- add text headers
- document API
- provide more examples using
javascript
- load document from URL, wait for all resources do be loaded (css, js, images, fonts, etc.), wait for javascript execution and than generate PDF
- setup CI
- cover with tests
- add performance tests
- option to configure reusing chrome instances
- use puppeteer header/footer template only when there is no need for different headers/footers per different pages
How to run tests
Docker is used to run tests due to minor difference in generated pdfs across various environments.
Build container:
docker build -t html-to-pdf-converter .
To run tests (note that this will give rw rights to docker container to projects folder):
docker-compose run --rm converter npm run test
In watch mode:
docker-compose run --rm converter npm run test:watch
If pdf comparing tests would fail, an image diff would be generated inside output
folder.