forked from Hopding/pdf-lib
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test8.html
88 lines (74 loc) · 2.63 KB
/
test8.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
<!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 8</title>
<script type="text/javascript" src="/dist/pdf-lib.min.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/test7.html'">
Prev
</button>
<button onclick="test()">Run Test</button>
<button onclick="window.location.href = '/apps/web/test9.html'">
Next
</button>
</div>
<div id="animation-target"></div>
<iframe id="iframe"></iframe>
</body>
<script type="text/javascript">
startFpsTracker('animation-target');
const fetchAsset = (asset) =>
fetch(`/assets/${asset}`)
.then((res) => res.arrayBuffer())
.then((res) => new Uint8Array(res));
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, rgb, StandardFonts } = PDFLib;
const [inputPdfBytes] = await Promise.all([
fetchAsset(
'pdfs/with_newline_whitespace_in_indirect_object_numbers.pdf',
),
]);
const pdfDoc = await PDFDocument.load(inputPdfBytes);
const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica);
const pages = pdfDoc.getPages();
const [firstPage] = pages;
const { width, height } = firstPage.getSize();
const text = 'pdf-lib is awesome!';
const textWidth = helveticaFont.widthOfTextAtSize(text, 75);
firstPage.moveTo(width / 2 - textWidth / 2, height - 100);
firstPage.setFont(helveticaFont);
firstPage.setFontSize(75);
firstPage.setFontColor(rgb(1, 0, 0));
firstPage.drawText(text);
pages.forEach((page, idx) => {
page.moveTo(10, 10);
page.setFont(helveticaFont);
page.setFontSize(17);
page.setFontColor(rgb(1, 0, 0));
page.drawText(`${idx + 1} / ${pages.length}`);
});
const pdfBytes = await pdfDoc.save();
renderInIframe(pdfBytes);
}
</script>
</html>