@things-factory/integration-headless 7.0.48 → 7.0.53

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@things-factory/integration-headless",
3
- "version": "7.0.48",
3
+ "version": "7.0.53",
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": "^7.0.48",
26
- "@things-factory/board-service": "^7.0.48",
27
- "@things-factory/integration-base": "^7.0.48",
28
- "@things-factory/shell": "^7.0.48",
25
+ "@things-factory/attachment-base": "^7.0.49",
26
+ "@things-factory/board-service": "^7.0.49",
27
+ "@things-factory/integration-base": "^7.0.49",
28
+ "@things-factory/shell": "^7.0.49",
29
29
  "ejs": "^3.1.10",
30
30
  "pdf-lib": "^1.17.1"
31
31
  },
32
- "gitHead": "516772ebee08900790bcc43477419d60adcb4dcd"
32
+ "gitHead": "96b4308aff1d312343c647a80823658e78b03824"
33
33
  }
@@ -1,9 +1,7 @@
1
- import * as ejs from 'ejs'
2
1
  import { TaskRegistry } from '@things-factory/integration-base'
3
- import { ConnectionManager } from '@things-factory/integration-base'
4
- import { access } from '@things-factory/utils'
2
+
3
+ import { PDFCaptureUtil, getCommonParameterSpec } from './pdf-capture-util'
5
4
  import { BoardFunc } from '@things-factory/board-service'
6
- import { PDFDocument } from 'pdf-lib'
7
5
 
