Code Monkey home page Code Monkey logo

html5-to-pdf's Introduction

Build Status

Node module that converts HTML files to PDFs.

The PDF looks great because it is styled by HTML5 Boilerplate or Bootstrap. What? - Yes! HTML is pushed into the HTML5 template index.html. Phantomjs renders the page and saves it to a PDF. You can even customize the style of the PDF by passing an optional path to your CSS and you can pre-process your html file before it is converted to a PDF by passing in a pre-processing function, for creating templates.

v2 (BREAKING CHANGES)

  • Uses Nightmare - an Electron based headless browser.
  • Simple API
  • No more streams

Getting started

npm install --save html5-to-pdf

Example usage

HTMLToPDF = require 'html5-to-pdf'
htmlToPDF = new HTMLToPDF {
  inputPath: './path/to/input.html',
  outputPath: './path/to/output.pdf',
}

htmlToPDF.build (error) =>
  throw error if error?
  # Done!

Options

Options are passed into the constructor.

options.inputPath

Type: String Required: true

Path to the input HTML

options.inputBody

Type: String or Buffer

Path to the input html as a String, or Buffer. If specified this will override inputPath.

options.outputPath

Type: String Required: true

Path to the output pdf file.

options.include

Type: Array<Object>

An array of objects containing a type of ['css', 'js'] and a filePath pointing to the asset.

Example:

[
  {
    "type": "css",
    "filePath": "/path/to/asset.css"
  }
  // ...
]

options.options.pageSize

Type: String Default value: A4

'A3', 'A4', 'Legal', 'Letter' or 'Tabloid'

options.options.landscape

Type: Boolean Default value: false

true for landscape, false for portait.

options.options.marginsType

Type: Number Default value: 0

  • 0 - default
  • 1 - none
  • 2 - minimum

options.options.printBackground

Type: Boolean Default value: false

Whether to print CSS backgrounds.

options.renderDelay

Type: Number Default value: 0

Delay in milli-seconds before rendering the PDF (give HTML and CSS a chance to load)

options.template

Type: String Default value: html5bp

The template to use when rendering the html. You can choose between html5bp (HTML5 Boilerplate) or htmlbootstrap (Boostrap 3.1.1)

options.templatePath

Type: String Default value: the html5-to-pdf/templates/#{options.template}

The template to use for rendering the html. If this is set, it will use this instead of the template path.

options.templateUrl

Type: String

The url to use for rendering the html. If this is set, this will be used for serving up the html. This will override options.templatePath and options.template

CLI interface

Installation

To use html5-to-pdf as a standalone program from the terminal run

npm install --global html5-to-pdf

Usage

Usage: command [options] <path/to/html-file-path>

Options:

  -h, --help                   output usage information
  -V, --version                output the version number
  -i --include <path>..<path>  path to either a javascript asset, or a css asset
  --page-size [size]           'A3', 'A4', 'Legal', 'Letter' or 'Tabloid'
  --margin-type [n]            Specify the type of margins to use: 0 - default, 1 - none, 2 - minimum
  --landscape                  If set it will change orientation to landscape from portriat
  --print-background           Whether to print CSS backgrounds
  -t --template [template]     The template to used. Defaults to html5bp.
  -d --render-delay [millis]   Delay before rendering the PDF (give HTML and CSS a chance to load)
  -o --output <path>           Path of where to save the PDF

html5-to-pdf's People

Contributors

alanshaw avatar alexwhitman avatar finnp avatar fnogatz avatar mauvm avatar ncrazed avatar neurobashing avatar peterdemartini avatar rodmoreno avatar shinnn avatar tn1ck avatar

Watchers

 avatar  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.