Module that converts html to a PDF file.
Currently there is support for Handlebars, Mustache, EJS, HAML, PUG.
Make sure you change the templateType
to the template you are using.
var options = {
templateOptions: {
template: '<p>{{basic}}</p>',
templateData: { basic: 'hello world' },
templateType: 'Handlebars'
},
aws: {
s3: true,
bucket: 'pdf-err/meow/baby'
}
}
Download and install pdftk
. https://www.pdflabs.com/tools/pdftk-server/
npm install html-template-pdf
var templateToPdf = require('html-template-pdf')
var options = {
html: "<div><p>hello der</p></div>",
fileName: 'howdycolton.pdf',
filePath: '/Users/myname/Desktop/baaay/'
}
templatetoPdf(options)
.then(function(resp){
console.log(resp);
})
.catch(function(err){
console.log(err);
});
Save to file System
fileName: 'howdycolton.pdf',
filePath: '/Users/myname/Desktop/baaay/'
Return a Buffer
buffer = true
Save to s3
aws: {
s3: true,
bucket: 'pdf-err/meow/baby'
}
Requires AWS credentials in ~/.aws/credentials
.
[default]
aws_access_key_id = YOURACCESSKEYID
aws_secret_access_key = YOURSECRETACCESSKEY
We use the phantom-html-to-pdf library for pdf generation.
They accept a list of arguments for the pdf that you could optionally pass in.
Basic pdfOPtions
var options = {
html: "<div><p>hello der</p></div>",
fileName: 'howdycolton.pdf',
filePath: '/Users/myname/Desktop/baaay/'
pdfOptions: {
orientation: 'landscape',
format: "letter",
margin: '0px'
}
}
More Papersize options: http://phantomjs.org/api/webpage/property/paper-size.html
If on MAC OSX 10.11 use this https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/pdftk_server-2.02-mac_osx-10.11-setup.pkg version of pdftk as there have been issues reported with 10.11.
By default we are using the command line call for pdftk, but you may pass in an optional path if you are having trouble rendering a pdf.
var options = {
html: "<div><p>hello der</p></div>",
fileName: 'howdycolton.pdf',
pdftkPath: '/usr/local/bin/pdftk',
buffer: true
}
Parameters
data
object an object that contains the template/html, fileName, and AWS configurations.
Examples
var templateToPDF = require('template-to-pdf');
var data = {
fileName: "newFile.pdf",
pdfOptions: {
orientation: 'landscape',
format: "letter",
margin: '0px'
},
templateOptions: {
template: "h1 #{message}",
templateData: [{message: "Hello!"}],
templateType: "pug"
},
html: "<h1> Hello! </h1>" #alternative to templateOptions
aws: {
s3: true,
bucket: "pdf-err/"
}
}
templateToPDF(data)
.then(function (downloadLink) {
console.log(downloadLink);
})
.catch(function (error) {
console.log(error);
})
Returns object an object that contains the download link for the pdf generated
Parameters
options
string object that contains the template, template data, and template type
Examples
var compileTemplate = require('./lib/compileTemplate');
var options = {
template: "h1 #{message}",
templateData: [{message: "Hello!"}],
templateType: "pug"
}
compileTemplate(options)
.then(function (renderedTemplates) {
console.log(renderedTemplates);
})
.catch(function (error) {
console.log(error);
})
Returns string path to the file saved
Parameters
options
object pdf paperSize optionstemplates
array rendered html templatesfileName
string desired file namepdftkPath
string path to pdftk
Examples
var generatePDF = require('./lib/generatePDF');
var options = {
orientation: 'landscape',
format: "letter",
margin: '0px'
};
var templates = [];
var fileName = "newFile.pdf";
var pdftkPath = "usr/local/bin/pdftk";
generatePDF(options, templates, fileName, pdftkPath)
.then(function (url) {
console.log(url);
})
.catch(function (error) {
console.log(error);
})
Returns string path to the file generated
Parameters
options
object aws bucket datafilePath
string local path to file that will be uploaded to aws bucketfileName
string desired file name
Examples
var awsUpload = require('./lib/awsUpload');
var options = {
s3: true,
bucket: 'pdf-err/meow/baby'
}
var filePath = "./tmp/tempFile.pdf"
var fileName = "newFile.pdf"
awsUpload(options, filePath, fileName)
.then(function (url) {
console.log(url);
})
.catch(function (error) {
console.log(error);
})
Returns string url download link of pdf file in aws s3 bucket
Parameters
tempFile
string rendered html templatesfilePath
string desired file namefileName
string path to pdftk
Examples
var saveFile = require('./lib/saveFile');
var tempFile = "./tmp/tempFile.pdf";
var filePath = "./pdf-files/";
var fileName = "newFile.pdf";
saveFile(options, templates, fileName, pdftkPath)
.then(function (newFilePath) {
console.log(newFilePath);
})
.catch(function (error) {
console.log(error);
})
Returns string path to the file saved
Parameters
options
string object that contains the template, template data, and template type
Examples
var validateOptions = require('./lib/validateOptions');
var options = {
fileName: "newFile.pdf",
pdfOptions: {
orientation: 'landscape',
format: "letter",
margin: '0px'
},
templateOptions: {
template: "h1 #{message}",
templateData: [{message: "Hello!"}],
templateType: "pug"
},
html: "<h1> Hello! </h1>" #alternative to templateOptions
aws: {
s3: true,
bucket: "pdf-err/"
}
}
validateOptions(options)
.then(function (newFilePath) {
console.log(newFilePath);
})
.catch(function (error) {
console.log(error);
})
Returns string path to the file saved