HTML5 to PDF converter
npm install html5-to-pdf> Node module that converts HTML files to PDFs.



The PDF looks great because it is styled by HTML5 Boilerplate or Bootstrap. What? - Yes! HTML is pushed into the HTML5 template index.html. Electron renders the page and saves it to a PDF. You can customize the page by adding custom CSS and JS assets.
* Uses Puppeteer in order to get more fine-grain PDF options.
* Use async/await and ES6 - no more coffee-script
* For migration in your current code, consider using the new async functionality in your existing promise chains (see example)
npm install --save html5-to-pdf
or
npm install --global html5-to-pdf
Uses webpack and webpack-dev-server to let you see your changes live, and has the option to publish to HTML or PDF.
``javascript
const HTML5ToPDF = require("../lib")
const path = require("path")
const run = async () => {
const html5ToPDF = new HTML5ToPDF({
inputPath: path.join(__dirname, "assets", "basic.html"),
outputPath: path.join(__dirname, "..", "tmp", "output.pdf"),
templatePath: path.join(__dirname, "templates", "basic"),
include: [
path.join(__dirname, "assets", "basic.css"),
path.join(__dirname, "assets", "custom-margin.css"),
],
})
await html5ToPDF.start()
await html5ToPDF.build()
await html5ToPDF.close()
}
(async () => {
try {
await run()
console.log("DONE")
} catch (error) {
console.error(error)
process.exitCode = 1
} finally {
process.exit();
}
})()
`
---
A Typescript definition for this library can be obtained by installing @types/html5-to-pdf.
Options are passed into the constructor.
#### options.inputPath
Type: String
Required: true
Path to the input HTML
#### options.inputBody
Type: String or Buffer
Path to the input html as a String, or Buffer. If specified this will override inputPath.
#### options.outputPath
Type: String
Path to the output pdf file.
#### options.include
Type: Array
An array of strings or objects containing a type of ['css', 'js'] and a filePath pointing to the asset.
Example:
`javascript`
[
"/path/to/asset.css"
// ...
]
or
`javascript`
[
{
"type": "css",
"filePath": "/path/to/asset.css"
}
// ...
]
#### options.renderDelay
Type: Number0
Default value:
Delay in milli-seconds before rendering the PDF (give HTML and CSS a chance to load)
#### options.template
Type: Stringhtml5bp
Default value:
The template to use when rendering the html. You can choose between html5bp (HTML5 Boilerplate) or htmlbootstrap (Boostrap 3.1.1)
#### options.templatePath
Type: Stringhtml5-to-pdf/templates/#{options.template}
Default value: the
The template to use for rendering the html. If this is set, it will use this instead of the template path.
#### options.templateUrl
Type: String
The url to use for rendering the html. If this is set, this will be used for serving up the html. This will override options.templatePath and options.template
#### options.pdf
Type: Object
This object will be passed directly to puppeteer. The full list of options can be found here.
#### options.launchOptions
Type: Object
This object will be passed directly to puppeteer. The full list of options can be found here.
[ DEPRECATED ]
See options.pdf above for pdf options. Since some of these options are converted over to work with puppeteer, this is automatically done if options.pdf is left empty.
#### options.options.pageSize [COMPATIBLE]
Type: StringA4
Default value:
'A3', 'A4', 'Legal', 'Letter' or 'Tabloid'
#### options.options.landscape [COMPATIBLE]
Type: Booleanfalse
Default value:
true for landscape, false for portrait.
#### options.options.marginsType [NOT COMPATIBLE]
Type: Number0
Default value:
* 0 - default
* 1 - none
* 2 - minimum
#### options.options.printBackground [COMPATIBLE]
Type: Booleanfalse
Default value:
Whether to print CSS backgrounds.
---
To use html5-to-pdf as a standalone program from the terminal run
`sh`
npm install --global html5-to-pdf
`sh
Usage: html5-to-pdf [options]
Options:
-V, --version output the version number
-i --include
--page-size [size] 'A3', 'A4', 'Legal', 'Letter' or 'Tabloid'
--landscape If set it will change orientation to landscape from portriat
--print-background Whether to print CSS backgrounds
-t --template [template] The template to used. Defaults to html5bp.
--template-path [/path/to/template/folder] Specifies the template folder path for static assets, this will override template.
--template-url [http://localhost:8080] Specifies the template url to use. Cannot be used with --template-path.
-d --render-delay [milli-seconds] Delay before rendering the PDF (give HTML and CSS a chance to load)
-o --output
-h, --help output usage information
`
---
See Example Dockerfile. Make sure to container with
--cap-add=SYS_ADMIN.
`sh`
docker build -t local/html5-to-pdf-example -f examples/Dockerfile .
docker run --rm -i --cap-add=SYS_ADMIN local/html5-to-pdf-example
Refer to puppeteer documentation.
---
_(like on a server without X)_:
html5-to-pdf uses Puppeteer, which Google Chrome Headlessly, which in turn relies on an X server to render the pdf.
If for whatever reason it can't find a running X server, it will silently fail.
To fix, just run whatever display server you prefer (that's implementing X).
If you have no X server, chances are you are running on a headless server anyway, in which case there is no point in running a full-blown GUI (that's not facing any users).
You can instead use Xvfb, a virtual frame buffer.
#### Depedency Installation
`sh`(might need sudo)
apt-get install -y libgtk2.0-0 libgconf-2-4 libasound2 libxtst6 libxss1 libnss3 xvfb
#### Environment Setup
`sh`(might need sudo)
Xvfb -ac -screen scrn 1280x2000x24 :9.0 &
export DISPLAY=:9.0
#### Troubleshooting
It's ok if Xvfb can't find fonts or shows other warnings.
If Xvfb can't start, it probably thinks there's another X server running. Check that.
If there is no other X server running but Xvfb insists there is, run this:
`sh``(might need sudo)
rm /tmp/.X11-unix/X1
rm /tmp/.X1