Skip to content

abhisek-niyo/template-to-pdf

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Template to PDF

Module that converts html to a PDF file.

Support for templating

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'
  }
}

Usage

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);
  });

Saving options:

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

Passing in pdf options

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

Note

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
}

Documentation

Table of Contents

template-to-pdf

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

compileTemplate

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

generatePDF

Parameters

  • options object pdf paperSize options
  • templates array rendered html templates
  • fileName string desired file name
  • pdftkPath 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

awsUpload

Parameters

  • options object aws bucket data
  • filePath string local path to file that will be uploaded to aws bucket
  • fileName 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

saveFile

Parameters

  • tempFile string rendered html templates
  • filePath string desired file name
  • fileName 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

validateOptions

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