You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+38-36Lines changed: 38 additions & 36 deletions
Original file line number
Diff line number
Diff line change
@@ -1,17 +1,6 @@
1
1
# Template renderer
2
2
3
-
A combination of three remarkable open source projects makes it easy to create PDF documents from HTML templates. With only a few lines of code Puppeteer and mustache.js can be combined to create PDF's from HTML. This is demonstrated in template-renderer.js. Paged.js can be used to create documents with a table of contents and page numbers. This is demonstrated in tests/pagedjs/pagedjs.html.
4
-
5
-
Creating PDF's is a common task in software development. This repository shows a free and open source alternative to commercial packages like Aspose.Pdf or Exstream.
6
-
7
-
## Security
8
-
9
-
Some remarks concerning security.
10
-
11
-
- Ensure user input is validated.
12
-
- Ensure the npm packages and docker image are up to date.
13
-
- 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.
14
-
- For serving over https see <https://expressjs.com/en/5x/api.html#app.listen> and <https://nodejs.org/api/https.html#httpscreateserveroptions-requestlistener>.
3
+
Creating PDF documents is a common task in software development. This repository shows how to create PDF's from HTML templates with the help of three remarkable open source projects. It shows a free and open source alternative to commercial software like Aspose.Pdf or Exstream.
15
4
16
5
## General information
17
6
@@ -24,10 +13,8 @@ License: See project, Apache License 2.0 at time of writing
24
13
25
14
### mustache.js
26
15
27
-
An open source javascript library to render mustache templates.\
28
-
<https://github.com/janl/mustache.js>\
29
-
<https://mustache.github.io>\
30
-
License: See project, MIT License at time of writeing
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
31
18
32
19
### Paged.js
33
20
@@ -38,6 +25,11 @@ An open source library to paginate HTML content for printing to PDF. This librar
38
25
<https://www.adamhyde.net/some-pagedjs-info>\
39
26
License: See project, MIT license at time of writing
40
27
28
+
### Implementation
29
+
30
+
- template-renderer.js - Combines Puppeteer and mustache.js with just a few lines to create PDF's from mustache HTML templates.
31
+
- tests/pagedjs/pagedjs.html - Uses Paged.js to create a document with a table of contents and page numbers.
32
+
41
33
### Typesetting with CSS
42
34
43
35
Browsers provide some interesting options for typesetting. For example the following CSS styles.
@@ -51,26 +43,36 @@ Browsers provide some interesting options for typesetting. For example the follo
51
43
52
44
**Note:** It seems that Puppeteer does not yet handle hyphens correctly. Two possible workarrounds are [hyphen](https://www.npmjs.com/package/hyphen) (node) and [Hyphenopoly](https://github.com/mnater/Hyphenopoly) (browser).
53
45
46
+
## Security
47
+
48
+
Some remarks concerning security.
49
+
50
+
- Ensure user input is validated.
51
+
- 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.
53
+
- For serving over https see <https://expressjs.com/en/5x/api.html#app.listen> and <https://nodejs.org/api/https.html#httpscreateserveroptions-requestlistener>.
54
+
55
+
54
56
## Getting started
55
57
56
58
### Npm commands
57
59
58
-
See package.json for some scripts to run, build and test template-renderer.js and some example templates.
60
+
See package.json for scripts to run, build and test template-renderer.js and some example templates.
59
61
60
-
- Run `npm run generate-keys jwt` to create a private and public key used by template-renderer.js for validating JWT's.
62
+
- 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.
61
63
- 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.
62
64
63
65
### Docker image
64
66
65
-
See <https://pptr.dev/guides/docker> and <https://github.com/puppeteer/puppeteer/tree/main/docker> for more information.
67
+
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.
66
68
67
69
### Some example templates
68
70
69
71
- tests/a4-pages.html
70
-
- tests/email.html
71
-
- tests/image.html
72
+
- tests/email
73
+
- tests/image
72
74
- tests/mustache.html
73
-
- tests/pagedjs.html
75
+
- tests/pagedjs
74
76
75
77
### Validating JWKS tokens
76
78
@@ -110,23 +112,23 @@ _For local testing -_ Extra self-signed certificate(s) can be provided with a no
110
112
111
113
## Packages
112
114
113
-
- Puppeteer: library providing control to Chrome or Chromium
114
-
- mustache.js: library to render mustache templates
115
-
- Paged.js: library to paginate HTML content for printing to PDF
116
-
- winston: a simple logging library with support for multiple transports
117
-
- jsonwebtoken: an implementation of JSON Web Tokens use for signing and verifying JWT's
118
-
- jwks-rsa: a library to retrieve signing keys from a JWKS endpoint
115
+
- Puppeteer - library providing control to Chrome or Chromium
116
+
- mustache.js - library to render mustache templates
117
+
- Paged.js - library to paginate HTML content for printing to PDF
118
+
- winston - a simple logging library with support for multiple transports
119
+
- jsonwebtoken - an implementation of JSON Web Tokens used for signing and verifying JWT's
120
+
- jwks-rsa - a library to retrieve signing keys from a JWKS endpoint
119
121
120
122
### Packages for testing
121
123
122
-
- axios: node package for http requests
123
-
- http-server: serve files to test HTML locally
124
-
- json5: for parsing json written by hand
125
-
- juice: inline web resources (styles, scripts, images)
126
-
- nodemon: run script and restart when changed
127
-
- npm-run-all: run multiple npm scripts with one command
128
-
- web-resource-inliner: inline web resources without any changes (preserves all Paged.js styles)
124
+
- axios - node package for http requests
125
+
- http-server - serve files locally for testing
126
+
- json5 - for parsing json written by hand
127
+
- juice - inline web resources (styles, scripts, images)
128
+
- nodemon - run script and restart when changed
129
+
- npm-run-all - run multiple npm scripts with one command
130
+
- web-resource-inliner - inline web resources without any changes (preserves all Paged.js styles)
129
131
130
132
### Package for formatting
131
133
132
-
- prettier: opinionated code formatter (run with `npx prettier . --check` or `npx prettier . --write`)
134
+
- prettier - opinionated code formatter (run with `npx prettier . --check` or `npx prettier . --write`)
0 commit comments