A Node HTML to PDF converter using a the Java flyingsaucer library.
npm install html-to-pdfhtml-to-pdf
===========
A Node wrapper around a Java HTML to PDF converter to allow users to make HTML templates for automated generation of PDFs. While often in Node this is achieved through phantomjs, for larger PDF reports each report being a large image can make the file very large very quickly. This aims to fix that problem by using the Java library flyingsaucer to do the heavy lifting and providing a Node wrapper around it for ease of use with web applications.
sh
npm install html-to-pdf
`
How To Use html-to-pdf ##
To use html-to-pdf in your Node application, just require it:
`js
var htmlToPdf = require('html-to-pdf');
`
$3
You can use convertHTMLFile to convert HTML files to PDFs. Simply provide a path to a source HTML file and a path to a destination PDF file for conversion.
`js
htmlToPdf.convertHTMLFile('path/to/source.html', 'path/to/destination.pdf',
function (error, success) {
if (error) {
console.log('Oh noes! Errorz!');
console.log(error);
} else {
console.log('Woot! Success!');
console.log(success);
}
}
);
`
$3
You can use convertHTMLString to turn a string of HTML into a PDF file. Simply pass in a string of HTML and a path to a destination PDF file for conversion. This is useful for using other templating languages (like Jade or Mustache) where you can convert the template into HTML and then use this to convert it to a PDF.
`js
var html = ...; //Some HTML String from code abovehtmlToPdf.convertHTMLString(html, 'path/to/destination.pdf',
function (error, success) {
if (error) {
console.log('Oh noes! Errorz!');
console.log(error);
} else {
console.log('Woot! Success!');
console.log(success);
}
}
);
`
$3
If you want to see the output you can set debug mode to true to see the output of the PDF Renderer (debug is false by default):
`js
htmlToPdf.setDebug(true);
`$3
If using a particular encoding type is important to getting the output you want, you can set the encoding types for both the input and output:
`js
htmlToPdf.setInputEncoding('UTF-8');
htmlToPdf.setOutputEncoding('UTF-8');
``https://github.com/flyingsaucerproject/flyingsaucer
Flyingsaucer has some contraints on CSS styling and some extra styling features to define PDF parameters. html-to-pdf also uses these features by extension. Other than these two contraints, html-to-pdf is very lightweight.
Also, html-to-pdf creates a temporary file while converting. While it will clean up after itself, this file creation/deletion can trigger things like forever to restart the server. Adding the temp file to the ignore list should prevent this from happening.