Skip to content

Commit 8d35470

Browse files
authored
Merge pull request #3 from matheuswsantos/master
Add support to puppeter pdf options + Add kool_global network
2 parents 3db3886 + b6adb27 commit 8d35470

5 files changed

Lines changed: 58 additions & 6 deletions

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
node_modules/
22
.vscode/
3-
storage/
43
.env

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ If you use Docker Compose (hopefully with [`kool`](https://github.com/kool-dev/k
2020
```php
2121
use GuzzleHttp\Client;
2222
23-
$pdf = (new Client())->post('http://pdf/from-html', [
23+
$pdf = (new Client())->post('http://pdf_dev/from-html', [
2424
'form_params' => [
2525
'html' => '<h1>This is my super kool HTML that I want to turn into an awesome PDF file!</h1> <p> This is a very silly example, but you get the idea of how powerful this is <b>:)</b> </p>',
26+
'options' => json_encode([
27+
'format' => 'A4',
28+
'printBackground' => false,
29+
]),
2630
],
2731
])->getBody();
2832
@@ -31,6 +35,10 @@ file_put_contents('path/to/my/super-kool.pdf', $pdf);
3135

3236
* Important to notice, the code above assumes you are running it from within another container in the same Docker Compose application so the `pdf` domain resolves to our microservice.
3337

38+
* The `options` should be a json data type
39+
40+
* You can see all these `options` in [puppeteer docs](https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#pagepdfoptions)
41+
3442
## Getting started on developing locally this microservice
3543

3644
To get started with development locally (using [`kool`](https://github.com/kool-dev/kool), of course!):

kool.services.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,12 @@ services:
1010
- '.:/app'
1111
ports:
1212
- '3000:3000'
13+
networks:
14+
kool_global:
15+
aliases:
16+
- pdf_dev
17+
18+
networks:
19+
kool_global:
20+
external: true
21+
name: "${KOOL_GLOBAL_NETWORK:-kool_global}"

pdf-service.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ app.get('/health', (req, res) => {
3838
app.post('/from-html', async (req, res) => {
3939
calls.fromHtml++;
4040
const html = req.body.html;
41+
const options = getPdfOptions(req.body.options);
4142
const htmlFile = md5(html.substr(0, 100)) + '.html';
4243
const fullHtmlPath = path.join(storagePath, htmlFile);
4344

4445
fs.writeFileSync(fullHtmlPath, html);
4546

4647
let pdfFilePath;
4748
try {
48-
pdfFilePath = await generatePdf(`file://${fullHtmlPath}`, req.query.media);
49+
pdfFilePath = await generatePdf(`file://${fullHtmlPath}`, req.query.media, options);
4950
} catch (err) {
5051
console.log('/from-html: error generating PDF', e);
5152
let msg = 'failure generating PDF';
@@ -100,7 +101,7 @@ function deliverPdfFile(res, pdfFilePath) {
100101
});
101102
}
102103

103-
async function generatePdf(url, media) {
104+
async function generatePdf(url, media, options = {}) {
104105
console.log('generatePdf: browser.newPage');
105106
const page = await browser.newPage();
106107

@@ -127,8 +128,7 @@ async function generatePdf(url, media) {
127128
scale: parseFloat(1),
128129
format: 'A4',
129130
printBackground: true,
130-
// displayHeaderFooter: false,
131-
// landscape: false,
131+
...options
132132
});
133133

134134
page.close();
@@ -140,6 +140,40 @@ function md5(seed) {
140140
return crypto.createHash('md5').update(seed).digest('hex');
141141
}
142142

143+
function getPdfOptions(options) {
144+
if (typeof options !== 'string') {
145+
return {};
146+
}
147+
148+
const parsedOptions = JSON.parse(options);
149+
150+
let mappedOptions = {};
151+
152+
[
153+
'path',
154+
'scale',
155+
'displayHeaderFooter',
156+
'headerTemplate',
157+
'footerTemplate',
158+
'printBackground',
159+
'landscape',
160+
'pageRanges',
161+
'format',
162+
'width',
163+
'height',
164+
'margin',
165+
'preferCSSPageSize',
166+
].forEach(prop => {
167+
if (parsedOptions[prop] === undefined) {
168+
return;
169+
}
170+
171+
mappedOptions[prop] = parsedOptions[prop];
172+
});
173+
174+
return mappedOptions;
175+
}
176+
143177
app.listen(port, () => {
144178
console.log(`Kool PDF service running at port ${port}`);
145179
console.log('Going to start puppeter');

storage/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

0 commit comments

Comments
 (0)