Giter VIP home page Giter VIP logo

template-to-pdf's Introduction

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
}

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

template-to-pdf's People

Contributors

absalas avatar dvictory avatar sdoty-clover avatar txssseal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

template-to-pdf's Issues

create pdf with handlebars from separate html file.

I have html file in my folder. I want to convert it to pdf using handlebars.
I want to pass object that will be printed into my dynamic html file through handlebars.

I am unable to create options variable.

Potential command injection vulnerability in template-to-pdf

Hi,

Thanks for developing this great npm package! We find a potential command injection vulnerability from it.
The bug is introduced because package-exported method fails to sanitize the fileName parameter and let it flow into a sensitive command execution API.

Here is the proof of concept.

var templateToPdf = require('template-to-pdf')

var options = {
      html: "<div><p>hello der</p></div>", 
      fileName: 'howdycolton.pdf | touch rce', 
      filePath: '/Users/myname/Desktop/baaay/' 
    
}
 
templateToPdf(options)
    .then(function(resp){
            console.log(resp);
          
    })
    .catch(function(err){
            console.log(err);
          
    });

local write to existing folder fails

I can't speak to aws uploading (great feature which I will eventually use!)...

But when I specify filePath for local write, the write fails if the directory already exists. If you're using fs & promises, should be a simple fix to check if the folder exists and bypass the mkdir if so. Sorry I don't have time right now to look at your code and point out the line.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.