forked from vanielf/pdf-lib
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathtest29.html
136 lines (124 loc) · 3.83 KB
/
test29.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
<!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 29 - test matrix transform</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/test28.html'">
Prev
</button>
<button onclick="test()">Run Test</button>
<button onclick="window.location.href = '/apps/web/test30.html'">
Next
</button>
</div>
<!-- <div ></div> -->
<iframe id="iframe"></iframe>
</body>
<script type="text/javascript">
// startFpsTracker('animation-target');
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, PDFPage, radians, StandardFonts, rgb, degrees } =
PDFLib;
const pdfDoc = await PDFDocument.create();
const firstPage = pdfDoc.addPage([1000, 1000]);
const fontSize = 20;
const svg = `<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<rect width="100" height="100" fill="#ff0000"/>
<g transform="translate(50 0)">
<path
d="M 10,30
A 20,20 0,0,1 50,30
A 20,20 0,0,1 90,30
Q 90,60 50,90
Q 10,60 10,30 z" fill="#000000"/>
</g>
</svg>`;
const drawLines = (page) => {
Array(10)
.fill(1)
.map((v, i) => i)
.forEach((v) => {
page.drawText('----' + v, {
x: 5,
y: v * 100,
size: 20,
});
});
Array(100)
.fill(1)
.map((v, i) => i)
.forEach((v) => {
page.drawText(v % 5 == 0 ? '---' : '--', {
x: 5,
y: v * 10,
size: 20,
});
});
};
const drawGrid = (page) => {
Array(parseInt(page.getHeight() / 10))
.fill(1)
.map((v, i) => {
page.drawLine({
start: { x: 0, y: i * 10 },
end: { x: page.getWidth(), y: i * 10 },
});
});
Array(parseInt(page.getWidth() / 10))
.fill(1)
.map((v, i) => {
page.drawLine({
start: { x: i * 10, y: 0 },
end: { x: i * 10, y: page.getHeight() },
});
});
};
// drawGrid(firstPage)
drawLines(firstPage);
firstPage.drawSvg(svg, {
height: 100,
width: 100,
x: 100,
y: 700,
});
const svg2 = `<svg height="200" width="300">
<image width="300" height="200" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIAQMAAAD+wSzIAAAABlBMVEX///+/v7+jQ3Y5AAAADklEQVQI12P4AIX8EAgALgAD/aNpbtEAAAAASUVORK5CYII" />
</svg>`;
const pdfSvg = await pdfDoc.embedSvg(svg2);
firstPage.drawSvg(pdfSvg, {
height: 200,
width: 300,
x: 500,
y: 700,
});
firstPage.moveUp(600);
firstPage.moveRight(100);
firstPage.drawSvgPath(
'M 0,20 L 100,160 Q 130,200 150,120 C 190,-40 200,200 300,150 L 400,90',
);
const pdfBytes = await pdfDoc.save();
renderInIframe(pdfBytes);
}
</script>
</html>