forked from vanielf/pdf-lib
-
Notifications
You must be signed in to change notification settings - Fork 29
/
test30.html
143 lines (127 loc) · 4.07 KB
/
test30.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
<!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 - Draw Rectangles with PDF-Lib</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/test29.html'">
Prev
</button>
<button onclick="test()">Run Test</button>
<button onclick="window.location.href = '/apps/web/test31.html'">
Next
</button>
</div>
<iframe id="iframe"></iframe>
</body>
<script type="text/javascript">
const renderInIframe = (pdfBytes) => {
const blob = new Blob([pdfBytes], { type: 'application/pdf' });
const blobUrl = URL.createObjectURL(blob);
document.getElementById('iframe').src = blobUrl;
};
const svg = `<svg viewBox="0 0 100 50" xmlns="http://www.w3.org/2000/svg">
<rect width="100" height="50" fill="lightgrey" />
<rect x="5" y="5" width="140" height="40" rx="20" ry="20" fill="blue" stroke="black" stroke-width="3" />
</svg>`;
async function test() {
const { PDFDocument, rgb, degrees } = PDFLib;
const pdfDoc = await PDFDocument.create();
const page = pdfDoc.addPage([600, 600]);
// Helper function to add rectangles to the page
function drawRectangle(x, y, width, height, options) {
const {
color = undefined,
borderColor = undefined,
borderWidth = 1,
rx,
ry,
} = options;
const drawRect = page.drawRectangle;
drawRect.bind(page)({
x,
y,
width,
height,
color,
borderColor,
borderWidth,
rx,
ry,
});
}
const drawGrid = (page) => {
const n = 100;
Array(parseInt(page.getHeight() / n))
.fill(1)
.map((v, i) => {
page.drawLine({
start: { x: 0, y: i * n },
end: { x: page.getWidth(), y: i * n },
});
});
Array(parseInt(page.getWidth() / n))
.fill(1)
.map((v, i) => {
page.drawLine({
start: { x: i * n, y: 0 },
end: { x: i * n, y: page.getHeight() },
});
});
};
drawGrid(page);
// Draw a filled rectangle with sharp corners
drawRectangle(50, 500, 100, 50, {
color: rgb(1, 0, 0), // red
});
// Draw a rectangle with sharp corners and only a border
drawRectangle(200, 500, 100, 50, {
borderColor: rgb(0, 1, 0), // green
borderWidth: 3,
});
// Draw a filled rectangle with rounded corners in one dimension only
drawRectangle(50, 400, 100, 50, {
color: rgb(0, 0, 1), // blue
rx: 10,
});
// Draw a rectangle with rounded corners and only a border
drawRectangle(200, 400, 100, 50, {
borderColor: rgb(1, 0, 1), // purple
borderWidth: 3,
rx: 5,
ry: 10,
});
// Draw a rectangle with a very large borderRadius to test maximum rounding
drawRectangle(50, 300, 100, 50, {
color: rgb(0.5, 0.5, 0),
borderColor: rgb(0, 0, 0),
borderWidth: 2,
rx: 25, // Half of the smallest dimension
ry: 25, // Half of the smallest dimension
});
page.drawSvg(svg, { x: 200, y: 300, width: 100, height: 50 });
drawRectangle(300, 300, 100, 50, {
color: rgb(0.5, 0.5, 0),
rx: 25,
ry: 25,
});
const pdfBytes = await pdfDoc.save();
renderInIframe(pdfBytes);
}
</script>
</html>