0.3.0 • Published 6 years ago

pdfy v0.3.0

Weekly downloads
-
License
MIT
Repository
-
Last release
6 years ago

pdfy

Build Status NPM downloads Dependencies

Creates PDF documents from HTML templates using Headless Chrome.

Install

yarn

$ yarn add pdfy

npm

$ npm install --save pdfy

Usage

Basic

const pdfy = require('pdfy');

pdfy(`<h1> Hello {{user}} </h1>`, { user: 'James' })
  .then(buff => {
     //=> <Buffer 32 40 2a ... >
  })

Saving files

const pdfy = require('pdfy');
const options = { path: 'example.pdf' } // Just add the path in options

pdfy(`<h1> Hello {{user}} </h1>`, { user: 'James' }, options)
//=> <Promise<Buffer>>

Bootstrap styles

See example

const pdfy = require('pdfy');
const axios = require('axios');

const bootstrapUrl = 'https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap.min.css';
const { data : bootstrap } = await axios.get(bootstrapUrl);

const template = `
   <html>
      <head>
        <style>{{ bootstrap }}</style>
      </head>
      <body>
        <div class="container">
          <h1>Hello {{ user }}</h1>

          <button type="button" class="btn btn-primary">Primary</button>
          <button type="button" class="btn btn-secondary">Secondary</button>
          <button type="button" class="btn btn-success">Success</button>

        </div>
      </body>
    </html>`;

pdfy(template, { user: 'James', bootstrap });
//=> <Promise<Buffer>>

API

pdfy(template, data, options)

Returns a <Promise<Buffer>> with the generated PDF.

template

Type: string

HTML template string.

data

Type: object

Data that will be injected on the template.

options

Type: object

See puppeteer options

// Default options
{
  format: "A4",
  path: "",
  margin: { top: '50px', bottom: '50px' }
}

Related

License

MIT © Ruan Martinelli

0.3.0

6 years ago

0.2.0

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago