forked from Hopding/pdf-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test12.html
341 lines (301 loc) · 9.12 KB
/
test12.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<!DOCTYPE html>
<html lang="en">
<head>
<meta
http-equiv="Content-Security-Policy"
content="
default-src 'self' 'unsafe-inline' blob: resource: https://unpkg.com/@pdf-lib/fontkit/dist/fontkit.umd.js;
object-src 'self' blob:;
frame-src 'self' blob:;
"
/>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link rel="stylesheet" type="text/css" href="/apps/web/index.css" />
<title>Test 12</title>
<script type="text/javascript" src="/dist/pdf-lib.js"></script>
<script type="text/javascript" src="/apps/web/utils.js"></script>
</head>
<body>
<div id="button-container">
<button onclick="window.location.href = '/apps/web/test11.html'">
Prev
</button>
<button onclick="test()">Run Test</button>
<button onclick="window.location.href = '/apps/web/test13.html'">
Next
</button>
</div>
<div id="animation-target"></div>
<iframe id="iframe"></iframe>
</body>
<script type="text/javascript">
const {
PageSizes,
PDFDocument,
LineCapStyle,
rgb,
cmyk,
BlendMode,
degrees,
values,
} = PDFLib;
startFpsTracker('animation-target');
const fetchBinaryAsset = (asset) =>
fetch(`/assets/${asset}`).then((res) => res.arrayBuffer());
const fetchStringAsset = (asset) =>
fetch(`/assets/${asset}`).then((res) => res.text());
const renderInIframe = (pdfBytes) => {
const blob = new Blob([pdfBytes], {
type: 'application/pdf',
});
const blobUrl = URL.createObjectURL(blob);
document.getElementById('iframe').src = blobUrl;
};
const inchToPt = (inches) => Math.round(inches * 72);
const firstPage = async (pdfDoc) => {
const page = pdfDoc.addPage(PageSizes.Letter);
// SVG sample paths from
// https://svgwg.org/svg2-draft/paths.html
// bezier curve example
page.drawSvgPath('M100,200 C100,100 250,100 250,200 S400,300 400,200', {
x: inchToPt(0.25),
y: inchToPt(12),
});
// downward facing triangle
page.drawSvgPath('M 100 100 L 300 100 L 200 300 z', {
x: inchToPt(-1),
y: inchToPt(12),
color: rgb(1, 0, 0),
borderColor: rgb(0, 0, 1),
borderWidth: 1,
opacity: 0.7,
borderOpacity: 0.7,
});
// bezier control point adjustments
page.drawSvgPath('M100,200 C100,100 400,100 400,200', {
x: inchToPt(-1),
y: inchToPt(9.25),
});
page.drawSvgPath('M600,200 C675,100 975,100 900,200', {
x: inchToPt(-4.5),
y: inchToPt(8.25),
});
page.drawSvgPath('M100,500 C25,400 475,400 400,500', {
x: inchToPt(-1),
y: inchToPt(11.25),
});
page.drawSvgPath('M600,500 C600,350 900,650 900,500', {
x: inchToPt(-4.5),
y: inchToPt(10.25),
});
page.drawSvgPath('M100,800 C175,700 325,700 400,800', {
x: inchToPt(-1),
y: inchToPt(13.35),
});
page.drawSvgPath('M600,800 C625,700 725,700 750,800 S875,900 900,800', {
x: inchToPt(-4.5),
y: inchToPt(12.25),
});
};
const secondPage = async (pdfDoc) => {
const page = pdfDoc.addPage(PageSizes.Letter);
// quadratic bezier example
page.drawSvgPath('M200,300 Q400,50 600,300 T1000,300', {
x: 100,
y: 700,
scale: 0.5,
borderWidth: 4,
borderDashArray: [24, 12],
borderLineCap: LineCapStyle.Round,
rotate: degrees(30),
});
page.drawSvgPath('M200,300 L400,50 L600,300 L800,550 L1000,300', {
x: inchToPt(-1),
y: inchToPt(9),
scale: 0.5,
borderWidth: 2,
});
// arc examples
page.drawSvgPath('M300,200 h-150 a150,150 0 1,0 150,-150 z', {
x: inchToPt(-1),
y: inchToPt(5.5),
color: rgb(1, 0, 0),
borderColor: rgb(0, 0, 1),
borderWidth: 1,
});
page.drawSvgPath('M275,175 v-150 a150,150 0 0,0 -150,150 z', {
x: inchToPt(-1),
y: inchToPt(5.5),
color: rgb(1, 1, 0),
borderColor: rgb(0, 0, 1),
borderWidth: 1,
});
page.drawSvgPath(
'M600,350 l 50,-25 a25,25 -30 0,1 50,-25 l 50,-25 a25,50 -30 0,1 50,-25 l 50,-25 a25,75 -30 0,1 50,-25 l 50,-25 a25,100 -30 0,1 50,-25 l 50,-25',
{
x: inchToPt(1),
y: inchToPt(3),
scale: 0.5,
borderColor: rgb(1, 0, 0),
borderWidth: 2,
},
);
page.drawCircle({
x: inchToPt(3),
y: inchToPt(5),
color: rgb(0, 1, 1),
opacity: 0.1,
borderWidth: 3,
borderColor: rgb(1, 0, 1),
borderOpacity: 0.2,
});
page.drawText('Semi-Transparent Text', {
color: rgb(0, 1, 1),
opacity: 0.5,
x: inchToPt(1),
y: inchToPt(2.5),
size: 50,
});
};
const thirdPage = async (pdfDoc, assets) => {
const page = pdfDoc.addPage(PageSizes.Letter);
const modeNames = values(BlendMode);
page.drawRectangle({
x: 30,
y: 30,
width: 100,
height: 732,
color: cmyk(0, 0.7, 0.3, 0),
blendMode: BlendMode.Normal,
});
page.drawRectangle({
x: 340,
y: 30,
width: 100,
height: 732,
color: cmyk(0.6, 0, 0.3, 0),
blendMode: BlendMode.Normal,
});
page.drawText(`pdf-lib Blend Mode Test`, {
size: 24,
x: 45,
y: 735,
color: cmyk(0.75, 0, 0, 0),
blendMode: BlendMode.Multiply,
});
// List all blend modes available
modeNames.forEach((m, i) => {
page.drawText(`blendMode: ${m}`, {
size: 14,
x: 40,
y: 700 - i * 20,
color: cmyk(0, 0, 0, 0.65),
blendMode: m,
});
});
// quadratic bezier example
page.drawSvgPath('M200,300 Q400,50 600,300 T1000,300', {
x: inchToPt(-1),
y: inchToPt(10),
scale: 0.5,
borderWidth: 6,
borderColor: cmyk(0, 0, 0, 1),
blendMode: BlendMode.Overlay,
});
// arc examples
page.drawSvgPath('M300,200 h-150 a150,150 0 1,0 150,-150 z', {
x: inchToPt(-1),
y: inchToPt(5.5),
color: cmyk(0, 1, 1, 0),
borderColor: cmyk(1, 0.7, 0, 0),
borderWidth: 2,
blendMode: BlendMode.HardLight,
});
page.drawSvgPath('M275,175 v-150 a150,150 0 0,0 -150,150 z', {
x: inchToPt(-1),
y: inchToPt(5.5),
color: cmyk(0, 0.3, 1, 0),
borderColor: cmyk(0, 0, 0, 1),
borderWidth: 2,
blendMode: BlendMode.Darken,
});
// rectangle example
page.drawRectangle({
x: 350,
y: 220,
width: 60,
height: 160,
rotate: degrees(-30),
color: cmyk(0.4, 0, 1, 0),
borderColor: cmyk(0, 1, 1, 0),
borderWidth: 2,
blendMode: BlendMode.ColorBurn,
});
// circle example
page.drawCircle({
x: inchToPt(3),
y: inchToPt(5),
color: cmyk(0.7, 1, 0.5, 0),
blendMode: BlendMode.ColorDodge,
});
// add alpha-image with 'Screen' blend mode
// const pngImage = await pdfDoc.embedPng(assets.images.png.self_drive);
const pngImage = await pdfDoc.embedPng(assets.selfDrivePngBytes);
const pngDims = pngImage.scale(1.0);
const cx = page.getWidth() / 2 - 70;
const cy = page.getHeight() / 2;
page.drawImage(pngImage, {
x: cx - pngDims.width / 2,
y: cy - pngDims.height / 2,
width: pngDims.width,
height: pngDims.height,
blendMode: BlendMode.Screen,
});
// embed page from other PDF using blendMode
const [embeddedPage] = await pdfDoc.embedPdf(
// assets.pdfs.simple_pdf_2_example,
assets.simplePdf2ExampleBytes,
[0],
);
const [px, py, scale] = [300, 100, 0.33];
const { width, height } = embeddedPage.scale(scale);
page.drawPage(embeddedPage, {
x: px,
y: py,
xScale: scale,
yScale: scale,
blendMode: BlendMode.Multiply,
});
page.drawRectangle({
x: px,
y: py,
width,
height,
borderColor: cmyk(0, 1, 1, 0),
borderWidth: 2,
blendMode: BlendMode.Normal,
});
page.drawText('Embedded PDF document (blendMode: Multiply)', {
size: 9,
x: px,
y: py - 12,
color: cmyk(0, 0, 0, 1),
blendMode: BlendMode.Multiply,
});
};
async function test() {
const [selfDrivePngBytes, simplePdf2ExampleBytes] = await Promise.all([
fetchBinaryAsset('images/self_drive.png'),
fetchBinaryAsset('pdfs/pdf20examples/Simple PDF 2.0 file.pdf'),
]);
const pdfDoc = await PDFDocument.create();
await firstPage(pdfDoc);
await secondPage(pdfDoc);
await thirdPage(pdfDoc, { selfDrivePngBytes, simplePdf2ExampleBytes });
const pdfBytes = await pdfDoc.save();
renderInIframe(pdfBytes);
}
</script>
</html>