forked from Hopding/pdf-lib
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest4.js
86 lines (69 loc) · 2.27 KB
/
test4.js
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 {
PDFDocument,
PDFPage,
radians,
StandardFonts,
rgb,
degrees,
} from 'pdf-lib';
import { fetchAsset, writePdf } from './assets';
export default async () => {
const [inputPdf, minionsLaughingBytes] = await Promise.all([
fetchAsset('pdfs/normal.pdf'),
fetchAsset('images/minions_laughing.jpg'),
]);
const pdfDoc = await PDFDocument.load(inputPdf);
const minionsLaughingImage = await pdfDoc.embedJpg(minionsLaughingBytes);
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((fontName, idx) => {
middlePage.moveDown(fontSize);
const font = pdfDoc.embedStandardFont(StandardFonts[fontName]);
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) => {
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 base64Pdf = await pdfDoc.saveAsBase64({ dataUri: true });
return { base64Pdf };
// const pdfBytes = await pdfDoc.save();
// const path = await writePdf(pdfBytes);
// return { base64Pdf: `file://${path}` };
};