Skip to content

Commit 9a5a40c

Browse files
committed
-
1 parent 865a439 commit 9a5a40c

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Template renderer
22

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.
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.
44

55
## General information
66

@@ -25,10 +25,10 @@ An open source library to paginate HTML content for printing to PDF. This librar
2525
<https://www.adamhyde.net/some-pagedjs-info>\
2626
License: See project, MIT license at time of writing
2727

28-
### Implementation
28+
### Source code
2929

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.
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.
3232

3333
### Typesetting with CSS
3434

@@ -41,7 +41,7 @@ Browsers provide some interesting options for typesetting. For example the follo
4141
- **text-align: justify** - align text to the left and right edges of lines, except at the last line
4242
- **hyphens: auto** - words are hyphenated according to language-specific hyphenation rules, where the language should be specified with a lang attribute
4343

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).
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).
4545

4646
## Security
4747

@@ -52,7 +52,6 @@ Some remarks concerning security.
5252
- 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.
5353
- For serving over https see <https://expressjs.com/en/5x/api.html#app.listen> and <https://nodejs.org/api/https.html#httpscreateserveroptions-requestlistener>.
5454

55-
5655
## Getting started
5756

5857
### Npm commands
@@ -97,7 +96,7 @@ function createTokenValidationFunction(url, requiredScope) {
9796
function getKey(header, callback) {
9897
client.getSigningKey(header.kid, function (err, key) {
9998
if (err) {
100-
logger.error({ message: err });
99+
logger.error({ message: err.message });
101100
}
102101
var signingKey = key?.publicKey || key?.rsaPublicKey;
103102
callback(null, signingKey);

template-renderer.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,17 @@ function sanatizeInput(data) {
8080
return { template, data: data.data, partials };
8181
}
8282

83-
function logHandler(req, _res, next) {
84-
const logInfo = { requestPath: req.path, traceId: req.headers.traceId };
83+
function logHandler(req, res, next) {
84+
const logInfo = { requestPath: req.path, traceId: getTraceId(req) };
8585
logger.info({ message: "Started handling request", ...logInfo });
86+
res.on("finish", () => {
87+
logger.info({ message: "Finished handling request", ...logInfo });
88+
});
8689
next();
87-
logger.info({ message: "Finished handling request", ...logInfo });
8890
}
8991

9092
async function tokenHandler(req, res, next) {
91-
const logInfo = { requestPath: req.path, traceId: req.headers.traceId };
93+
const logInfo = { requestPath: req.path, traceId: getTraceId(req) };
9294
const validationResult = await validateToken(getBearerToken(req));
9395
if (!validationResult.valid) {
9496
logger.warn({ message: validationResult.message, ...logInfo });
@@ -110,6 +112,15 @@ function getBearerToken(req) {
110112
return parts[0] === "Bearer" ? parts[1] : "";
111113
}
112114

115+
function getTraceId(req) {
116+
const traceparent = req.headers.traceparent;
117+
const parts = traceparent?.split("-");
118+
if (parts?.length >= 2) {
119+
return parts[1];
120+
}
121+
return traceparent;
122+
}
123+
113124
function createTokenValidationFunction(filename, requiredScope) {
114125
let jwtPublicKey = "";
115126
try {
@@ -131,7 +142,7 @@ function verifyToken(token, secretOrPublicKey, requiredScope) {
131142
return new Promise((resolve) => {
132143
jwt.verify(token, secretOrPublicKey, function (err, decoded) {
133144
if (err) {
134-
resolve({ valid: false, message: err });
145+
resolve({ valid: false, message: err.message });
135146
} else if (
136147
Array.isArray(decoded.scope) &&
137148
decoded.scope.indexOf(requiredScope) !== -1

0 commit comments

Comments
 (0)