|
1 |
| -# Template renderer |
| 1 | +# template-renderer |
2 | 2 |
|
3 |
| -Creating PDF documents is a common task in software development. This repository shows how to create PDF's from HTML templates using three remarkable open source projects. It shows a free and open source alternative to commercial software like Aspose.Pdf or Exstream. |
4 |
| - |
5 |
| -## General information |
6 |
| - |
7 |
| -### Puppeteer |
8 |
| - |
9 |
| -An open source Node.js library providing control to Chrome or Chromium used for automated testing and creating PDF's. Puppeteer is maintained by the Chrome DevTools team.\ |
10 |
| -<https://pptr.dev>\ |
11 |
| -<https://github.com/puppeteer/puppeteer>\ |
12 |
| -License: See project, Apache License 2.0 at time of writing |
13 |
| - |
14 |
| -### mustache.js |
15 |
| - |
16 |
| -An open source javascript library to render mustache templates. The [mustache template syntax](https://github.com/janl/mustache.js#templates) is explained in the [mustache.js github repository](https://github.com/janl/mustache.js#templates). The mustache template engine is [widely available](https://mustache.github.io).\ |
17 |
| -License: See project, MIT License at time of writing |
18 |
| - |
19 |
| -### Paged.js |
20 |
| - |
21 |
| -An open source library to paginate HTML content for printing to PDF. This library is used to design books with HTML and CSS.\ |
22 |
| -<https://pagedjs.org>\ |
23 |
| -<https://github.com/pagedjs/pagedjs>\ |
24 |
| -<https://ashok-khanna.medium.com/beautiful-pdfs-from-html-9a7a3c565404>\ |
25 |
| -<https://www.adamhyde.net/some-pagedjs-info>\ |
26 |
| -License: See project, MIT license at time of writing |
27 |
| - |
28 |
| -### Source code |
29 |
| - |
30 |
| -- [template-renderer.js](/template-renderer.js) - Combines Puppeteer and mustache.js with just a few lines of code in order to create PDF's from mustache HTML templates. |
31 |
| -- [tests/pagedjs/pagedjs.html](/tests/pagedjs/pagedjs.html) - Uses Paged.js to create a document with a table of contents and page numbers. |
32 |
| - |
33 |
| -### Typesetting with CSS |
34 |
| - |
35 |
| -Browsers provide some interesting options for typesetting. For example the following CSS styles. |
36 |
| - |
37 |
| -- **orphans** - minimum number of lines at the bottom of a page |
38 |
| -- **widows** - minimum number of lines at the top of a page |
39 |
| -- **break-before: avoid-page** - avoid page break before a HTML element |
40 |
| -- **break-after: avoid-page** - avoid page break after a HTML element |
41 |
| -- **text-align: justify** - align text to the left and right edges of lines, except at the last line |
42 |
| -- **hyphens: auto** - words are hyphenated according to language-specific hyphenation rules, where the language should be specified with a lang attribute |
43 |
| - |
44 |
| -**Note:** It seems that Puppeteer does not yet handle hyphens correctly. Two possible workarounds are [hyphen](https://www.npmjs.com/package/hyphen) (node) and [Hyphenopoly](https://github.com/mnater/Hyphenopoly) (browser). |
| 3 | +This small demo project generates PDFs from HTML templates. |
45 | 4 |
|
46 | 5 | ## Security
|
47 | 6 |
|
48 |
| -Some remarks concerning security. |
| 7 | +Some remarks concerning security: |
49 | 8 |
|
50 | 9 | - Ensure user input is validated.
|
51 | 10 | - Ensure the npm packages and docker image are up to date.
|
52 |
| -- Only allow access from trusted services. This is often achieved using json web tokens. Thanks to the package [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken) this was easily implemented in template-renderer.js. |
| 11 | +- Only allow access from trusted services. This is often achieved using json web tokens. Thanks to the package [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken) this was easily implemented in [template-renderer.js](/template-renderer.js). |
53 | 12 | - For serving over https see <https://expressjs.com/en/5x/api.html#app.listen> and <https://nodejs.org/api/https.html#httpscreateserveroptions-requestlistener>.
|
54 | 13 |
|
55 | 14 | ## Getting started
|
56 | 15 |
|
57 | 16 | ### Npm commands
|
58 | 17 |
|
59 |
| -See package.json for scripts to run, build and test template-renderer.js and some example templates. |
| 18 | +See [package.json](/package.json) for scripts to run, build and test [template-renderer.js](/template-renderer.js). It provides scripts to build some example templates as well. |
60 | 19 |
|
61 |
| -- Run `npm run generate-keys jwt` to create a private and public key. The public key is used by template-renderer.js for validating JWT's. |
62 |
| -- Run `npm run start:test` to serve template-renderer.js on port 5000 and a HTML test page on port 5021. See tests directory for some templates which can be used for testing. |
| 20 | +- Run `npm run generate-keys jwt` to create a private and public key. The public key is used by [template-renderer.js](/template-renderer.js) for validating JWT's. |
| 21 | +- Run `npm run start:test` to serve [template-renderer.js](/template-renderer.js) on port 5000 and a HTML test page on port 5021. The [examples directory](/examples) contains templates which can be used for testing. |
| 22 | +- Run `npm run start:image` to serve [template-renderer.js](/template-renderer.js) on port 5000 and run `start:test-page` to run a HTML test page on port 5021. The [examples directory](/examples) contains templates which can be used for testing. |
| 23 | +- Run `npx prettier . --check` or `npx prettier . --write` to format code using prettier (an opinionated formatter). |
63 | 24 |
|
64 | 25 | ### Docker image
|
65 | 26 |
|
66 |
| -A Dockerfile is included in this is repository. See <https://pptr.dev/guides/docker> and <https://github.com/puppeteer/puppeteer/tree/main/docker> for more information. |
| 27 | +A [Dockerfile](/Dockerfile) is included in this repository. See <https://pptr.dev/guides/docker> and <https://github.com/puppeteer/puppeteer/blob/main/docker/Dockerfile> for more information. |
| 28 | + |
| 29 | +### Source code |
| 30 | + |
| 31 | +- [template-renderer.js](/template-renderer.js) - Combines Puppeteer and mustache.js with a few lines of code in order to create PDFs from mustache HTML templates.\ |
| 32 | +The service waits until `window.readyForPdf != false`. This way the template can run JavaScript before it's rendered. |
| 33 | +- [generate-keys.js](/utils/generate-keys.js) - Generates a private and public key. |
| 34 | +- [test-page.js](/utils/test-page.js) - Serves a test page for testing [template-renderer.js](/template-renderer.js). |
67 | 35 |
|
68 | 36 | ### Some example templates
|
69 | 37 |
|
70 |
| -- tests/a4-pages.html |
71 |
| -- tests/email |
72 |
| -- tests/image |
73 |
| -- tests/mustache.html |
74 |
| -- tests/pagedjs |
| 38 | +- [a4](/examples/a4.html) |
| 39 | +- [email](/examples/email.html) |
| 40 | +- [image](/examples/image.html) |
| 41 | +- [mustache](/examples/mustache.html) |
| 42 | +- [pagedjs](/examples/pagedjs.html) |
| 43 | +- [pagedjs-toc](/examples/pagedjs.html) |
| 44 | +- [partial-lorem-ipsum](/examples/partial-lorem-ipsum.html) |
| 45 | +- [partial-mustache](/examples/partial-mustache.html) |
75 | 46 |
|
76 | 47 | ### Validating JWKS tokens
|
77 | 48 |
|
@@ -108,26 +79,3 @@ function createTokenValidationFunction(url, requiredScope) {
|
108 | 79 | ```
|
109 | 80 |
|
110 | 81 | _For local testing -_ Extra self-signed certificate(s) can be provided with a node environment variable: [NODE_EXTRA_CA_CERTS](https://nodejs.org/api/cli.html#node_extra_ca_certsfile).
|
111 |
| -
|
112 |
| -## Packages |
113 |
| -
|
114 |
| -- Puppeteer - library providing control to Chrome or Chromium |
115 |
| -- mustache.js - library to render mustache templates |
116 |
| -- Paged.js - library to paginate HTML content for printing to PDF |
117 |
| -- winston - a simple logging library with support for multiple transports |
118 |
| -- jsonwebtoken - an implementation of JSON Web Tokens used for signing and verifying JWT's |
119 |
| -- jwks-rsa - a library to retrieve signing keys from a JWKS endpoint |
120 |
| -
|
121 |
| -### Packages for testing |
122 |
| -
|
123 |
| -- axios - node package for http requests |
124 |
| -- http-server - serve files locally for testing |
125 |
| -- json5 - for parsing json written by hand |
126 |
| -- juice - inline web resources (styles, scripts, images) |
127 |
| -- nodemon - run script and restart when changed |
128 |
| -- npm-run-all - run multiple npm scripts with one command |
129 |
| -- web-resource-inliner - inline web resources without any changes (preserves all Paged.js styles) |
130 |
| -
|
131 |
| -### Package for formatting |
132 |
| -
|
133 |
| -- prettier - opinionated code formatter (run with `npx prettier . --check` or `npx prettier . --write`) |
0 commit comments