@browserless/pdf 10.7.13 → 10.8.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -3
- package/src/index.js +47 -9
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@browserless/pdf",
|
|
3
3
|
"description": "Sensible good defaults for exporting a website as PDF",
|
|
4
4
|
"homepage": "https://browserless.js.org/#/?id=pdfurl-options",
|
|
5
|
-
"version": "10.
|
|
5
|
+
"version": "10.8.0-beta.1",
|
|
6
6
|
"main": "src",
|
|
7
7
|
"repository": {
|
|
8
8
|
"directory": "packages/pdf",
|
|
@@ -24,7 +24,8 @@
|
|
|
24
24
|
"screen"
|
|
25
25
|
],
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@browserless/goto": "^10.7.13"
|
|
27
|
+
"@browserless/goto": "^10.7.13",
|
|
28
|
+
"@browserless/screenshot": "^10.8.0-beta.0"
|
|
28
29
|
},
|
|
29
30
|
"engines": {
|
|
30
31
|
"node": ">= 12"
|
|
@@ -37,5 +38,5 @@
|
|
|
37
38
|
"test": "exit 0"
|
|
38
39
|
},
|
|
39
40
|
"license": "MIT",
|
|
40
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "c94f407b23a2ad309f71069c1a3e4a9b74c09fd5"
|
|
41
42
|
}
|
package/src/index.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
const { takeScreenshot } = require('@browserless/screenshot')
|
|
4
|
+
const debug = require('debug-logfmt')('browserless:pdf')
|
|
3
5
|
const createGoto = require('@browserless/goto')
|
|
6
|
+
const timeSpan = require('@kikobeats/time-span')({ format: require('pretty-ms') })
|
|
4
7
|
|
|
5
8
|
const getMargin = unit => {
|
|
6
9
|
if (!unit) return unit
|
|
@@ -16,15 +19,50 @@ const getMargin = unit => {
|
|
|
16
19
|
module.exports = ({ goto, ...gotoOpts } = {}) => {
|
|
17
20
|
goto = goto || createGoto(gotoOpts)
|
|
18
21
|
|
|
19
|
-
return page
|
|
20
|
-
async (
|
|
21
|
-
|
|
22
|
+
return function pdf (page) {
|
|
23
|
+
return async (
|
|
24
|
+
url,
|
|
25
|
+
{ margin = '0.35cm', scale = 0.65, printBackground = true, waitUntil = 'auto', ...opts } = {}
|
|
26
|
+
) => {
|
|
27
|
+
let pdfBuffer
|
|
22
28
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
+
const generatePdf = page =>
|
|
30
|
+
page.pdf({
|
|
31
|
+
...opts,
|
|
32
|
+
margin: getMargin(margin),
|
|
33
|
+
printBackground,
|
|
34
|
+
scale
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
const timePdf = timeSpan()
|
|
38
|
+
|
|
39
|
+
if (waitUntil !== 'auto') {
|
|
40
|
+
await goto(page, { ...opts, url, waitUntil })
|
|
41
|
+
pdfBuffer = await generatePdf(page)
|
|
42
|
+
} else {
|
|
43
|
+
await goto(page, { ...opts, url, waitUntil, waitUntilAuto })
|
|
44
|
+
async function waitUntilAuto (page) {
|
|
45
|
+
let isWhite = false
|
|
46
|
+
let retryCount = 0
|
|
47
|
+
const maxRetries = 3
|
|
48
|
+
|
|
49
|
+
do {
|
|
50
|
+
const screenshotResult = await takeScreenshot({ page, goto, opts })
|
|
51
|
+
isWhite = screenshotResult.isWhite
|
|
52
|
+
|
|
53
|
+
if (isWhite && retryCount < maxRetries) {
|
|
54
|
+
retryCount++
|
|
55
|
+
debug('screenshot:retry', { waitUntil, isWhite, retryCount, maxRetries })
|
|
56
|
+
await goto.waitUntilAuto(page, { timeout: opts.timeout })
|
|
57
|
+
}
|
|
58
|
+
} while (isWhite && retryCount < maxRetries)
|
|
59
|
+
|
|
60
|
+
pdfBuffer = await generatePdf(page)
|
|
61
|
+
debug('screenshot', { waitUntil, isWhite, retryCount, duration: timePdf() })
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return pdfBuffer
|
|
29
66
|
}
|
|
67
|
+
}
|
|
30
68
|
}
|