8.11.2 • Published 3 months ago

express-dom-pdf v8.11.2

Weekly downloads
7
License
MIT
Repository
github
Last release
3 months ago

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.

8.11.2

3 months ago

8.11.1

3 months ago

8.11.0

3 months ago

8.10.2

6 months ago

8.10.1

7 months ago

8.10.4

6 months ago

8.10.3

6 months ago

8.10.6

6 months ago

8.10.5

6 months ago

8.10.0

8 months ago

8.9.0

8 months ago

8.9.2

8 months ago

8.9.1

8 months ago

8.8.0

9 months ago

8.6.0

9 months ago

8.5.2

1 year ago

8.5.1

1 year ago

8.5.0

1 year ago

8.4.0

2 years ago

8.1.0

2 years ago

8.3.0

2 years ago

8.2.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

3.0.1

2 years ago

8.0.1

2 years ago

8.0.0

2 years ago

3.0.0

2 years ago

4.0.0

2 years ago

1.3.0

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.3

4 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.0

7 years ago

0.4.3

7 years ago

0.4.2

7 years ago

0.4.1

7 years ago

0.4.0

7 years ago

0.3.0

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.0

8 years ago