/
test12.html
168 lines (150 loc) · 4.86 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
<!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">
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);
async function test() {
const { PageSizes, PDFDocument, rgb } = PDFLib;
const pdfDoc = await PDFDocument.create();
const page1 = pdfDoc.addPage(PageSizes.Letter);
// SVG sample paths from
// https://svgwg.org/svg2-draft/paths.html
// bezier curve example
page1.drawSvgPath('M100,200 C100,100 250,100 250,200 S400,300 400,200', {
x: inchToPt(0.25),
y: inchToPt(12),
});
// downward facing triangle
page1.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
page1.drawSvgPath('M100,200 C100,100 400,100 400,200', {
x: inchToPt(-1),
y: inchToPt(9.25),
});
page1.drawSvgPath('M600,200 C675,100 975,100 900,200', {
x: inchToPt(-4.5),
y: inchToPt(8.25),
});
page1.drawSvgPath('M100,500 C25,400 475,400 400,500', {
x: inchToPt(-1),
y: inchToPt(11.25),
});
page1.drawSvgPath('M600,500 C600,350 900,650 900,500', {
x: inchToPt(-4.5),
y: inchToPt(10.25),
});
page1.drawSvgPath('M100,800 C175,700 325,700 400,800', {
x: inchToPt(-1),
y: inchToPt(13.35),
});
page1.drawSvgPath('M600,800 C625,700 725,700 750,800 S875,900 900,800', {
x: inchToPt(-4.5),
y: inchToPt(12.25),
});
const page2 = pdfDoc.addPage(PageSizes.Letter);
// quadratic bezier example
page2.drawSvgPath('M200,300 Q400,50 600,300 T1000,300', {
x: inchToPt(-1),
y: inchToPt(11),
scale: 0.5,
borderWidth: 2,
});
page2.drawSvgPath('M200,300 L400,50 L600,300 L800,550 L1000,300', {
x: inchToPt(-1),
y: inchToPt(9),
scale: 0.5,
borderWidth: 2,
});
// arc examples
page2.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,
});
page2.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,
});
page2.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,
},
);
page2.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,
});
page2.drawText('Semi-Transparent Text', {
color: rgb(0, 1, 1),
opacity: 0.5,
x: inchToPt(1),
y: inchToPt(2.5),
size: 50,
});
const pdfBytes = await pdfDoc.save();
renderInIframe(pdfBytes);
}
</script>
</html>