pdfy
v0.3.0
Published
Creates PDF documents from HTML templates using Headless Chrome
Downloads
4
Maintainers
Readme
pdfy
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
// Default options
{
format: "A4",
path: "",
margin: { top: '50px', bottom: '50px' }
}
Related
License
MIT © Ruan Martinelli