@things-factory/integration-headless 8.0.0-beta.1 → 8.0.0-beta.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/package.json +6 -6
- package/server/engine/index.ts +0 -1
- package/server/engine/task/headless-pdf-capture-board.ts +0 -182
- package/server/engine/task/headless-pdf-capture-markdown.ts +0 -47
- package/server/engine/task/headless-pdf-capture.ts +0 -39
- package/server/engine/task/headless-pdf-open.ts +0 -98
- package/server/engine/task/headless-pdf-save.ts +0 -88
- package/server/engine/task/index.ts +0 -9
- package/server/engine/task/pdf-capture-util.ts +0 -336
- package/server/index.ts +0 -3
- package/server/tsconfig.json +0 -10
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@things-factory/integration-headless",
|
|
3
|
-
"version": "8.0.0-beta.
|
|
3
|
+
"version": "8.0.0-beta.5",
|
|
4
4
|
"main": "dist-server/index.js",
|
|
5
5
|
"things-factory": true,
|
|
6
6
|
"author": "heartyoh <heartyoh@hatiolab.com>",
|
|
@@ -22,12 +22,12 @@
|
|
|
22
22
|
"clean": "npm run clean:server"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@things-factory/attachment-base": "^8.0.0-beta.
|
|
26
|
-
"@things-factory/board-service": "^8.0.0-beta.
|
|
27
|
-
"@things-factory/integration-base": "^8.0.0-beta.
|
|
28
|
-
"@things-factory/shell": "^8.0.0-beta.
|
|
25
|
+
"@things-factory/attachment-base": "^8.0.0-beta.5",
|
|
26
|
+
"@things-factory/board-service": "^8.0.0-beta.5",
|
|
27
|
+
"@things-factory/integration-base": "^8.0.0-beta.5",
|
|
28
|
+
"@things-factory/shell": "^8.0.0-beta.5",
|
|
29
29
|
"ejs": "^3.1.10",
|
|
30
30
|
"pdf-lib": "^1.17.1"
|
|
31
31
|
},
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "9b8ca958169b0b0ceac8f355c4480c4536b6a821"
|
|
33
33
|
}
|
package/server/engine/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import './task'
|
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
import { TaskRegistry } from '@things-factory/integration-base'
|
|
2
|
-
import { PDFCaptureUtil, getCommonParameterSpec } from './pdf-capture-util'
|
|
3
|
-
import { BoardFunc } from '@things-factory/board-service'
|
|
4
|
-
import { access } from '@things-factory/utils'
|
|
5
|
-
|
|
6
|
-
const PAGE_FORMATS = {
|
|
7
|
-
A4: { width: 595.28, height: 841.89 },
|
|
8
|
-
A3: { width: 841.89, height: 1190.55 },
|
|
9
|
-
Letter: { width: 612, height: 792 },
|
|
10
|
-
Legal: { width: 612, height: 1008 }
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function convertToPixels(value: string): number {
|
|
14
|
-
const dpi = 96 // PDF에서 기본적으로 사용하는 DPI
|
|
15
|
-
|
|
16
|
-
if (value.endsWith('px')) {
|
|
17
|
-
return parseFloat(value)
|
|
18
|
-
} else if (value.endsWith('in')) {
|
|
19
|
-
const inches = parseFloat(value)
|
|
20
|
-
return inches * dpi
|
|
21
|
-
} else if (value.endsWith('cm')) {
|
|
22
|
-
const cm = parseFloat(value)
|
|
23
|
-
return cm * (dpi / 2.54)
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
return 0
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export async function HeadlessPDFCaptureBoard(step, context) {
|
|
30
|
-
const pdfUtil = new PDFCaptureUtil(context)
|
|
31
|
-
await pdfUtil.initBrowser(step.connection)
|
|
32
|
-
|
|
33
|
-
try {
|
|
34
|
-
var {
|
|
35
|
-
board: boardObject,
|
|
36
|
-
accessor,
|
|
37
|
-
boardAccessor,
|
|
38
|
-
draft,
|
|
39
|
-
format = 'A4',
|
|
40
|
-
width,
|
|
41
|
-
height,
|
|
42
|
-
landscape,
|
|
43
|
-
marginLeft = 0,
|
|
44
|
-
marginRight = 0,
|
|
45
|
-
marginTop = 0,
|
|
46
|
-
marginBottom = 0
|
|
47
|
-
} = step.params
|
|
48
|
-
var { domain, data, user, logger } = context
|
|
49
|
-
|
|
50
|
-
const boardId = boardAccessor ? access(boardAccessor, data) : boardObject?.id
|
|
51
|
-
|
|
52
|
-
if (!boardId) {
|
|
53
|
-
throw new Error('The board property must be set')
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const boardInput = access(accessor, data)
|
|
57
|
-
|
|
58
|
-
const { model, base } = await BoardFunc.headlessModel({ domain, id: boardId }, draft)
|
|
59
|
-
const [fontsToUse, fontStyles] = await BoardFunc.fonts(domain)
|
|
60
|
-
|
|
61
|
-
model.fonts = fontsToUse
|
|
62
|
-
model.fontStyles = fontStyles
|
|
63
|
-
|
|
64
|
-
var widthN = width ? convertToPixels(width) : 0
|
|
65
|
-
var heightN = height ? convertToPixels(height) : 0
|
|
66
|
-
|
|
67
|
-
if (!widthN && !heightN && PAGE_FORMATS[format]) {
|
|
68
|
-
const pageDimensions = PAGE_FORMATS[format]
|
|
69
|
-
widthN = pageDimensions.width * (96 / 72)
|
|
70
|
-
heightN = pageDimensions.height * (96 / 72)
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (landscape && widthN && heightN) {
|
|
74
|
-
;[widthN, heightN] = [heightN, widthN]
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
marginLeft = convertToPixels(marginLeft)
|
|
78
|
-
marginTop = convertToPixels(marginTop)
|
|
79
|
-
marginBottom = convertToPixels(marginBottom)
|
|
80
|
-
marginRight = convertToPixels(marginRight)
|
|
81
|
-
|
|
82
|
-
const contentWidth = widthN - marginLeft - marginRight
|
|
83
|
-
const contentHeight = heightN - marginTop - marginBottom
|
|
84
|
-
|
|
85
|
-
const page = await pdfUtil.browser!.newPage()
|
|
86
|
-
|
|
87
|
-
await page.setViewport({ width: Math.round(contentWidth), height: Math.round(contentHeight) })
|
|
88
|
-
await page.setRequestInterception(true)
|
|
89
|
-
await page.setDefaultTimeout(10000)
|
|
90
|
-
|
|
91
|
-
page.on('console', async msg => {
|
|
92
|
-
console.log(`[browser ${msg.type()}] ${msg.text()}`)
|
|
93
|
-
})
|
|
94
|
-
|
|
95
|
-
page.on('requestfailed', request => {
|
|
96
|
-
console.log('Request failed:', request.url())
|
|
97
|
-
})
|
|
98
|
-
|
|
99
|
-
const protocol = 'http'
|
|
100
|
-
const host = 'localhost'
|
|
101
|
-
const port = process.env.PORT ? `:${process.env.PORT}` : ''
|
|
102
|
-
const path = '/internal-board-service-view'
|
|
103
|
-
const url = `${protocol}://${host}${port}${path}`
|
|
104
|
-
|
|
105
|
-
const token = await user?.sign()
|
|
106
|
-
|
|
107
|
-
page.on('request', request => {
|
|
108
|
-
if (request.url() === url) {
|
|
109
|
-
request.continue({
|
|
110
|
-
method: 'POST',
|
|
111
|
-
headers: {
|
|
112
|
-
'Content-Type': 'application/json',
|
|
113
|
-
'x-things-factory-domain': domain?.subdomain,
|
|
114
|
-
Authorization: 'Bearer ' + token
|
|
115
|
-
},
|
|
116
|
-
postData: JSON.stringify({
|
|
117
|
-
model,
|
|
118
|
-
base
|
|
119
|
-
})
|
|
120
|
-
})
|
|
121
|
-
} else if (request.url().startsWith(`${protocol}://${host}${port}`)) {
|
|
122
|
-
request.continue({
|
|
123
|
-
headers: {
|
|
124
|
-
...request.headers(),
|
|
125
|
-
'x-things-factory-domain': domain?.subdomain,
|
|
126
|
-
Authorization: 'Bearer ' + token
|
|
127
|
-
}
|
|
128
|
-
})
|
|
129
|
-
} else {
|
|
130
|
-
request.continue()
|
|
131
|
-
}
|
|
132
|
-
})
|
|
133
|
-
|
|
134
|
-
await page.goto(url)
|
|
135
|
-
|
|
136
|
-
await page.evaluate(data => {
|
|
137
|
-
//@ts-ignore
|
|
138
|
-
s.data = data
|
|
139
|
-
return new Promise(resolve => {
|
|
140
|
-
requestAnimationFrame(() => resolve(0))
|
|
141
|
-
})
|
|
142
|
-
}, boardInput)
|
|
143
|
-
|
|
144
|
-
await pdfUtil.processPageAndGeneratePDF(step.params, null, page)
|
|
145
|
-
|
|
146
|
-
return {
|
|
147
|
-
data: context.__headless_pdf
|
|
148
|
-
}
|
|
149
|
-
} catch (error) {
|
|
150
|
-
throw error
|
|
151
|
-
} finally {
|
|
152
|
-
await pdfUtil.closeBrowser()
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
HeadlessPDFCaptureBoard.parameterSpec = [
|
|
157
|
-
...getCommonParameterSpec(),
|
|
158
|
-
{
|
|
159
|
-
type: 'string',
|
|
160
|
-
name: 'boardAccessor',
|
|
161
|
-
label: 'board-accessor'
|
|
162
|
-
},
|
|
163
|
-
{
|
|
164
|
-
type: 'resource-object',
|
|
165
|
-
name: 'board',
|
|
166
|
-
label: 'board',
|
|
167
|
-
property: {
|
|
168
|
-
queryName: 'boards'
|
|
169
|
-
}
|
|
170
|
-
},
|
|
171
|
-
{
|
|
172
|
-
type: 'boolean',
|
|
173
|
-
name: 'draft',
|
|
174
|
-
label: 'board-draft',
|
|
175
|
-
defaultValue: false,
|
|
176
|
-
description: 'Set whether to get the current working version or the last released version'
|
|
177
|
-
}
|
|
178
|
-
]
|
|
179
|
-
|
|
180
|
-
HeadlessPDFCaptureBoard.help = 'integration/task/headless-pdf-capture-board'
|
|
181
|
-
|
|
182
|
-
TaskRegistry.registerTaskHandler('headless-pdf-capture-board', HeadlessPDFCaptureBoard)
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { TaskRegistry } from '@things-factory/integration-base'
|
|
2
|
-
import { access } from '@things-factory/utils'
|
|
3
|
-
import { marked } from 'marked'
|
|
4
|
-
|
|
5
|
-
import { PDFCaptureUtil, getCommonParameterSpec } from './pdf-capture-util'
|
|
6
|
-
|
|
7
|
-
export async function HeadlessPDFCaptureMarkdown(step, context) {
|
|
8
|
-
const pdfUtil = new PDFCaptureUtil(context)
|
|
9
|
-
await pdfUtil.initBrowser(step.connection)
|
|
10
|
-
|
|
11
|
-
try {
|
|
12
|
-
const { accessor, markdownContent, markdownContentAccessor } = step.params
|
|
13
|
-
const templateInput = access(accessor, context.data)
|
|
14
|
-
const markdownTemplate = markdownContentAccessor ? access(markdownContentAccessor, context.data) : markdownContent
|
|
15
|
-
|
|
16
|
-
const renderedMarkdown = pdfUtil.renderTemplate(markdownTemplate, templateInput)
|
|
17
|
-
const htmlContent = marked(renderedMarkdown)
|
|
18
|
-
|
|
19
|
-
await pdfUtil.processPageAndGeneratePDF(step.params, htmlContent)
|
|
20
|
-
|
|
21
|
-
return {
|
|
22
|
-
data: context.__headless_pdf
|
|
23
|
-
}
|
|
24
|
-
} catch (error) {
|
|
25
|
-
throw error
|
|
26
|
-
} finally {
|
|
27
|
-
await pdfUtil.closeBrowser()
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
HeadlessPDFCaptureMarkdown.parameterSpec = [
|
|
32
|
-
...getCommonParameterSpec(),
|
|
33
|
-
{
|
|
34
|
-
type: 'scenario-step-input',
|
|
35
|
-
name: 'markdownContentAccessor',
|
|
36
|
-
label: 'markdown-content-accessor'
|
|
37
|
-
},
|
|
38
|
-
{
|
|
39
|
-
type: 'textarea',
|
|
40
|
-
name: 'markdownContent',
|
|
41
|
-
label: 'markdown-content'
|
|
42
|
-
}
|
|
43
|
-
]
|
|
44
|
-
|
|
45
|
-
HeadlessPDFCaptureMarkdown.help = 'integration/task/headless-pdf-capture-markdown'
|
|
46
|
-
|
|
47
|
-
TaskRegistry.registerTaskHandler('headless-pdf-capture-markdown', HeadlessPDFCaptureMarkdown)
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { TaskRegistry } from '@things-factory/integration-base'
|
|
2
|
-
import { access } from '@things-factory/utils'
|
|
3
|
-
|
|
4
|
-
import { PDFCaptureUtil, getCommonParameterSpec } from './pdf-capture-util'
|
|
5
|
-
|
|
6
|
-
export async function HeadlessPDFCapture(step, context) {
|
|
7
|
-
const pdfUtil = new PDFCaptureUtil(context)
|
|
8
|
-
await pdfUtil.initBrowser(step.connection)
|
|
9
|
-
|
|
10
|
-
try {
|
|
11
|
-
const { accessor, htmlContent } = step.params
|
|
12
|
-
const templateInput = access(accessor, context.data)
|
|
13
|
-
|
|
14
|
-
const renderedHtmlContent = pdfUtil.renderTemplate(htmlContent, templateInput)
|
|
15
|
-
|
|
16
|
-
await pdfUtil.processPageAndGeneratePDF(step.params, renderedHtmlContent)
|
|
17
|
-
|
|
18
|
-
return {
|
|
19
|
-
data: context.__headless_pdf
|
|
20
|
-
}
|
|
21
|
-
} catch (error) {
|
|
22
|
-
throw error
|
|
23
|
-
} finally {
|
|
24
|
-
await pdfUtil.closeBrowser()
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
HeadlessPDFCapture.parameterSpec = [
|
|
29
|
-
...getCommonParameterSpec(),
|
|
30
|
-
{
|
|
31
|
-
type: 'textarea',
|
|
32
|
-
name: 'htmlContent',
|
|
33
|
-
label: 'html-content'
|
|
34
|
-
}
|
|
35
|
-
]
|
|
36
|
-
|
|
37
|
-
HeadlessPDFCapture.help = 'integration/task/headless-pdf-capture'
|
|
38
|
-
|
|
39
|
-
TaskRegistry.registerTaskHandler('headless-pdf-capture', HeadlessPDFCapture)
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { PDFDocument } from 'pdf-lib'
|
|
2
|
-
import { TaskRegistry } from '@things-factory/integration-base'
|
|
3
|
-
import { access } from '@things-factory/utils'
|
|
4
|
-
import { PDFCaptureUtil, getCommonParameterSpec } from './pdf-capture-util'
|
|
5
|
-
|
|
6
|
-
async function HeadlessPDFOpen(step, context) {
|
|
7
|
-
const { connection: connectionName, params: stepOptions } = step
|
|
8
|
-
const { accessor, coverPage, lastPage, header, footer, watermark, fileName } = stepOptions || {}
|
|
9
|
-
|
|
10
|
-
const { data } = context
|
|
11
|
-
|
|
12
|
-
// Create a new PDF document using pdf-lib
|
|
13
|
-
const pdfDoc = await PDFDocument.create()
|
|
14
|
-
|
|
15
|
-
try {
|
|
16
|
-
const fileNameEncoded = fileName ? new TextEncoder().encode(access(fileName, data)) : undefined
|
|
17
|
-
|
|
18
|
-
const pdfInfo = {
|
|
19
|
-
pdfDoc,
|
|
20
|
-
watermark,
|
|
21
|
-
fileName: fileNameEncoded,
|
|
22
|
-
pageCount: 0
|
|
23
|
-
} as any
|
|
24
|
-
context.__headless_pdf = pdfInfo
|
|
25
|
-
|
|
26
|
-
var pdfUtil = new PDFCaptureUtil(context)
|
|
27
|
-
await pdfUtil.initBrowser(connectionName)
|
|
28
|
-
|
|
29
|
-
const templateInput = access(accessor, data)
|
|
30
|
-
|
|
31
|
-
// Convert Cover Page to PDF and add it to the document (if provided)
|
|
32
|
-
if (coverPage) {
|
|
33
|
-
const renderedCoverPage = pdfUtil.renderTemplate(coverPage, templateInput)
|
|
34
|
-
await pdfUtil.processPageAndGeneratePDF(stepOptions, renderedCoverPage)
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
var lastPageBuffer
|
|
38
|
-
|
|
39
|
-
// Process Last Page (if provided) but add it later in the save task
|
|
40
|
-
if (lastPage) {
|
|
41
|
-
const renderedLastPage = pdfUtil.renderTemplate(lastPage, templateInput)
|
|
42
|
-
lastPageBuffer = await pdfUtil.generateLastPageBuffer(stepOptions, renderedLastPage)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
pdfInfo.lastPageBuffer = lastPageBuffer
|
|
46
|
-
pdfInfo.pageCount = pdfDoc.getPageCount()
|
|
47
|
-
pdfInfo.header = header
|
|
48
|
-
pdfInfo.footer = footer
|
|
49
|
-
|
|
50
|
-
return {
|
|
51
|
-
data: pdfInfo
|
|
52
|
-
}
|
|
53
|
-
} catch (error) {
|
|
54
|
-
throw error
|
|
55
|
-
} finally {
|
|
56
|
-
await pdfUtil?.closeBrowser()
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
HeadlessPDFOpen.parameterSpec = [
|
|
61
|
-
...getCommonParameterSpec(),
|
|
62
|
-
{
|
|
63
|
-
type: 'textarea',
|
|
64
|
-
name: 'coverPage',
|
|
65
|
-
label: 'pdf-cover-page'
|
|
66
|
-
},
|
|
67
|
-
{
|
|
68
|
-
type: 'textarea',
|
|
69
|
-
name: 'lastPage',
|
|
70
|
-
label: 'pdf-last-page'
|
|
71
|
-
},
|
|
72
|
-
{
|
|
73
|
-
type: 'string',
|
|
74
|
-
name: 'header',
|
|
75
|
-
label: 'header',
|
|
76
|
-
placeholder: 'Page <%= pageNumber %> of <%= totalPages %>'
|
|
77
|
-
},
|
|
78
|
-
{
|
|
79
|
-
type: 'string',
|
|
80
|
-
name: 'footer',
|
|
81
|
-
label: 'footer',
|
|
82
|
-
placeholder: 'Page <%= pageNumber %> of <%= totalPages %>'
|
|
83
|
-
},
|
|
84
|
-
{
|
|
85
|
-
type: 'string',
|
|
86
|
-
name: 'watermark',
|
|
87
|
-
label: 'watermark'
|
|
88
|
-
},
|
|
89
|
-
{
|
|
90
|
-
type: 'string',
|
|
91
|
-
name: 'fileName',
|
|
92
|
-
label: 'filename'
|
|
93
|
-
}
|
|
94
|
-
]
|
|
95
|
-
|
|
96
|
-
HeadlessPDFOpen.help = 'integration/task/headless-pdf-open'
|
|
97
|
-
|
|
98
|
-
TaskRegistry.registerTaskHandler('headless-pdf-open', HeadlessPDFOpen)
|
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import { PDFDocument } from 'pdf-lib'
|
|
2
|
-
import { Readable } from 'stream'
|
|
3
|
-
import { TaskRegistry } from '@things-factory/integration-base'
|
|
4
|
-
import { Attachment, createAttachment } from '@things-factory/attachment-base'
|
|
5
|
-
import { getRepository } from '@things-factory/shell'
|
|
6
|
-
|
|
7
|
-
async function HeadlessPDFSave(step, context) {
|
|
8
|
-
const { domain, user, __headless_pdf } = context
|
|
9
|
-
|
|
10
|
-
if (!__headless_pdf || !__headless_pdf.pdfDoc) {
|
|
11
|
-
throw new Error(
|
|
12
|
-
'No PDF document found. Ensure that headless-pdf-open and headless-pdf-capture tasks are executed before saving.'
|
|
13
|
-
)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const pdfDoc = __headless_pdf.pdfDoc
|
|
17
|
-
|
|
18
|
-
// Add last page if it exists
|
|
19
|
-
if (__headless_pdf.lastPageBuffer) {
|
|
20
|
-
await appendLastPageToPDF(pdfDoc, __headless_pdf.lastPageBuffer)
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const pdfBytes = await pdfDoc.save()
|
|
24
|
-
const finalFileName = generateFileName(__headless_pdf.fileName)
|
|
25
|
-
|
|
26
|
-
const savedAttachment = await savePDFToFile(pdfBytes, finalFileName, domain, user)
|
|
27
|
-
const attachment = await getAttachmentDetails(savedAttachment.id)
|
|
28
|
-
|
|
29
|
-
return {
|
|
30
|
-
data: {
|
|
31
|
-
id: attachment.id,
|
|
32
|
-
name: attachment.name,
|
|
33
|
-
path: attachment.path,
|
|
34
|
-
mimetype: attachment.mimetype,
|
|
35
|
-
refBy: attachment.refBy,
|
|
36
|
-
fullpath: attachment.fullpath
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
async function appendLastPageToPDF(pdfDoc, lastPageBuffer) {
|
|
42
|
-
const lastPageDoc = await PDFDocument.load(lastPageBuffer)
|
|
43
|
-
const copiedLastPages = await pdfDoc.copyPages(lastPageDoc, lastPageDoc.getPageIndices())
|
|
44
|
-
copiedLastPages.forEach(page => pdfDoc.addPage(page))
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function generateFileName(fileName) {
|
|
48
|
-
return fileName || `document-${Date.now()}.pdf`
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
async function savePDFToFile(pdfBytes, finalFileName, domain, user) {
|
|
52
|
-
return await createAttachment(
|
|
53
|
-
null,
|
|
54
|
-
{
|
|
55
|
-
attachment: {
|
|
56
|
-
file: {
|
|
57
|
-
createReadStream: () =>
|
|
58
|
-
new Readable({
|
|
59
|
-
read() {
|
|
60
|
-
this.push(pdfBytes)
|
|
61
|
-
this.push(null)
|
|
62
|
-
}
|
|
63
|
-
}),
|
|
64
|
-
filename: finalFileName,
|
|
65
|
-
mimetype: 'application/pdf',
|
|
66
|
-
encoding: 'binary'
|
|
67
|
-
},
|
|
68
|
-
refType: 'headless-pdf-save',
|
|
69
|
-
refBy: 'pdf-published'
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
|
-
{
|
|
73
|
-
state: { domain, user }
|
|
74
|
-
}
|
|
75
|
-
)
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
async function getAttachmentDetails(attachmentId) {
|
|
79
|
-
return await getRepository(Attachment).findOne({
|
|
80
|
-
where: { id: attachmentId }
|
|
81
|
-
})
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
HeadlessPDFSave.parameterSpec = []
|
|
85
|
-
|
|
86
|
-
HeadlessPDFSave.help = 'integration/task/headless-pdf-save'
|
|
87
|
-
|
|
88
|
-
TaskRegistry.registerTaskHandler('headless-pdf-save', HeadlessPDFSave)
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import './headless-pdf-capture'
|
|
2
|
-
import './headless-pdf-capture-markdown'
|
|
3
|
-
import './headless-pdf-capture-board'
|
|
4
|
-
import './headless-pdf-open'
|
|
5
|
-
import './headless-pdf-save'
|
|
6
|
-
import './pdf-capture-util'
|
|
7
|
-
|
|
8
|
-
export * from './pdf-capture-util'
|
|
9
|
-
export * from './headless-pdf-capture-board'
|
|
@@ -1,336 +0,0 @@
|
|
|
1
|
-
import * as ejs from 'ejs'
|
|
2
|
-
import { ConnectionManager } from '@things-factory/integration-base'
|
|
3
|
-
import { PDFDocument } from 'pdf-lib'
|
|
4
|
-
import { Browser, Page } from 'puppeteer'
|
|
5
|
-
import { Logger } from 'winston'
|
|
6
|
-
|
|
7
|
-
interface StepOptions {
|
|
8
|
-
format?: string
|
|
9
|
-
width?: string
|
|
10
|
-
height?: string
|
|
11
|
-
marginTop?: string
|
|
12
|
-
marginBottom?: string
|
|
13
|
-
marginLeft?: string
|
|
14
|
-
marginRight?: string
|
|
15
|
-
scale?: number
|
|
16
|
-
printBackground?: boolean
|
|
17
|
-
landscape?: boolean
|
|
18
|
-
preferCSSPageSize?: boolean
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
interface PDFContext {
|
|
22
|
-
domain: any
|
|
23
|
-
data: any
|
|
24
|
-
logger: Logger
|
|
25
|
-
__headless_pdf: any
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export class PDFCaptureUtil {
|
|
29
|
-
private context: PDFContext
|
|
30
|
-
private headlessConnection: {
|
|
31
|
-
acquireSessionPage: () => Promise<Page>
|
|
32
|
-
releasePage: (page: Page) => Promise<void>
|
|
33
|
-
acquireBrowser: () => Promise<Browser>
|
|
34
|
-
releaseBrowser: (browser: Browser) => Promise<void>
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
public browser: Browser | null = null
|
|
38
|
-
|
|
39
|
-
constructor(context: PDFContext) {
|
|
40
|
-
this.context = context
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
async initBrowser(connectionName: string) {
|
|
44
|
-
const { domain } = this.context
|
|
45
|
-
this.headlessConnection = ConnectionManager.getConnectionInstanceByName(domain, connectionName)
|
|
46
|
-
this.browser = await this.headlessConnection.acquireBrowser()
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
async closeBrowser() {
|
|
50
|
-
if (this.browser) {
|
|
51
|
-
await this.browser.close()
|
|
52
|
-
this.headlessConnection.releaseBrowser(this.browser)
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
renderTemplate(template: string, data: any): string {
|
|
57
|
-
const { logger } = this.context
|
|
58
|
-
try {
|
|
59
|
-
return ejs.render(template, data)
|
|
60
|
-
} catch (error) {
|
|
61
|
-
logger.warn(`Template rendering error: ${error.message}`)
|
|
62
|
-
return template
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
async generatePDFContent(page: Page, pageOptions: any): Promise<PDFDocument> {
|
|
67
|
-
const contentBuffer = await page.pdf(pageOptions)
|
|
68
|
-
const contentPdfDoc = await PDFDocument.load(contentBuffer)
|
|
69
|
-
return contentPdfDoc
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
async appendPDFContentToDocument(pdfDoc: PDFDocument, contentPdfDoc: PDFDocument) {
|
|
73
|
-
const copiedPages = await pdfDoc.copyPages(contentPdfDoc, contentPdfDoc.getPageIndices())
|
|
74
|
-
copiedPages.forEach(page => pdfDoc.addPage(page))
|
|
75
|
-
this.context.__headless_pdf.pageCount += copiedPages.length
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
async processPageAndGeneratePDF(stepOptions: StepOptions, htmlContent: string, page?: Page) {
|
|
79
|
-
if (!this.browser) throw new Error('Browser not initialized. Call initBrowser() first.')
|
|
80
|
-
|
|
81
|
-
if (!page) {
|
|
82
|
-
page = page || (await this.browser.newPage())
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (htmlContent) {
|
|
86
|
-
// 페이지 컨텐츠 설정
|
|
87
|
-
await page.setContent(htmlContent, { waitUntil: 'networkidle0' }) // HTML 컨텐츠가 완전히 로드될 때까지 대기
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// 페이지가 완전히 로드된 후에 워터마크와 머릿글 적용
|
|
91
|
-
const { __headless_pdf } = this.context
|
|
92
|
-
const { header, footer, watermark, landscape } = __headless_pdf
|
|
93
|
-
|
|
94
|
-
if (header || footer || watermark) {
|
|
95
|
-
await this.applyHeaderFooterWatermark(page, { header, footer, watermark, landscape, data: __headless_pdf })
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
const pageOptions = this.buildPageOptions(stepOptions)
|
|
99
|
-
const contentPdfDoc = await this.generatePDFContent(page, pageOptions)
|
|
100
|
-
await this.appendPDFContentToDocument(__headless_pdf.pdfDoc, contentPdfDoc)
|
|
101
|
-
await page.close()
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
buildPageOptions({
|
|
105
|
-
format,
|
|
106
|
-
width,
|
|
107
|
-
height,
|
|
108
|
-
marginTop,
|
|
109
|
-
marginBottom,
|
|
110
|
-
marginLeft,
|
|
111
|
-
marginRight,
|
|
112
|
-
scale,
|
|
113
|
-
printBackground,
|
|
114
|
-
landscape,
|
|
115
|
-
preferCSSPageSize
|
|
116
|
-
}: StepOptions): any {
|
|
117
|
-
if (preferCSSPageSize) {
|
|
118
|
-
width = undefined
|
|
119
|
-
height = undefined
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
return {
|
|
123
|
-
format,
|
|
124
|
-
width,
|
|
125
|
-
height,
|
|
126
|
-
margin: {
|
|
127
|
-
top: marginTop,
|
|
128
|
-
right: marginRight,
|
|
129
|
-
bottom: marginBottom,
|
|
130
|
-
left: marginLeft
|
|
131
|
-
},
|
|
132
|
-
scale,
|
|
133
|
-
printBackground,
|
|
134
|
-
landscape,
|
|
135
|
-
displayHeaderFooter: false,
|
|
136
|
-
preferCSSPageSize
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
async applyHeaderFooterWatermark(
|
|
141
|
-
page: Page,
|
|
142
|
-
{
|
|
143
|
-
header,
|
|
144
|
-
footer,
|
|
145
|
-
watermark,
|
|
146
|
-
landscape,
|
|
147
|
-
data
|
|
148
|
-
}: { header: string; footer: string; watermark: string; landscape: boolean; data: any }
|
|
149
|
-
) {
|
|
150
|
-
await page.evaluate(
|
|
151
|
-
({ header, footer, watermark, landscape, data }) => {
|
|
152
|
-
const setPositioning = (element: HTMLElement, position: 'header' | 'footer') => {
|
|
153
|
-
element.style.position = 'fixed'
|
|
154
|
-
element.style.left = '0'
|
|
155
|
-
element.style.width = '100%'
|
|
156
|
-
element.style.textAlign = 'center'
|
|
157
|
-
element.style.fontSize = '12px'
|
|
158
|
-
element.style.margin = '0'
|
|
159
|
-
element.style.padding = '0'
|
|
160
|
-
element.style.zIndex = '1000'
|
|
161
|
-
|
|
162
|
-
if (position === 'header') {
|
|
163
|
-
element.style.top = '0'
|
|
164
|
-
} else if (position === 'footer') {
|
|
165
|
-
element.style.bottom = '0'
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
if (header) {
|
|
170
|
-
const headerElement = document.createElement('div')
|
|
171
|
-
headerElement.innerHTML = header
|
|
172
|
-
setPositioning(headerElement, 'header')
|
|
173
|
-
document.body.appendChild(headerElement)
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
if (footer) {
|
|
177
|
-
const footerElement = document.createElement('div')
|
|
178
|
-
footerElement.innerHTML = footer
|
|
179
|
-
setPositioning(footerElement, 'footer')
|
|
180
|
-
document.body.appendChild(footerElement)
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
if (watermark) {
|
|
184
|
-
const watermarkElement = document.createElement('div')
|
|
185
|
-
watermarkElement.innerHTML = watermark
|
|
186
|
-
watermarkElement.style.position = 'fixed'
|
|
187
|
-
watermarkElement.style.top = landscape ? '40%' : '50%'
|
|
188
|
-
watermarkElement.style.left = '50%'
|
|
189
|
-
watermarkElement.style.transform = 'translate(-50%, -50%) rotate(-45deg)'
|
|
190
|
-
watermarkElement.style.opacity = '0.2'
|
|
191
|
-
watermarkElement.style.fontSize = '50px'
|
|
192
|
-
watermarkElement.style.color = 'red'
|
|
193
|
-
watermarkElement.style.pointerEvents = 'none'
|
|
194
|
-
watermarkElement.style.zIndex = '9999'
|
|
195
|
-
document.body.appendChild(watermarkElement)
|
|
196
|
-
}
|
|
197
|
-
},
|
|
198
|
-
{
|
|
199
|
-
header: header && this.renderTemplate(header, data),
|
|
200
|
-
footer: footer && this.renderTemplate(footer, data),
|
|
201
|
-
watermark: watermark && this.renderTemplate(watermark, data),
|
|
202
|
-
landscape,
|
|
203
|
-
data
|
|
204
|
-
}
|
|
205
|
-
)
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* Generates a PDF buffer for the last page.
|
|
210
|
-
* @param stepOptions - Options used for generating the PDF.
|
|
211
|
-
* @param htmlContent - HTML content to be converted to PDF.
|
|
212
|
-
* @returns {Promise<Uint8Array>} - The generated PDF as a buffer.
|
|
213
|
-
*/
|
|
214
|
-
async generateLastPageBuffer(stepOptions: StepOptions, htmlContent: string): Promise<Uint8Array> {
|
|
215
|
-
if (!this.browser) throw new Error('Browser not initialized. Call initBrowser() first.')
|
|
216
|
-
|
|
217
|
-
const page = await this.browser.newPage()
|
|
218
|
-
|
|
219
|
-
// 페이지에 HTML 콘텐츠를 설정합니다.
|
|
220
|
-
await page.setContent(htmlContent, { waitUntil: 'networkidle0' })
|
|
221
|
-
|
|
222
|
-
// 페이지가 완전히 로드된 후에 워터마크와 머릿글 적용
|
|
223
|
-
const { __headless_pdf } = this.context
|
|
224
|
-
const { header, footer, watermark, landscape } = __headless_pdf
|
|
225
|
-
|
|
226
|
-
if (header || footer || watermark) {
|
|
227
|
-
await this.applyHeaderFooterWatermark(page, { header, footer, watermark, landscape, data: __headless_pdf })
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
// 페이지 옵션 설정
|
|
231
|
-
const pageOptions = this.buildPageOptions(stepOptions)
|
|
232
|
-
|
|
233
|
-
// PDF를 생성하고 버퍼를 반환합니다.
|
|
234
|
-
const lastPageBuffer = await page.pdf(pageOptions)
|
|
235
|
-
|
|
236
|
-
// 페이지 닫기
|
|
237
|
-
await page.close()
|
|
238
|
-
|
|
239
|
-
return lastPageBuffer
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
export function getCommonParameterSpec() {
|
|
244
|
-
return [
|
|
245
|
-
{
|
|
246
|
-
type: 'scenario-step-input',
|
|
247
|
-
name: 'accessor',
|
|
248
|
-
label: 'accessor'
|
|
249
|
-
},
|
|
250
|
-
{
|
|
251
|
-
type: 'select',
|
|
252
|
-
name: 'format',
|
|
253
|
-
label: 'page-format',
|
|
254
|
-
property: {
|
|
255
|
-
options: [
|
|
256
|
-
{ display: '', value: '' },
|
|
257
|
-
{ display: 'A4', value: 'A4' },
|
|
258
|
-
{ display: 'A3', value: 'A3' },
|
|
259
|
-
{ display: 'Letter', value: 'Letter' },
|
|
260
|
-
{ display: 'Legal', value: 'Legal' }
|
|
261
|
-
]
|
|
262
|
-
},
|
|
263
|
-
description: 'Select the paper format for the PDF'
|
|
264
|
-
},
|
|
265
|
-
{
|
|
266
|
-
type: 'string',
|
|
267
|
-
name: 'width',
|
|
268
|
-
label: 'page-width',
|
|
269
|
-
placeholder: '(e.g., "8.5in", "21cm", "600px")',
|
|
270
|
-
description: 'Specify the width of the page (e.g., "8.5in", "21cm", "600px")'
|
|
271
|
-
},
|
|
272
|
-
{
|
|
273
|
-
type: 'string',
|
|
274
|
-
name: 'height',
|
|
275
|
-
label: 'page-height',
|
|
276
|
-
placeholder: '(e.g., "11in", "29.7cm", "800px")',
|
|
277
|
-
description: 'Specify the height of the page (e.g., "11in", "29.7cm", "800px")'
|
|
278
|
-
},
|
|
279
|
-
{
|
|
280
|
-
type: 'string',
|
|
281
|
-
name: 'marginTop',
|
|
282
|
-
label: 'page-margin-top',
|
|
283
|
-
placeholder: '(e.g., "0.5in", "1cm", "100px")',
|
|
284
|
-
description: 'Set the top margin for the page'
|
|
285
|
-
},
|
|
286
|
-
{
|
|
287
|
-
type: 'string',
|
|
288
|
-
name: 'marginBottom',
|
|
289
|
-
label: 'page-margin-bottom',
|
|
290
|
-
placeholder: '(e.g., "0.5in", "1cm", "100px")',
|
|
291
|
-
description: 'Set the bottom margin for the page'
|
|
292
|
-
},
|
|
293
|
-
{
|
|
294
|
-
type: 'string',
|
|
295
|
-
name: 'marginLeft',
|
|
296
|
-
label: 'page-margin-left',
|
|
297
|
-
placeholder: '(e.g., "0.5in", "1cm", "100px")',
|
|
298
|
-
description: 'Set the left margin for the page'
|
|
299
|
-
},
|
|
300
|
-
{
|
|
301
|
-
type: 'string',
|
|
302
|
-
name: 'marginRight',
|
|
303
|
-
label: 'page-margin-right',
|
|
304
|
-
placeholder: '(e.g., "0.5in", "1cm", "100px")',
|
|
305
|
-
description: 'Set the right margin for the page'
|
|
306
|
-
},
|
|
307
|
-
{
|
|
308
|
-
type: 'number',
|
|
309
|
-
name: 'scale',
|
|
310
|
-
label: 'page-scale',
|
|
311
|
-
defaultValue: 1,
|
|
312
|
-
description: 'Set the scale of the page content (default is 1)'
|
|
313
|
-
},
|
|
314
|
-
{
|
|
315
|
-
type: 'boolean',
|
|
316
|
-
name: 'printBackground',
|
|
317
|
-
label: 'print-background',
|
|
318
|
-
defaultValue: true,
|
|
319
|
-
description: 'Include background graphics when printing the page'
|
|
320
|
-
},
|
|
321
|
-
{
|
|
322
|
-
type: 'boolean',
|
|
323
|
-
name: 'landscape',
|
|
324
|
-
label: 'landscape',
|
|
325
|
-
defaultValue: false,
|
|
326
|
-
description: 'Print the PDF in landscape orientation'
|
|
327
|
-
},
|
|
328
|
-
{
|
|
329
|
-
type: 'boolean',
|
|
330
|
-
name: 'preferCSSPageSize',
|
|
331
|
-
label: 'prefer-css-page-size',
|
|
332
|
-
defaultValue: false,
|
|
333
|
-
description: 'Whether to prefer the CSS-defined page size over the given width and height'
|
|
334
|
-
}
|
|
335
|
-
]
|
|
336
|
-
}
|
package/server/index.ts
DELETED