forked from Hopding/pdf-lib
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest3.js
141 lines (121 loc) · 3.75 KB
/
test3.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
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
import {
degrees,
PDFDocument,
rgb,
StandardFonts,
LineCapStyle,
} from 'pdf-lib';
import { fetchAsset, writePdf } from './assets';
export default async () => {
const [
inputPdfBytes,
catRidingUnicornBytes,
cmykBytes,
normalPdfBase64,
existingPdfBytes,
] = await Promise.all([
fetchAsset('pdfs/with_update_sections.pdf'),
fetchAsset('images/cat_riding_unicorn_resized.jpg'),
fetchAsset('images/cmyk_colorspace.jpg'),
fetchAsset('pdfs/normal.pdf'),
fetchAsset('pdfs/with_annots.pdf'),
]);
const pdfDoc = await PDFDocument.load(inputPdfBytes, {
updateMetadata: false,
});
await pdfDoc.attach(normalPdfBase64, 'tax_form.pdf', {
mimeType: 'application/pdf',
description: 'D-2210 tax form for 2012 🏦',
creationDate: new Date('2004/04/04'),
modificationDate: new Date('2005/05/05'),
});
const helveticaFont = await pdfDoc.embedFont(StandardFonts.HelveticaBold);
const catRidingUnicornImage = await pdfDoc.embedJpg(catRidingUnicornBytes);
const cmykImage = await pdfDoc.embedJpg(cmykBytes);
const catRidingUnicornDims = catRidingUnicornImage.scale(0.25);
const cmykDims = cmykImage.scale(0.5);
const page0 = pdfDoc.insertPage(0, [305, 250]);
const page1 = pdfDoc.getPage(1);
const docWithAnnots = await PDFDocument.load(existingPdfBytes);
const [page2] = await pdfDoc.copyPages(docWithAnnots, [0]);
page2.scaleContent(0.5, 0.5);
pdfDoc.addPage(page2);
const [page3] = await pdfDoc.copyPages(docWithAnnots, [0]);
page3.scaleAnnotations(0.5, 0.5);
pdfDoc.addPage(page3);
const [page4] = await pdfDoc.copyPages(docWithAnnots, [0]);
page4.scale(0.5, 0.5);
pdfDoc.addPage(page4);
const page5 = pdfDoc.addPage([305, 250]);
const hotPink = rgb(1, 0, 1);
const red = rgb(1, 0, 0);
page0.drawText('This is the new first page!', {
x: 5,
y: 200,
font: helveticaFont,
color: hotPink,
});
page0.drawLine({
start: { x: 10, y: 207 },
end: { x: 295, y: 207 },
color: rgb(0, 1, 1),
thickness: 5,
opacity: 0.7,
});
page0.drawImage(catRidingUnicornImage, {
...catRidingUnicornDims,
x: 30,
y: 30,
});
const lastPageText = 'This is the last page!';
const lastPageTextWidth = helveticaFont.widthOfTextAtSize(lastPageText, 24);
const page1Text = 'pdf-lib is awesome!';
const page1TextWidth = helveticaFont.widthOfTextAtSize(page1Text, 70);
page1.setFontSize(70);
page1.drawText(page1Text, {
x: page1.getWidth() / 2 - page1TextWidth / 2 + 45,
y: page1.getHeight() / 2 + 45,
color: red,
rotate: degrees(-30),
xSkew: degrees(15),
ySkew: degrees(15),
});
page5.setFontSize(24);
page5.drawText('This is the last page!', {
x: 30,
y: 215,
font: helveticaFont,
color: hotPink,
});
page5.drawLine({
start: { x: 30, y: 205 },
end: { x: 30 + lastPageTextWidth, y: 205 },
color: hotPink,
thickness: 5,
});
page5.drawImage(cmykImage, {
...cmykDims,
x: 30,
y: 30,
});
page5.drawLine({
start: { x: 30, y: 240 },
end: { x: 30 + lastPageTextWidth, y: 240 },
color: hotPink,
thickness: 5,
lineCap: LineCapStyle.Round,
});
console.log('Title:', pdfDoc.getTitle());
console.log('Author:', pdfDoc.getAuthor());
console.log('Subject:', pdfDoc.getSubject());
console.log('Creator:', pdfDoc.getCreator());
console.log('Keywords:', pdfDoc.getKeywords());
console.log('Producer:', pdfDoc.getProducer());
console.log('Creation Date:', pdfDoc.getCreationDate());
console.log('Modification Date:', pdfDoc.getModificationDate());
const base64Pdf = await pdfDoc.saveAsBase64({ dataUri: true });
return { base64Pdf };
// const pdfBytes = await pdfDoc.save();
// const path = await writePdf(pdfBytes);
// return { base64Pdf: `file://${path}` };
};