8
6
  const PAGE_FORMATS = {
9
7
  A4: { width: 595.28, height: 841.89 },
@@ -12,250 +10,86 @@ const PAGE_FORMATS = {
12
10
  Legal: { width: 612, height: 1008 }
13
11
  }
14
12
 
15
- async function HeadlessPDFCaptureBoard(step, context) {
16
- var { connection: connectionName, params: stepOptions } = step
17
- var {
18
- accessor,
19
- board,
20
- draft,
21
- format = 'A4', // Set default value to A4
22
- width,
23
- height,
24
- marginTop = 0,
25
- marginBottom = 0,
26
- marginLeft = 0,
27
- marginRight = 0,
28
- scale,
29
- printBackground,
30
- landscape,
31
- preferCSSPageSize
32
- } = stepOptions || {}
33
- var { domain, data, user, logger, __headless_pdf } = context
34
-
35
- if (!__headless_pdf) {
36
- throw new Error('It requires headless-pdf-open to be performed first')
37
- }
38
-
39
- if (!board || !board.id) {
40
- throw new Error('The board property must be set')
41
- }
13
+ type PageFormat = keyof typeof PAGE_FORMATS
42
14
 
43
- var { pdfDoc, header, footer, watermark } = __headless_pdf
44
-
45
- var headlessPool = ConnectionManager.getConnectionInstanceByName(domain, connectionName)
46
- let browser
15
+ export async function HeadlessPDFCaptureBoard(step, context) {
16
+ const pdfUtil = new PDFCaptureUtil(context)
17
+ await pdfUtil.initBrowser(step.connection)
47
18
 
48
19
  try {
49
- browser = await headlessPool.acquire()
50
- const page = await browser.newPage()
20
+ const { board, draft, format = 'A4', landscape, preferCSSPageSize } = step.params
51
21
 
52
- const boardInput = access(accessor, data)
22
+ if (!board || !board.id) {
23
+ throw new Error('The board property must be set')
24
+ }
53
25
 
54
- var { model, base } = await BoardFunc.headlessModel({ domain, id: board.id, model }, draft)
55
- const [fontsToUse, fontStyles] = await BoardFunc.fonts(domain)
26
+ const { model, base } = await BoardFunc.headlessModel({ domain: context.domain, id: board.id }, draft)
27
+ const [fontsToUse, fontStyles] = await BoardFunc.fonts(context.domain)
56
28
 
57
29
  model.fonts = fontsToUse
58
30
  model.fontStyles = fontStyles
59
31
 
60
- var { width: boardWidthPx, height: boardHeightPx } = model
32
+ let width: number | undefined = PAGE_FORMATS[format as PageFormat]?.width
33
+ let height: number | undefined = PAGE_FORMATS[format as PageFormat]?.height
61
34
 
62
35
  if (preferCSSPageSize) {
63
36
  width = undefined
64
37
  height = undefined
65
- } else if (PAGE_FORMATS[format]) {
66
- const pageDimensions = PAGE_FORMATS[format]
67
- width = pageDimensions.width
68
- height = pageDimensions.height
69
38
  }
70
39
 
71
- if (landscape) {
40
+ if (landscape && width && height) {
72
41
  ;[width, height] = [height, width]
73
42
  }
74
43
 
75
- const contentWidth = width - marginLeft - marginRight
76
- const contentHeight = height - marginTop - marginBottom
77
-
78
- const boardWidthPt = (boardWidthPx * 72) / 96
79
- const boardHeightPt = (boardHeightPx * 72) / 96
80
-
81
- const widthRatio = contentWidth / boardWidthPt
82
- const heightRatio = contentHeight / boardHeightPt
83
- const ratio = Math.min(widthRatio, heightRatio)
84
-
85
- const scaledBoardWidthPt = boardWidthPt * ratio
86
- const scaledBoardHeightPt = boardHeightPt * ratio
87
-
88
- const offsetX = (contentWidth - scaledBoardWidthPt) / 2 + marginLeft
89
- const offsetY = (contentHeight - scaledBoardHeightPt) / 2 + marginBottom
90
-
91
- await page.setViewport({ width: Math.round(boardWidthPx * ratio), height: Math.round(boardHeightPx * ratio) })
92
- await page.setRequestInterception(true)
93
- await page.setDefaultTimeout(10000)
94
-
95
- page.on('console', async msg => {
96
- console.log(`[browser ${msg.type()}] ${msg.text()}`)
97
- })
98
-
99
- page.on('requestfailed', request => {
100
- console.log('Request failed:', request.url())
101
- })
102
-
103
44
  const protocol = 'http'
104
45
  const host = 'localhost'
105
46
  const port = process.env.PORT ? `:${process.env.PORT}` : ''
106
47
  const path = '/internal-board-service-view'
107
48
  const url = `${protocol}://${host}${port}${path}`
108
49
 
109
- const token = await user?.sign()
50
+ const page = await pdfUtil.browser!.newPage()
110
51
 
111
- page.on('request', request => {
112
- if (request.url() === url) {
113
- request.continue({
114
- method: 'POST',
115
- headers: {
116
- 'Content-Type': 'application/json',
117
- 'x-things-factory-domain': domain?.subdomain,
118
- Authorization: 'Bearer ' + token
119
- },
120
- postData: JSON.stringify({
121
- model,
122
- base
123
- })
124
- })
125
- } else if (request.url().startsWith(`${protocol}://${host}${port}`)) {
126
- request.continue({
127
- headers: {
128
- ...request.headers(),
129
- 'x-things-factory-domain': domain?.subdomain,
130
- Authorization: 'Bearer ' + token
131
- }
132
- })
133
- } else {
134
- request.continue()
135
- }
52
+ await page.setViewport({
53
+ width: Math.round(width || 800), // Fallback for width if not defined
54
+ height: Math.round(height || 600) // Fallback for height if not defined
136
55
  })
137
56
 
138
57
  await page.goto(url)
139
58
 
140
- await page.evaluate(data => {
141
- //@ts-ignore
142
- s.data = data
143
- return new Promise(resolve => {
144
- requestAnimationFrame(() => resolve(0))
145
- })
146
- }, boardInput)
147
-
148
- const renderTemplateSafely = (template, data) => {
149
- try {
150
- return ejs.render(template, data)
151
- } catch (error) {
152
- logger.warn(`Template rendering error: ${error.message}`)
153
- return template
154
- }
155
- }
156
-
157
- header = renderTemplateSafely(header, __headless_pdf)
158
- footer = renderTemplateSafely(footer, __headless_pdf)
159
-
160
- // Apply header, footer, and watermark using Puppeteer
161
- // Apply header, footer, and watermark using Puppeteer
162
- if (header || footer || watermark) {
163
- await page.evaluate(
164
- ({ header, footer, watermark, isLandscape }) => {
165
- const setPositioning = (element, position) => {
166
- element.style.position = 'fixed'
167
- element.style.left = '0'
168
- element.style.width = '100%'
169
- element.style.textAlign = 'center'
170
- element.style.fontSize = '12px'
171
- element.style.margin = '0'
172
- element.style.padding = '0'
173
- if (position === 'header') {
174
- element.style.top = '0'
175
- } else if (position === 'footer') {
176
- element.style.bottom = '0'
177
- }
178
- }
179
-
180
- if (header) {
181
- const headerElement = document.createElement('div')
182
- headerElement.innerHTML = header
183
- setPositioning(headerElement, 'header')
184
- document.body.appendChild(headerElement)
185
- }
186
-
187
- if (footer) {
188
- const footerElement = document.createElement('div')
189
- footerElement.innerHTML = footer
190
- setPositioning(footerElement, 'footer')
191
- document.body.appendChild(footerElement)
192
- }
193
-
194
- if (watermark) {
195
- const watermarkElement = document.createElement('div')
196
- watermarkElement.innerHTML = watermark
197
- watermarkElement.style.position = 'fixed'
198
- watermarkElement.style.top = isLandscape ? '40%' : '50%'
199
- watermarkElement.style.left = '50%'
200
- watermarkElement.style.transform = 'translate(-50%, -50%) rotate(-45deg)'
201
- watermarkElement.style.opacity = '0.2'
202
- watermarkElement.style.fontSize = '50px'
203
- watermarkElement.style.color = 'red'
204
- watermarkElement.style.pointerEvents = 'none'
205
- watermarkElement.style.zIndex = '9999'
206
- document.body.appendChild(watermarkElement)
207
- }
208
- },
209
- { header, footer, watermark, isLandscape: landscape }
210
- )
211
- }
212
-
213
- const pageOptions = {
214
- format,
215
- width: `${scaledBoardWidthPt}px`,
216
- height: `${scaledBoardHeightPt}px`,
217
- margin: {
218
- top: `${offsetY}px`,
219
- right: `${offsetX}px`,
220
- bottom: `${offsetY}px`,
221
- left: `${offsetX}px`
59
+ await page.evaluate(
60
+ data => {
61
+ //@ts-ignore
62
+ s.data = data
63
+ return new Promise(resolve => {
64
+ requestAnimationFrame(() => resolve(0))
65
+ })
222
66
  },
223
- scale,
224
- printBackground,
225
- landscape,
226
- displayHeaderFooter: false, // Handled via Puppeteer directly
227
- preferCSSPageSize
228
- }
229
-
230
- const contentBuffer = await page.pdf(pageOptions)
231
- const contentPdfDoc = await PDFDocument.load(contentBuffer)
232
- const copiedPages = await pdfDoc.copyPages(contentPdfDoc, contentPdfDoc.getPageIndices())
233
-
234
- copiedPages.forEach(page => pdfDoc.addPage(page))
235
-
236
- __headless_pdf.pageCount += copiedPages.length
67
+ { model, base }
68
+ )
69
+
70
+ // 페이지 옵션 설정
71
+ const pageOptions = pdfUtil.buildPageOptions({
72
+ ...step.params,
73
+ width: width ? `${width}px` : undefined,
74
+ height: height ? `${height}px` : undefined
75
+ })
237
76
 
238
- await page.close()
239
- headlessPool.release(browser)
77
+ // PDF 생성 및 추가
78
+ const htmlContent = '' // 보드에서 직접 렌더링되므로 HTML 컨텐츠가 비어있음
79
+ await pdfUtil.processPageAndGeneratePDF({ ...step.params, width, height }, htmlContent)
240
80
 
241
81
  return {
242
- data: __headless_pdf
82
+ data: context.__headless_pdf
243
83
  }
244
84
  } catch (error) {
245
- if (browser) {
246
- await browser.close()
247
- }
248
-
249
85
  throw error
86
+ } finally {
87
+ await pdfUtil.closeBrowser()
250
88
  }
251
89
  }
252
90
 
253
91
  HeadlessPDFCaptureBoard.parameterSpec = [
254
- {
255
- type: 'scenario-step-input',
256
- name: 'accessor',
257
- label: 'accessor'
258
- },
92
+ ...getCommonParameterSpec(),
259
93
  {
260
94
  type: 'resource-object',
261
95
  name: 'board',
@@ -270,91 +104,6 @@ HeadlessPDFCaptureBoard.parameterSpec = [
270
104
  label: 'board-draft',
271
105
  defaultValue: false,
272
106
  description: 'Set whether to get the current working version or the last released version'
273
- },
274
- {
275
- type: 'select',
276
- name: 'format',
277
- label: 'page-format',
278
- property: {
279
- options: [
280
- { display: '', value: '' },
281
- { display: 'A4', value: 'A4' },
282
- { display: 'A3', value: 'A3' },
283
- { display: 'Letter', value: 'Letter' },
284
- { display: 'Legal', value: 'Legal' }
285
- ]
286
- },
287
- description: 'Select the paper format for the PDF'
288
- },
289
- {
290
- type: 'string',
291
- name: 'width',
292
- label: 'page-width',
293
- placeholder: '(e.g., "8.5in", "21cm", "600px")',
294
- description: 'Specify the width of the page (e.g., "8.5in", "21cm", "600px")'
295
- },
296
- {
297
- type: 'string',
298
- name: 'height',
299
- label: 'page-height',
300
- placeholder: '(e.g., "11in", "29.7cm", "800px")',
301
- description: 'Specify the height of the page (e.g., "11in", "29.7cm", "800px")'
302
- },
303
- {
304
- type: 'string',
305
- name: 'marginTop',
306
- label: 'page-margin-top',
307
- placeholder: '(e.g., "0.5in", "1cm", "100px")',
308
- description: 'Set the top margin for the page'
309
- },
310
- {
311
- type: 'string',
312
- name: 'marginBottom',
313
- label: 'page-margin-bottom',
314
- placeholder: '(e.g., "0.5in", "1cm", "100px")',
315
- description: 'Set the bottom margin for the page'
316
- },
317
- {
318
- type: 'string',
319
- name: 'marginLeft',
320
- label: 'page-margin-left',
321
- placeholder: '(e.g., "0.5in", "1cm", "100px")',
322
- description: 'Set the left margin for the page'
323
- },
324
- {
325
- type: 'string',
326
- name: 'marginRight',
327
- label: 'page-margin-right',
328
- placeholder: '(e.g., "0.5in", "1cm", "100px")',
329
- description: 'Set the right margin for the page'
330
- },
331
- {
332
- type: 'number',
333
- name: 'scale',
334
- label: 'page-scale',
335
- defaultValue: 1,
336
- description: 'Set the scale of the page content (default is 1)'
337
- },
338
- {
339
- type: 'boolean',
340
- name: 'printBackground',
341
- label: 'print-background',
342
- defaultValue: true,
343
- description: 'Include background graphics when printing the page'
344
- },
345
- {
346
- type: 'boolean',
347
- name: 'landscape',
348
- label: 'landscape',
349
- defaultValue: false,
350
- description: 'Print the PDF in landscape orientation'
351
- },
352
- {
353
- type: 'boolean',
354
- name: 'preferCSSPageSize',
355
- label: 'prefer-css-page-size',
356
- defaultValue: false,
357
- description: 'Whether to prefer the CSS-defined page size over the given width and height'
358
107
  }
359
108
  ]
360
109
 
@@ -0,0 +1,41 @@
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 } = step.params
13
+ const templateInput = access(accessor, context.data)
14
+
15
+ const renderedMarkdown = pdfUtil.renderTemplate(markdownContent, templateInput)
16
+ const htmlContent = marked(renderedMarkdown)
17
+
18
+ await pdfUtil.processPageAndGeneratePDF(step.params, htmlContent)
19
+
20
+ return {
21
+ data: context.__headless_pdf
22
+ }
23
+ } catch (error) {
24
+ throw error
25
+ } finally {
26
+ await pdfUtil.closeBrowser()
27
+ }
28
+ }
29
+
30
+ HeadlessPDFCaptureMarkdown.parameterSpec = [
31
+ ...getCommonParameterSpec(),
32
+ {
33
+ type: 'textarea',
34
+ name: 'markdownContent',
35
+ label: 'markdown-content'
36
+ }
37
+ ]
38
+
39
+ HeadlessPDFCaptureMarkdown.help = 'integration/task/headless-pdf-capture-markdown'
40
+
41
+ TaskRegistry.registerTaskHandler('headless-pdf-capture-markdown', HeadlessPDFCaptureMarkdown)