htmltopdf
Version:
A node package to convert HTML to PDF, it supports sending in straight HTML and convert to PDF. It also supports sending in template in handlebars format with a data string and convert that to PDF
118 lines (96 loc) • 3.14 kB
Markdown
<h1>HTML TO PDF</h1>
author: Ólafur Aron Jóhannsson<br>
email: olafurjohannss@gmail.com<br>
website: https://www.olafuraron.is<br>
Convert HTML sites to PDF with this simple package.
https://www.npmjs.com/package/htmltopdf
<strong>npm install htmltopdf</strong>
<h2>HTML to PDF converter.</h2>
<p>
It converts raw HTML string data or a stream from a file.
</p>
<p>
The gist of it is that there are two methods
<ul>
<li>createFromHtml (html, pdfName, callback)</li>
<li>createFromTemplateData (html, htmlData, pdfName, callback)</li>
</ul>
</p>
<p>
Both of these methods are what make up this package, the callback is in the classic Node style, basically a function that has two parameters, <strong>callback (error, success)</strong> which do what they imply, if success then no error, if not success then the error field is populated with the information needed to correctify.
</p>
<h2>Usage for <small>createFromHtml</small></h2>
<p>
The former method <i>createFromHtml(html, pdfName, callback)</i> takes in an HTML string and the name of the PDF to be created and the previously explained callback.
<div>
<pre>
htmltopdf.createFromHtml("<html><h1>html</h1></html>", "pdfName.pdf", function (err, success) {
... do stuff
});
</pre>
</div>
</p>
<p>
If you want to read in a file just use the <i>fs</i> module
<pre>
fs.readFile('file.html', function (err, data) {
htmltopdf.createFromHtml(data, "pdfName.pdf", function (err, success) {
... do stuff
});
});
</pre>
</p>
<h2>Usage for <small>createFromTemplateData</small></h2>
<p>
The latter method <i>createFromTemplateData(html, htmlData, pdfName, callback)</i> uses templates to create the PDF. You send in the template HTML, such as <pre><html><h1>{{data}}</h1></html></pre>
<span>
where data is a JS object
</span>
<pre>{{'data':'test'}}</pre>
<span>The output would be</span>
<pre>
<html><h1>test</h1></html>
</pre>
<h3>Example:</h3>
<pre>
htmltopdf.createFromTemplateData("<html><h1>{{data}}</h1></html>", "{{'data':'test'}}", "pdfName.pdf", function (err, success) {
if (success) {
... do stuff
}
});
</pre>
</p>
<h3>Example using fs module</h3>
<h4>See test.js for examples</h4>
<pre>
var htmltopdf = require('./htmltopdf'),
fs = require('fs');
if (process.argv.length > 2) {
var pdfName = process.argv[2];
var html = process.argv[3];
if (pdfName.indexOf('.pdf') > 0) {
var htmlData = process.argv[4];
// If htmlData is valid
if (!!htmlData) {
htmltopdf.createFromTemplateData(html, htmlData, pdfName, function (err, success) {
if (success) {
console.log('Success creating ' + pdfName);
}
else {
console.log('Could not create PDF', err);
}
});
}
else {
htmltopdf.createFromHtml(html, pdfName, function (err, success) {
if (success) {
console.log('Success creating ' + pdfName);
}
else {
console.log('Could not create PDF', err);
}
});
}
}
}
</pre>