Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ministryofjustice/express-pug-pdf

Package Overview
Dependencies
Maintainers
4
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ministryofjustice/express-pug-pdf

Generates PDF from PUG templates in Express routes

  • 1.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
4
Created
Source

express-pug-pdf

npm NPM GitHub issues npm

Serve PDF documents in express generated from a Pug template

Installation

npm install @ministryofjustice/express-pug-pdf --save

Usage

Specify the location of your views directory

const pugPdf = require('@ministryofjustice/express-pug-pdf')

app.use(pugPdf({ views: path.join(__dirname, 'views') }))

Render a PDF from a pug template by specifying a template name and passing the data. In this example, the template views/helloWorld.pug will be used

app.use('/pdf', (req, res) => {
    res.pugpdf('helloWorld', { message: 'Hello World!' });
})

Using pug templates from the views directory

html
  body
    h1= message

Customising the output

To set the filename for the PDF when downloaded, pass the options object. The default filename is document.pdf

app.use('/pdf', (req, res) => {
    res.pugpdf('helloWorld', { message: 'Hello World!' }, { filename: 'helloWorld.pdf' });
})

To customise the PDF document, pass additional pdfOptions. The PDF creation uses https://www.npmjs.com/package/html-pdf. pdfOptions are passed through to html-pdf

const options = {
  filename: 'helloWorld.pdf',
  pdfOptions: {
    format: 'A4',
    border: {
      top: '40px',
      bottom: '20px',
      left: '40px',
      right: '20px',
    },
  },
}

app.use('/pdf', (req, res) => {
    res.pugpdf('helloWorld', { message: 'Hello World!' }, options);
})

How it works

express-pug-pdf uses Pug to render the Pug template into HTML. Then it passes the HTML to html-pdf to generate the PDF. The PDF is returned in the response as binary data with content type application/pdf

CSS

html-pdf uses PhantomJS to generate the PDF. PhantomJS needs to be able to see any stylesheets linked in your template. This means using an absolute url

doctype html
html(lang="en")
  head
    link(href= "http://domain/path/styles.css", media="print", rel="stylesheet", type="text/css")

  body

    block content

      div
        h1.myStyle
            A styled Heading

Or set the host (and port in dev environments) dynamically from environment config

link(href= "#{domain}/path/styles.css", media="print", rel="stylesheet", type="text/css")

Note that PhantomJS will use "print" media CSS rules when rendering PDF. Instead of @page CSS rules, use the PhantomJS paperSize options to control margins - http://phantomjs.org/api/webpage/property/paper-size.html

Example

run node examples/helloWorld/index.js then browse to http://localhost:3001/pdf

Keywords

FAQs

Package last updated on 20 Jun 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc