@live-change/print-service 0.9.4 → 0.9.5
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/browser.js +10 -5
- package/package.json +5 -5
- package/printPdf.js +28 -5
package/browser.js
CHANGED
|
@@ -18,9 +18,11 @@ async function newBrowser() {
|
|
|
18
18
|
} else if(config.browserUrl) {
|
|
19
19
|
const browserInfo = await got.post(config.browserUrl + '/json/version').json()
|
|
20
20
|
const browser = await chromium.connect({ wsEndpoint: browserInfo.webSocketDebuggerUrl })
|
|
21
|
+
return browser
|
|
21
22
|
} else {
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
const browser = await chromium.launch({
|
|
24
|
+
headless: true
|
|
25
|
+
})
|
|
24
26
|
return browser
|
|
25
27
|
}
|
|
26
28
|
}
|
|
@@ -28,8 +30,11 @@ async function newBrowser() {
|
|
|
28
30
|
export async function runWithBrowser(func) {
|
|
29
31
|
return await browserQueue.add(async () => {
|
|
30
32
|
const browser = await newBrowser()
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
try {
|
|
34
|
+
const result = await func(browser)
|
|
35
|
+
return result
|
|
36
|
+
} finally {
|
|
37
|
+
await browser.close()
|
|
38
|
+
}
|
|
34
39
|
})
|
|
35
40
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@live-change/print-service",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.5",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -21,13 +21,13 @@
|
|
|
21
21
|
"url": "https://www.viamage.com/"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@live-change/framework": "^0.9.
|
|
25
|
-
"@live-change/relations-plugin": "^0.9.
|
|
26
|
-
"@live-change/user-service": "^0.9.
|
|
24
|
+
"@live-change/framework": "^0.9.5",
|
|
25
|
+
"@live-change/relations-plugin": "^0.9.5",
|
|
26
|
+
"@live-change/user-service": "^0.9.5",
|
|
27
27
|
"got": "^11.8.6",
|
|
28
28
|
"p-queue": "^8.0.1",
|
|
29
29
|
"playwright": "1.48.1"
|
|
30
30
|
},
|
|
31
|
-
"gitHead": "
|
|
31
|
+
"gitHead": "59f1d66de3a64b751308593e282e8635c0ecab7e",
|
|
32
32
|
"type": "module"
|
|
33
33
|
}
|
package/printPdf.js
CHANGED
|
@@ -4,6 +4,8 @@ const app = App.app()
|
|
|
4
4
|
import definition from './definition.js'
|
|
5
5
|
import config from './config.js'
|
|
6
6
|
|
|
7
|
+
import fs from "fs/promises"
|
|
8
|
+
|
|
7
9
|
import { task } from '@live-change/task-service'
|
|
8
10
|
|
|
9
11
|
import { runWithBrowser } from './browser.js'
|
|
@@ -35,11 +37,15 @@ export const printToPdfFileTask = task({
|
|
|
35
37
|
waitForSelector: {
|
|
36
38
|
type: String
|
|
37
39
|
},
|
|
40
|
+
clipSelector: {
|
|
41
|
+
type: String
|
|
42
|
+
},
|
|
38
43
|
outputPath: {
|
|
39
44
|
type: String
|
|
40
45
|
}
|
|
41
46
|
},
|
|
42
|
-
async execute({ path, data, timeout, media, options, waitForSelector,
|
|
47
|
+
async execute({ path, data, timeout, media, options, waitForSelector, clipSelector, outputPath },
|
|
48
|
+
{ task, trigger, triggerService }, emit) {
|
|
43
49
|
const all = 10
|
|
44
50
|
let at = 0
|
|
45
51
|
const url = await getAuthenticatedUrl(path, data)
|
|
@@ -58,7 +64,9 @@ export const printToPdfFileTask = task({
|
|
|
58
64
|
}
|
|
59
65
|
if(waitForSelector) {
|
|
60
66
|
task.progress(at++, all, 'waitingForSelector')
|
|
61
|
-
|
|
67
|
+
console.log("WAITING FOR SELECTOR", waitForSelector, "AT URL", url)
|
|
68
|
+
const locator = page.locator(waitForSelector)
|
|
69
|
+
await locator.waitFor({ state: 'attached', timeout })
|
|
62
70
|
} else {
|
|
63
71
|
task.progress(at++, all, 'waitingForNetworkIdle')
|
|
64
72
|
await page.waitForLoadState('networkidle')
|
|
@@ -66,12 +74,27 @@ export const printToPdfFileTask = task({
|
|
|
66
74
|
task.progress(at++, all, 'waitingForRendering')
|
|
67
75
|
await sleep(100) // wait for some time to give browser time to render
|
|
68
76
|
task.progress(at++, all, 'printing')
|
|
69
|
-
page.emulateMedia(media ?? { media: 'print' })
|
|
70
|
-
|
|
77
|
+
await page.emulateMedia(media ?? { media: 'print' })
|
|
78
|
+
|
|
79
|
+
const pdfOptions = options ?? { format: 'A4', printBackground: true }
|
|
80
|
+
if(clipSelector) {
|
|
81
|
+
const clip = await page.evaluate(selector => {
|
|
82
|
+
const element = document.querySelector(selector)
|
|
83
|
+
if(!element) return null
|
|
84
|
+
const { x, y, width, height } = element.getBoundingClientRect()
|
|
85
|
+
return { x, y, width, height }
|
|
86
|
+
}, clipSelector)
|
|
87
|
+
if(clip) {
|
|
88
|
+
pdfOptions.width = clip.width
|
|
89
|
+
pdfOptions.height = clip.height
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
pdf = await page.pdf(pdfOptions)
|
|
71
94
|
task.progress(at++, all, 'closingPage')
|
|
72
95
|
})
|
|
73
96
|
task.progress(at++, all, 'writingPdfToFile')
|
|
74
|
-
await fs.
|
|
97
|
+
await fs.writeFile(outputPath, pdf)
|
|
75
98
|
task.progress(at++, all, 'done')
|
|
76
99
|
return outputPath
|
|
77
100
|
}
|