Skip to content

Commit c3849bf

Browse files
committed
Enhance PDF generation logic and improve alternateFormats check in workflows
1 parent dd38d12 commit c3849bf

2 files changed

Lines changed: 45 additions & 42 deletions

File tree

.github/workflows/build.yml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,21 @@ jobs:
2424
vnu --errors-only --skip-non-html --format json /site/snapshot.html \
2525
| tee $CHECK_DIR/html-validation.json
2626
27-
28-
- name: Check for alternateFormats
27+
- name: Check for alternateFormats via Node
2928
id: config
3029
run: |
31-
echo "grep=false" >> $GITHUB_OUTPUT
32-
if grep -q 'alternateFormats' ./js/config.js; then
33-
if grep -Pzoq 'alternateFormats:\s*\[\s*\{\s*label:\s*".+?",\s*uri:\s*".+?"\s*\}\s*\]' ./js/config.js; then
34-
echo "grep=true" >> $GITHUB_OUTPUT
35-
fi
30+
# Maak config require-baar
31+
cp ./js/config.js config.js
32+
echo "module.exports = { respecConfig };" >> config.js
33+
34+
# Laat Node bepalen of er een PDF entry is (en exitcode gebruiken)
35+
if node -e "const {respecConfig}=require('./config.js');
36+
const ok = Array.isArray(respecConfig.alternateFormats)
37+
&& respecConfig.alternateFormats.some(f => f && f.label==='pdf' && f.uri);
38+
if (!ok) process.exit(1);" ; then
39+
echo "grep=true" >> $GITHUB_OUTPUT
40+
else
41+
echo "grep=false" >> $GITHUB_OUTPUT
3642
fi
3743
3844
- name: Copy pdf.js if needed
@@ -46,7 +52,7 @@ jobs:
4652
echo "module.exports = { respecConfig };" >> config.js
4753
echo "var window = {respecMermaid:{createFigures:null}};" | cat - config.js > temp && mv temp config.js
4854
npm install puppeteer
49-
python3 -m http.server 8080 &
55+
python3 -m http.server 8081 &
5056
rm -f *.pdf
5157
node pdf.js
5258
mv *.pdf static/
@@ -126,4 +132,4 @@ jobs:
126132
static/*.pdf
127133
media/**/*
128134
js/**/*
129-
data/**/*
135+
data/**/*

.github/workflows/pdf.js

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,37 @@
1-
const config = require('./config.js');
1+
const config = require("./config.js");
22

33
const alternateFormats = config.respecConfig.alternateFormats;
44
if (alternateFormats === undefined) {
5-
console.warn("\'alternateFormats\' not found.");
6-
}
7-
else {
8-
const found = alternateFormats.find(element => element.label === "pdf");
9-
if (found === undefined) {
10-
console.warn("PDF not selected as alternate format.");
11-
}
12-
else if (found.uri === undefined) {
13-
console.warn("PDF file name (\'uri\') missing.");
14-
}
15-
else {
16-
const name = found.uri;
17-
console.log("Printing PDF with name: " + name);
5+
console.warn("'alternateFormats' not found.");
6+
} else {
7+
const found = alternateFormats.find((element) => element.label === "pdf");
8+
if (found === undefined) {
9+
console.warn("PDF not selected as alternate format.");
10+
} else if (found.uri === undefined) {
11+
console.warn("PDF file name ('uri') missing.");
12+
} else {
13+
const name = found.uri;
14+
console.log("Printing PDF with name: " + name);
1815

19-
// The following is based on code from
20-
// https://www.bannerbear.com/blog/how-to-convert-html-into-pdf-with-node-js-and-puppeteer/
21-
const puppeteer = require('puppeteer');
16+
const puppeteer = require("puppeteer");
2217

23-
(async () => {
24-
const browser = await puppeteer.launch();
25-
const page = await browser.newPage();
26-
const website_url = 'http://localhost:8080/snapshot.html';
27-
await page.goto(website_url, { waitUntil: 'networkidle0' });
28-
await page.emulateMediaType('print');
29-
await page.addStyleTag({ content: '.sidelabel {position: absolute}' })
30-
const pdf = await page.pdf({
31-
path: name,
32-
margin: { top: '100px', right: '50px', bottom: '100px', left: '50px' },
33-
printBackground: true,
34-
format: 'A4',
35-
});
18+
(async () => {
19+
const browser = await puppeteer.launch({
20+
args: ["--no-sandbox", "--disable-setuid-sandbox"],
21+
});
22+
const page = await browser.newPage();
23+
const website_url = "http://localhost:8081/snapshot.html";
24+
await page.goto(website_url, { waitUntil: "networkidle0" });
25+
await page.emulateMediaType("print");
26+
await page.addStyleTag({ content: ".sidelabel {position: absolute}" });
27+
const pdf = await page.pdf({
28+
path: name,
29+
margin: { top: "100px", right: "50px", bottom: "100px", left: "50px" },
30+
printBackground: true,
31+
format: "A4",
32+
});
3633

37-
await browser.close();
38-
})();
39-
}
34+
await browser.close();
35+
})();
36+
}
4037
}

0 commit comments

Comments
 (0)