Code Monkey home page Code Monkey logo

node-html-pdf's Introduction

node-html-pdf

a HTML to PDF converter that wraps phantomjs

image
Example Business Card
-> and its Source file

Example Receipt

var fs = require('fs');
var pdf = require('./lib');
var html = fs.readFileSync('./test/businesscard.html', 'utf8')
pdf.create(html, { filename: './businesscard.pdf', width: '50mm', height: '90mm'}, function(err, res) {
  if (err) return console.log(err);
  console.log(res);
  /*
    {
      filename: '{absolute path}/businesscard.pdf',
      pages: 1
    }
  */
});

API

Currently there is only one function

var pdf = require('html-pdf');
var callback = function(err, res){}
pdf.create(htmlString, options, callback)

Options

config = {

  // Export options
  "filename": "/tmp/html-pdf-123-123.pdf" // The file path of the file that will be written. If you want to save the file permanently, you have to pass this option.
  "directory": "/tmp"        // The directory the file gets written into if no filename is defined. default: '/tmp' 

  // Papersize Options: http://phantomjs.org/api/webpage/property/paper-size.html
  "height": "",              // allowed units: mm, cm, in, px
  "width": "",               // allowed units: mm, cm, in, px
  - or -
  "format": "A4",            // allowed units: A3, A4, A5, Legal, Letter, Tabloid
  "orientation": "portrait", // portrait or landscape

  // Page options
  "border": "0"              // default is 0, units: mm, cm, in, px
  "header": {
    "height": "45mm",
    "contents": '<div style="text-align: center;">Author: Marc Bachmann</div>'
  },
  "footer": {
    "height": "28mm",
    "contents": '<span style="color: #444;">{{page}}</span>/<span>{{pages}}</span>'
  },
  
  // File options
  "type": "pdf",             // allowed file types: png, jpeg, pdf
  "quality": "75",           // only used for types png & jpeg

  // Script options
  script: '/url'           // Absolute path to a custom phantomjs script, use the file in lib/scripts as example
  timeout: 10000           // Timeout that will cancel phantomjs, in milliseconds

}

The full options object gets converted to JSON and will get passed to the phantomjs script as third argument.
There are more options concerning the paperSize, header & footer options inside the phantomjs script.

node-html-pdf's People

Contributors

atinux avatar marcbachmann avatar

Watchers

 avatar

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.