npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-dom-pdf

v8.11.2

Published

PDF plugin for express-dom

Downloads

65

Readme

express-dom-pdf

PDF plugin for express-dom

Optionally converts pdf using ghostscript with presets.

Usage

See express-dom documentation about how web pages are prerendered.

const dom = require('express-dom');
const pdf = require('express-dom-pdf');
const express = require('express');
const app = express();

// unconditionally outputs a pdf

app.get('*.html', dom(pdf({
  presets: {
    // merged with pdf.presets
  },
  policies: {
    // merged with pdf.policies
  }
  plugins: ['custom'] // these plugins are added before 'pdf' plugin
})).route((phase, req, res) => {
  if (phase.visible) {
    phase.settings.pdf(req.query.pdf);
    // no need to keep the parameter during prerendering
    phase.location.searchParams.delete('pdf');
  } else {
    res.set('Content-Security-Policy', "default 'self' data:");
  }
}), express.static('public/'));

It is also possible to get a response stream directly without express, in scenarios where pdf generation takes a long time:

// res: { statusCode, headers } is a passthrough stream
const res = dom(pdf(opts))({
  url: 'http://localhost/custom.html',
  body: '<html>...</html>'
});

Presets

Depends on the value of the phase.settings.preset parameter. If not set, the "default" preset is used. If a preset is unknown, an error with error.statusCode of 400 is thrown.

Ghostscript can produce a pdf/x-3 using this kind of preset:

pdf.presets.fogra39l = {
 quality: 'printer',
 scale: 4,
 icc: 'ISOcoated_v2_300_eci.icc',
 condition: 'FOGRA39L',
 others: [ "-dColorImageResolution=600" ]
};

See also pdflib documentation.

Options

These settings can be changed globally, or for each instance.

  • timeout: max time to wait for page load to finish (default 30000)
  • pdfx: file path for the pdfx postscript template
  • iccdir: dir path for the icc profiles (icc-profiles debian package installs /usr/share/color/icc)
  • presets: map of presets
  • plugins: load these dom plugins before media and pdf plugins
  • policies: the csp for express-dom online phase

Presets accept these options:

  • quality: false (boolean) or screen|ebook|prepress|printer (string)
  • scale: device scale factor, changes value of window.devicePixelRatio
  • icc: profile file name found in iccdir (required for pdf/x-3)
  • condition: output condition identifier (required for pdf/x-3)
  • others: additional gs arguments, see ghostscript.
  • pageCount: boolean, sets X-Page-Count HTTP response header. defaults to true for printer preset.

Styling

A minimal stylesheet:

@media only print {

 @page {
  size: 210mm 297mm;
  margin:1cm;
 }
 html, body {
  padding: 0;
  margin: 0;
 }
 body > .page {
  page-break-inside: avoid;
  page-break-after: always;
 }
}

Autobreak (experimental)

Sample code of how to break pages at the DOM level, before printing, is available in test/public/autobreak.html (to actually see the result, just serve test/public and open autobreak.html).

This is more powerful than print breaks, because it allows one to style the resulting layout.

fonts

On Debian, install "fonts-recommended" package.

System fonts rendering can have some bugs, especially regarding emojis or color fonts.