forked from Hopding/pdf-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest15.html
203 lines (179 loc) · 6.75 KB
/
test15.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta
http-equiv="Content-Security-Policy"
content="
default-src 'self' 'unsafe-inline' blob: resource:;
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 15</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/test14.html'">
Prev
</button>
<button onclick="test()">Run Test</button>
<button onclick="window.location.href = '/apps/web/test16.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 renderInIframe = (pdfBytes) => {
const blob = new Blob([pdfBytes], { type: 'application/pdf' });
const blobUrl = URL.createObjectURL(blob);
document.getElementById('iframe').src = blobUrl;
};
async function test() {
const { PDFDocument, StandardFonts, rgb, TextAlignment } = PDFLib;
const [
dodCharacterPdf,
smallMarioPng,
marioEmblemPng,
] = await Promise.all([
fetchBinaryAsset('pdfs/dod_character.pdf'),
fetchBinaryAsset('images/small_mario.png'),
fetchBinaryAsset('images/mario_emblem.png'),
]);
const pdfDoc = await PDFDocument.load(dodCharacterPdf);
const marioImage = await pdfDoc.embedPng(smallMarioPng);
const emblemImage = await pdfDoc.embedPng(marioEmblemPng);
const form = pdfDoc.getForm();
form.getTextField('CharacterName 2').setText('Mario');
form.getTextField('Age').setText('24 years');
form.getTextField('Height').setText(`5' 1"`);
form.getTextField('Weight').setText('196 lbs');
form.getTextField('Eyes').setText('blue');
form.getTextField('Skin').setText('white');
form.getTextField('Hair').setText('brown');
form.getButton('CHARACTER IMAGE').setImage(marioImage);
form
.getTextField('Allies')
.setText(
[
`Allies:`,
` • Princess Daisy`,
` • Princess Peach`,
` • Rosalina`,
` • Geno`,
` • Luigi`,
` • Donkey Kong`,
` • Yoshi`,
` • Diddy Kong`,
``,
`Organizations:`,
` • Italian Plumbers Association`,
].join('\n'),
);
form.getTextField('FactionName').setText(`Mario's Emblem`);
form.getTextField('Faction Symbol Image').setImage(emblemImage);
form
.getTextField('Backstory')
.setText(
`Mario is a fictional character in the Mario video game franchise, owned by Nintendo and created by Japanese video game designer Shigeru Miyamoto. Serving as the company's mascot and the eponymous protagonist of the series, Mario has appeared in over 200 video games since his creation. Depicted as a short, pudgy, Italian plumber who resides in the Mushroom Kingdom, his adventures generally center upon rescuing Princess Peach from the Koopa villain Bowser. His younger brother and sidekick is Luigi.`,
);
form
.getTextField('Feat+Traits')
.setText(
[
`Mario can use three basic three power-ups:`,
` • the Super Mushroom, which causes Mario to grow larger`,
` • the Fire Flower, which allows Mario to throw fireballs`,
` • the Starman, which gives Mario temporary invincibility`,
].join('\n'),
);
form
.getTextField('Treasure')
.setText(['• Gold coins', '• Treasure chests'].join('\n'));
// Add new page with custom form fields to exercise options not used in test1
const { width, height } = pdfDoc.getPage(0).getSize();
const page2 = pdfDoc.addPage([width, height]);
// Singleline, centered
const singlelineCenteredTf = form.createTextField(
'singleline.centered.tf',
);
singlelineCenteredTf.setAlignment(TextAlignment.Center);
singlelineCenteredTf.setText('Sum centered text yo');
singlelineCenteredTf.addToPage(page2, {
y: height - 50,
width: 250,
height: 25,
borderWidth: 3,
borderColor: rgb(1, 0, 1),
});
// Multiline, centered
const multilineCenteredTf = form.createTextField('multiline.centered.tf');
multilineCenteredTf.enableMultiline();
multilineCenteredTf.setAlignment(TextAlignment.Center);
multilineCenteredTf.setText('Sum\ncentered\rtext\nyo');
multilineCenteredTf.addToPage(page2, {
y: height - 50 - 150,
width: 250,
height: 100,
borderWidth: 3,
borderColor: rgb(1, 0, 1),
});
// Singleline, right justified
const singlelineRightTf = form.createTextField('singleline.right.tf');
singlelineRightTf.setAlignment(TextAlignment.Right);
singlelineRightTf.setText('Sum right justified text yo');
singlelineRightTf.addToPage(page2, {
y: height - 50,
x: 300,
width: 250,
height: 25,
borderWidth: 3,
borderColor: rgb(1, 0, 1),
});
// Multiline, right justified
const multilineRightTf = form.createTextField('multiline.right.tf');
multilineRightTf.enableMultiline();
multilineRightTf.setAlignment(TextAlignment.Right);
multilineRightTf.setText('Sum\nright justified\rtext\nyo');
multilineRightTf.addToPage(page2, {
y: height - 50 - 150,
x: 300,
width: 250,
height: 100,
borderWidth: 3,
borderColor: rgb(1, 0, 1),
});
// Multiselect Option List
const optionList = form.createOptionList('option.list');
optionList.addToPage(page2, {
y: height - 50 - 150 - 250,
width: 250,
height: 200,
backgroundColor: rgb(1, 0.25, 0.25),
borderWidth: 5,
borderColor: rgb(1, 0, 1),
});
optionList.setOptions([
'Sojourner',
'Spirit',
'Opportunity',
'Curiosity',
'Perseverance',
]);
optionList.enableMultiselect();
optionList.select(['Sojourner', 'Curiosity', 'Perseverance']);
const pdfBytes = await pdfDoc.save();
renderInIframe(pdfBytes);
}
</script>
</html>