@softwear/latestcollectioncore 1.0.175 → 1.0.177
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/dist/index.d.ts +3 -0
- package/dist/index.js +5 -1
- package/dist/pdf.d.ts +69 -0
- package/dist/pdf.js +2 -0
- package/dist/reports.d.ts +35 -0
- package/dist/reports.js +724 -0
- package/package.json +3 -2
- package/src/index.ts +3 -0
- package/src/pdf.ts +78 -0
- package/src/reports.ts +859 -0
- package/test/reports.spec.ts +141 -0
- package/test.pdf +0 -0
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { genPDF } from '../src/reports'
|
|
3
|
+
import type { Layout } from '../src/pdf'
|
|
4
|
+
// import fs from 'fs'
|
|
5
|
+
|
|
6
|
+
const imageUrl = 'https://upload.wikimedia.org/wikipedia/commons/thumb/7/79/Phillips_PM5538.jpg/500px-Phillips_PM5538.jpg'
|
|
7
|
+
|
|
8
|
+
describe('genPDF', () => {
|
|
9
|
+
it('creates a PDF with an embedded image from a minimal layout and print buffer', async () => {
|
|
10
|
+
const layout: Layout = {
|
|
11
|
+
name: 'minimal',
|
|
12
|
+
paperSize: {
|
|
13
|
+
width: 210,
|
|
14
|
+
height: 297,
|
|
15
|
+
footerHeight: 20,
|
|
16
|
+
},
|
|
17
|
+
objects: [
|
|
18
|
+
{
|
|
19
|
+
type: 'text',
|
|
20
|
+
name: 'title-label',
|
|
21
|
+
x: 20,
|
|
22
|
+
y: 20,
|
|
23
|
+
width: 500,
|
|
24
|
+
height: 50,
|
|
25
|
+
active: true,
|
|
26
|
+
text: 'Report:',
|
|
27
|
+
textAlign: 1,
|
|
28
|
+
fontFamily: 'Helvetica',
|
|
29
|
+
fontSize: 32,
|
|
30
|
+
fontStyle: 0,
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
type: 'field',
|
|
34
|
+
name: 'title',
|
|
35
|
+
x: 180,
|
|
36
|
+
y: 20,
|
|
37
|
+
width: 500,
|
|
38
|
+
height: 50,
|
|
39
|
+
active: true,
|
|
40
|
+
source: 'title',
|
|
41
|
+
textAlign: 1,
|
|
42
|
+
fontFamily: 'Helvetica',
|
|
43
|
+
fontSize: 32,
|
|
44
|
+
fontStyle: 0,
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
type: 'image',
|
|
48
|
+
name: 'test-pattern',
|
|
49
|
+
x: 20,
|
|
50
|
+
y: 90,
|
|
51
|
+
width: 200,
|
|
52
|
+
height: 200,
|
|
53
|
+
active: true,
|
|
54
|
+
url: imageUrl,
|
|
55
|
+
},
|
|
56
|
+
],
|
|
57
|
+
}
|
|
58
|
+
const printBuffer = {
|
|
59
|
+
title: 'Minimal PDF',
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const doc = await genPDF(layout, printBuffer)
|
|
63
|
+
const pdf = doc.output('arraybuffer')
|
|
64
|
+
const pdfText = Buffer.from(pdf).toString('latin1')
|
|
65
|
+
|
|
66
|
+
expect(doc.getNumberOfPages()).toBe(1)
|
|
67
|
+
expect(pdf.byteLength).toBeGreaterThan(0)
|
|
68
|
+
expect(pdfText).toContain('/Subtype /Image')
|
|
69
|
+
expect(pdfText).toContain('/Filter /DCTDecode')
|
|
70
|
+
expect(pdfText).toContain('Report:')
|
|
71
|
+
expect(pdfText).toContain(printBuffer.title)
|
|
72
|
+
// fs.writeFileSync('test.pdf', Buffer.from(pdf))
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it('draws printOnlyAtStart before body containers and printOnlyAtEnd after', async () => {
|
|
76
|
+
const textChild = (name: string, y: number, label: string) =>
|
|
77
|
+
({
|
|
78
|
+
type: 'text' as const,
|
|
79
|
+
name,
|
|
80
|
+
x: 20,
|
|
81
|
+
y,
|
|
82
|
+
width: 400,
|
|
83
|
+
height: 40,
|
|
84
|
+
active: true,
|
|
85
|
+
text: label,
|
|
86
|
+
textAlign: 1 as const,
|
|
87
|
+
fontFamily: 'Helvetica',
|
|
88
|
+
fontSize: 24,
|
|
89
|
+
fontStyle: 0,
|
|
90
|
+
}) as const
|
|
91
|
+
|
|
92
|
+
const layout: Layout = {
|
|
93
|
+
name: 'order-test',
|
|
94
|
+
paperSize: { width: 210, height: 297, footerHeight: 20 },
|
|
95
|
+
objects: [
|
|
96
|
+
{
|
|
97
|
+
type: 'container',
|
|
98
|
+
x: 0,
|
|
99
|
+
y: 40,
|
|
100
|
+
width: 500,
|
|
101
|
+
height: 80,
|
|
102
|
+
active: true,
|
|
103
|
+
source: '',
|
|
104
|
+
printOnlyAtStart: true,
|
|
105
|
+
children: [textChild('start', 0, 'START_ONLY')],
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
type: 'container',
|
|
109
|
+
x: 0,
|
|
110
|
+
y: 120,
|
|
111
|
+
width: 500,
|
|
112
|
+
height: 80,
|
|
113
|
+
active: true,
|
|
114
|
+
source: '',
|
|
115
|
+
children: [textChild('mid', 0, 'BODY_MIDDLE')],
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
type: 'container',
|
|
119
|
+
x: 0,
|
|
120
|
+
y: 200,
|
|
121
|
+
width: 500,
|
|
122
|
+
height: 80,
|
|
123
|
+
active: true,
|
|
124
|
+
source: '',
|
|
125
|
+
printOnlyAtEnd: true,
|
|
126
|
+
children: [textChild('end', 0, 'END_ONLY')],
|
|
127
|
+
},
|
|
128
|
+
],
|
|
129
|
+
}
|
|
130
|
+
const doc = await genPDF(layout, {})
|
|
131
|
+
const pdfText = Buffer.from(doc.output('arraybuffer')).toString('latin1')
|
|
132
|
+
const iStart = pdfText.indexOf('(START_ONLY)')
|
|
133
|
+
const iMid = pdfText.indexOf('(BODY_MIDDLE)')
|
|
134
|
+
const iEnd = pdfText.indexOf('(END_ONLY)')
|
|
135
|
+
expect(iStart).toBeGreaterThanOrEqual(0)
|
|
136
|
+
expect(iMid).toBeGreaterThanOrEqual(0)
|
|
137
|
+
expect(iEnd).toBeGreaterThanOrEqual(0)
|
|
138
|
+
expect(iStart).toBeLessThan(iMid)
|
|
139
|
+
expect(iMid).toBeLessThan(iEnd)
|
|
140
|
+
})
|
|
141
|
+
})
|
package/test.pdf
ADDED
|
Binary file
|