-
-
Notifications
You must be signed in to change notification settings - Fork 676
/
test4.ts
86 lines (71 loc) · 2.23 KB
/
test4.ts
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
import { Assets } from '../index.ts';
// @deno-types="../dummy.d.ts"
import {
ParseSpeeds,
PDFDocument,
PDFPage,
radians,
StandardFonts,
rgb,
degrees,
} from '../../../dist/pdf-lib.esm.js';
export default async (assets: Assets) => {
const { pdfs, images } = assets;
const pdfDoc = await PDFDocument.load(pdfs.normal_base64, {
parseSpeed: ParseSpeeds.Fastest,
});
const minionsLaughingImage = await pdfDoc.embedJpg(
images.jpg.minions_laughing,
);
const minionsLaughingDims = minionsLaughingImage.scale(0.6);
const firstPage = pdfDoc.getPage(0);
const middlePage = pdfDoc.insertPage(1, [600, 500]);
const lastPage = pdfDoc.getPage(2);
const fontSize = 20;
middlePage.setFontSize(fontSize);
middlePage.moveTo(0, middlePage.getHeight());
Object.keys(StandardFonts).forEach((fontNameStr, idx) => {
middlePage.moveDown(fontSize);
const fontName = fontNameStr as keyof typeof StandardFonts;
const fontObj = StandardFonts[fontName];
const font = pdfDoc.embedStandardFont(fontObj);
middlePage.setFont(font);
// prettier-ignore
const text = (
fontName === StandardFonts.Symbol ? `${idx + 1}. Τηεσε αρε τηε 14 Στανδαρδ Φοντσ.`
: fontName === StandardFonts.ZapfDingbats ? `✑✔✎ ✴❈❅▲❅ ❁❒❅ ▼❈❅ ✑✔ ✳▼❁■❄❁❒❄ ✦❏■▼▲✎`
: `${idx + 1}. These are the 14 Standard Fonts.`
);
middlePage.drawText(text, {
rotate: radians(-Math.PI / 6),
xSkew: radians(Math.PI / 10),
ySkew: radians(Math.PI / 10),
});
});
middlePage.drawEllipse({
x: 450,
y: 225,
xScale: 25,
yScale: 150,
color: rgb(0, 1, 0),
borderWidth: 2,
borderColor: rgb(1, 0, 1),
rotate: degrees(45),
opacity: 0.5,
});
const stampImage = (page: PDFPage) => {
const { width, height } = page.getSize();
const centerX = width / 2;
const centerY = height / 2;
page.drawImage(minionsLaughingImage, {
...minionsLaughingDims,
x: centerX - minionsLaughingDims.width / 2,
y: centerY - minionsLaughingDims.height / 2,
opacity: 0.75,
});
};
stampImage(firstPage);
stampImage(lastPage);
const pdfBytes = await pdfDoc.save();
return pdfBytes;
};