@browserless/pdf 10.10.1 → 10.10.3
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/README.md +5 -3
- package/package.json +4 -4
- package/src/index.js +30 -2
package/README.md
CHANGED
|
@@ -73,6 +73,7 @@ The package applies these optimized defaults:
|
|
|
73
73
|
| `scale` | `number` | `0.65` | Scale of the webpage rendering (0.1 - 2) |
|
|
74
74
|
| `printBackground` | `boolean` | `true` | Print background graphics |
|
|
75
75
|
| `waitUntil` | `string` | `'auto'` | When to consider navigation done |
|
|
76
|
+
| `waitForDom` | `number` | `0` | DOM stability window in ms (idle is `waitForDom / 10`, `0` disables DOM wait) |
|
|
76
77
|
| `format` | `string` | `'Letter'` | Paper format (A4, Letter, etc.) |
|
|
77
78
|
| `landscape` | `boolean` | `false` | Paper orientation |
|
|
78
79
|
| `width` | `string \| number` | — | Paper width (overrides format) |
|
|
@@ -111,9 +112,10 @@ Supported units: `px`, `in`, `cm`, `mm`
|
|
|
111
112
|
When `waitUntil: 'auto'` (the default), the package:
|
|
112
113
|
|
|
113
114
|
1. Navigates to the page
|
|
114
|
-
2.
|
|
115
|
-
3.
|
|
116
|
-
4.
|
|
115
|
+
2. Optionally waits for DOM stability (`waitForDom`, default `0` = disabled)
|
|
116
|
+
3. Takes a low-quality screenshot to check for white/blank content
|
|
117
|
+
4. If the page appears blank, retries until content is detected
|
|
118
|
+
5. Generates the PDF once content is confirmed
|
|
117
119
|
|
|
118
120
|
This ensures dynamic content (JavaScript-rendered pages) is fully loaded before PDF generation.
|
|
119
121
|
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@browserless/pdf",
|
|
3
3
|
"description": "Convert websites to high-quality PDFs with customizable margins, background printing, and optimized scaling.",
|
|
4
4
|
"homepage": "https://browserless.js.org/#/?id=pdfurl-options",
|
|
5
|
-
"version": "10.10.
|
|
5
|
+
"version": "10.10.3",
|
|
6
6
|
"main": "src",
|
|
7
7
|
"author": {
|
|
8
8
|
"email": "hello@microlink.io",
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
"conversion"
|
|
31
31
|
],
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@browserless/goto": "^10.10.
|
|
34
|
-
"@browserless/screenshot": "^10.10.
|
|
33
|
+
"@browserless/goto": "^10.10.2",
|
|
34
|
+
"@browserless/screenshot": "^10.10.3",
|
|
35
35
|
"@kikobeats/time-span": "~1.0.11",
|
|
36
36
|
"debug-logfmt": "~1.4.7",
|
|
37
37
|
"pretty-ms": "~7.0.1"
|
|
@@ -47,5 +47,5 @@
|
|
|
47
47
|
"test": "exit 0"
|
|
48
48
|
},
|
|
49
49
|
"license": "MIT",
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "4d2b893e8215a91d830f4471f51c567a53cd6bbb"
|
|
51
51
|
}
|
package/src/index.js
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const timeSpan = require('@kikobeats/time-span')({ format: n => Math.round(n) })
|
|
4
|
-
const
|
|
4
|
+
const pReflect = require('p-reflect')
|
|
5
|
+
const {
|
|
6
|
+
isWhiteScreenshot,
|
|
7
|
+
waitForDomStability,
|
|
8
|
+
resolveWaitForDom,
|
|
9
|
+
DEFAULT_WAIT_FOR_DOM
|
|
10
|
+
} = require('@browserless/screenshot')
|
|
5
11
|
const debug = require('debug-logfmt')('browserless:pdf')
|
|
6
12
|
const createGoto = require('@browserless/goto')
|
|
7
13
|
|
|
@@ -22,9 +28,17 @@ module.exports = ({ goto, ...gotoOpts } = {}) => {
|
|
|
22
28
|
return function pdf (page) {
|
|
23
29
|
return async (
|
|
24
30
|
url,
|
|
25
|
-
{
|
|
31
|
+
{
|
|
32
|
+
margin = '0.35cm',
|
|
33
|
+
scale = 0.65,
|
|
34
|
+
printBackground = true,
|
|
35
|
+
waitUntil = 'auto',
|
|
36
|
+
waitForDom = DEFAULT_WAIT_FOR_DOM,
|
|
37
|
+
...opts
|
|
38
|
+
} = {}
|
|
26
39
|
) => {
|
|
27
40
|
let pdfBuffer
|
|
41
|
+
const waitForDomOpts = resolveWaitForDom(waitForDom)
|
|
28
42
|
|
|
29
43
|
const generatePdf = page =>
|
|
30
44
|
page.pdf({
|
|
@@ -34,12 +48,26 @@ module.exports = ({ goto, ...gotoOpts } = {}) => {
|
|
|
34
48
|
scale
|
|
35
49
|
})
|
|
36
50
|
|
|
51
|
+
const waitForDomStabilityResult = async page => {
|
|
52
|
+
if (!waitForDomOpts) return
|
|
53
|
+
|
|
54
|
+
const result = await pReflect(page.evaluate(waitForDomStability, waitForDomOpts))
|
|
55
|
+
debug(
|
|
56
|
+
'waitForDomStability',
|
|
57
|
+
result.isRejected
|
|
58
|
+
? { ...waitForDomOpts, error: result.reason.message || result.reason }
|
|
59
|
+
: { ...waitForDomOpts, ...result.value }
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
|
|
37
63
|
if (waitUntil !== 'auto') {
|
|
38
64
|
await goto(page, { ...opts, url, waitUntil })
|
|
65
|
+
await waitForDomStabilityResult(page)
|
|
39
66
|
pdfBuffer = await generatePdf(page)
|
|
40
67
|
} else {
|
|
41
68
|
await goto(page, { ...opts, url, waitUntil, waitUntilAuto })
|
|
42
69
|
async function waitUntilAuto (page) {
|
|
70
|
+
await waitForDomStabilityResult(page)
|
|
43
71
|
const timeout = goto.timeouts.action(opts.timeout)
|
|
44
72
|
let isWhite = false
|
|
45
73
|
let retry = -1
|