forked from Hopding/pdf-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test6.ts
128 lines (113 loc) · 3.55 KB
/
test6.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
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
import fontkit from '@pdf-lib/fontkit';
import { Assets } from '..';
import { degrees, ParseSpeeds, PDFDocument, rgb } from '../../..';
export default async (assets: Assets) => {
const { pdfs, images, fonts } = assets;
const pdfDoc = await PDFDocument.load(
pdfs.with_missing_endstream_eol_and_polluted_ctm,
{ parseSpeed: ParseSpeeds.Fastest },
);
pdfDoc.registerFontkit(fontkit);
await pdfDoc.attach(pdfs.us_constitution, 'us_constitution.pdf', {
mimeType: 'application/pdf',
description: 'Constitution of the United States 🇺🇸🦅',
creationDate: new Date('1787/09/17'),
modificationDate: new Date('1992/05/07'),
});
await pdfDoc.attach(
images.jpg.cat_riding_unicorn_base64,
'cat_riding_unicorn.jpg',
{
mimeType: 'image/jpeg',
description: 'Cool cat riding a unicorn! 🦄🐈🕶️',
creationDate: new Date('2019/12/01'),
modificationDate: new Date('2020/04/19'),
},
);
const nunitoLigaFont = await pdfDoc.embedFont(fonts.ttf.nunito, {
subset: true,
features: { liga: true },
});
const nunitoNoLigaFont = await pdfDoc.embedFont(fonts.ttf.nunito, {
subset: true,
features: { liga: false },
});
const smallMarioImage = await pdfDoc.embedPng(images.png.small_mario);
const smallMarioDims = smallMarioImage.scale(0.15);
const page1 = pdfDoc.getPage(0);
const text =
'This is an image of Mario running. This image and text was drawn on top of an existing PDF using pdf-lib!';
const fontSize = 24;
const solarizedWhite = rgb(253 / 255, 246 / 255, 227 / 255);
const solarizedGray = rgb(101 / 255, 123 / 255, 131 / 255);
const boxWidth = 387.5;
const { width, height } = page1.getSize();
const centerX = width / 2;
const centerY = height / 2;
page1.drawImage(smallMarioImage, {
...smallMarioDims,
x: centerX - smallMarioDims.width / 2,
y: centerY - 15,
});
page1.drawImage(smallMarioImage, {
...smallMarioDims,
x: centerX + smallMarioDims.width / 2,
y: centerY,
rotate: degrees(180),
xSkew: degrees(35),
ySkew: degrees(35),
});
const boxHeight = (fontSize + 5) * 3;
page1.drawRectangle({
x: centerX - boxWidth / 2 - 5,
y: centerY - 60 - boxHeight + fontSize + 3,
width: boxWidth + 10,
height: boxHeight,
color: solarizedWhite,
borderColor: solarizedGray,
borderWidth: 3,
rotate: degrees(10),
ySkew: degrees(15),
});
page1.setFont(nunitoLigaFont);
page1.setFontColor(solarizedGray);
page1.drawText(text, {
x: centerX - boxWidth / 2 + 5,
y: centerY - 60,
rotate: degrees(10),
ySkew: degrees(15),
maxWidth: boxWidth + 5,
});
page1.setSize(page1.getWidth() + 100, page1.getHeight() + 100);
page1.translateContent(100, 100);
page1.setFont(nunitoLigaFont);
page1.drawText('This text is shifted - fi', {
color: rgb(1, 0, 0),
size: 50,
});
page1.resetPosition();
page1.setFont(nunitoNoLigaFont);
page1.drawText('This text is not shifted - fi', {
color: rgb(0, 0, 1),
size: 50,
});
// Add page with CropBox
const pdfDocWithCropBox = await PDFDocument.load(pdfs.with_cropbox);
const [page2] = await pdfDoc.copyPages(pdfDocWithCropBox, [0]);
pdfDoc.addPage(page2);
page2.drawRectangle({
x: page2.getWidth(),
width: 50,
height: page2.getHeight(),
color: rgb(1, 0, 0),
});
page2.drawRectangle({
y: page2.getHeight(),
height: 50,
width: page2.getWidth(),
color: rgb(0, 1, 0),
});
page2.setSize(page2.getWidth() + 50, page2.getHeight() + 50);
const pdfBytes = await pdfDoc.save();
return pdfBytes;
};