Skip to content
This repository has been archived by the owner on Aug 23, 2020. It is now read-only.

jaumard/trailpack-pdf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

trailpack-pdf

📦 Trailpack to generate PDF from routes or templates for Trails.js project

NPM version Build status Dependency Status Code Climate Beerpay Beerpay

This Trailpack is based on phantomjs and phantomjs-node to generate PDF.

It can also generate page into image (png, jpeg) instead of PDF.

Install

With yo :

npm install -g yo generator-trails
yo trails:trailpack trailpack-pdf

With npm (you will have to create config file manually) :

npm install --save trailpack-pdf

Configure

Load the trailpack to the main config

// config/main.js
module.exports = {
  packs: [
    // ... other trailpacks
    require('trailpack-pdf')
  ]
}
// config/pdf.js
module.exports = {
  /**
   * Phantom launch options, default to empty
   */
  options: ['--ignore-ssl-errors=yes', '--load-images=no'],

  /**
   * Global page settings, default to empty
   * Example : javascriptEnabled to enable/disable javascript support on the page, userAgent...
   */
  pageSettings: {javascriptEnabled:true},

  /**
   * Global page properties, default to empty
   * Example : page size, header, footer...
   */
  pageProperties: {
    paperSize: (phantom) => {
      return {
        format: 'A4',
        header: {
          height: '1.5cm',
          contents: phantom.callback(function (pageNum, numPages) {
            return '<h1>Header <span style=\'float:right\'>' + pageNum + ' / ' + numPages + '</span></h1>'
          })
        },
        footer: {
          height: '1.5cm',
          contents: phantom.callback(function (pageNum, numPages) {
            return '<h1>Footer <span style=\'float:right\'>' + pageNum + ' / ' + numPages + '</span></h1>'
          })
        }
      }
    }
  }
}

Usage

This Trailpack expose a service to generate PDF, you can call it like this under controller/services/policies :

From route

/** 
 * Generate PDF from a Trails route, or route path
 * You can override global page settings and properties, these params are optional
**/
this.app.services.PdfService.generateFromRoute('/', '/path/to/my/file.pdf', {javascriptEnabled: true}, {pageSize: {format: 'A4'})
.then(() => {
  //pdf generated
})
.catch(err => this.log.error(err))

From URL

/** 
 * Generate PDF from an URL
 * You can override global page settings and properties, these params are optional
**/
this.app.services.PdfService.generateFromUrl('http://google.fr', '/path/to/my/file.pdf', {javascriptEnabled: true}, {pageSize: {format: 'A4'})
.then(() => {
  //pdf generated
})
.catch(err => this.log.error(err))

From HTML or Template engine (jade, ejs...)

const path = 'html.pdf'
const template = 'index.jade'
const fn = jade.compile(fs.readFileSync(template))
this.app.services.PdfService.generateFromHtml(fn(), path).then(status => {
  //pdf generated
}).catch(err => done(err))

Generate image instead of PDF

Use same code as above but change the extension to .png or .jpeg

Contributing

We love contributions! In order to be able to review your code efficiently, please keep the following in mind:

  1. Pull Requests (PRs) must include new and/or updated tests, and all tests must pass.
  2. Use eslint! See the eslintConfig in package.json.
  3. Please reference the relevant issue in your Pull Request.

License

MIT

Support on Beerpay

Hey dude! Help me out for a couple of 🍻!

Beerpay Beerpay

About

📦 Trailpack to generate PDF from routes or templates for Trails.js project

Resources

License

Stars

Watchers

Forks

Packages

No packages published