forked from Hopding/pdf-lib
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest2.js
124 lines (107 loc) · 4.08 KB
/
test2.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
import fontkit from '@pdf-lib/fontkit';
import { PDFDocument, rgb } from 'pdf-lib';
import { fetchAsset, writePdf } from './assets';
// This test loads an existing PDF document with many pages.
// It inserts data for every page (images, rectangles, texts, embedded PDFs).
// Also, the second page is removed.
export default async () => {
const [ubuntuBytes, smallMarioBytes, inputPdfBytes, largePageCountPdfBytes] =
await Promise.all([
fetchAsset('fonts/ubuntu/Ubuntu-R.ttf'),
fetchAsset('images/small_mario_resized.png'),
fetchAsset('pdfs/linearized_with_object_streams.pdf'),
fetchAsset('pdfs/with_large_page_count.pdf'),
]);
const pdfDoc = await PDFDocument.load(inputPdfBytes, {
updateMetadata: false,
});
pdfDoc.registerFontkit(fontkit);
const ubuntuFont = await pdfDoc.embedFont(ubuntuBytes, { subset: true });
const smallMarioImage = await pdfDoc.embedPng(smallMarioBytes);
const smallMarioDims = smallMarioImage.scale(0.75);
const sourcePdfDoc = await PDFDocument.load(largePageCountPdfBytes);
const sourcePdfPage = sourcePdfDoc.getPage(73);
const embeddedPageFigure = {
xOffset: 100,
yOffset: 330,
width: 350,
height: 240,
padding: 10,
};
const embeddedPage = await pdfDoc.embedPage(sourcePdfPage, {
// clip the PDF page to a certain area within the page
left: embeddedPageFigure.xOffset,
right: embeddedPageFigure.xOffset + embeddedPageFigure.width,
bottom: embeddedPageFigure.yOffset,
top: embeddedPageFigure.yOffset + embeddedPageFigure.height,
});
const embeddedPageDims = embeddedPage.scale(0.5);
const lines = [
'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 textWidth = ubuntuFont.widthOfTextAtSize(lines[2], fontSize);
pdfDoc.getPages().forEach((page) => {
const { width, height } = page.getSize();
const centerX = width / 2;
const centerY = height / 2;
page.drawImage(smallMarioImage, {
...smallMarioDims,
x: centerX - smallMarioDims.width / 2,
y: centerY + 15,
});
const boxHeight = (fontSize + 5) * lines.length;
page.drawRectangle({
x: centerX - textWidth / 2 - 5,
y: centerY - 15 - boxHeight + fontSize + 3,
width: textWidth + 10,
height: boxHeight,
color: solarizedWhite,
opacity: 0.85,
borderColor: solarizedGray,
borderWidth: 3,
});
page.setFont(ubuntuFont);
page.setFontColor(solarizedGray);
page.drawText(lines.join('\n'), {
x: centerX - textWidth / 2,
y: centerY - 15,
});
page.drawRectangle({
x: 10,
y: 10,
width: embeddedPageFigure.width / 2 + embeddedPageFigure.padding * 2,
height: embeddedPageFigure.height / 2 + embeddedPageFigure.padding * 2,
color: solarizedWhite,
opacity: 0.6,
borderColor: solarizedGray,
borderWidth: 2,
});
page.drawPage(embeddedPage, {
x: embeddedPageFigure.padding + 10,
y: embeddedPageFigure.padding + 10,
...embeddedPageDims,
});
});
pdfDoc.removePage(1);
// These will all be undefined since the source document's metadata is
// stored in a metadata stream, not the more widely used info dictionary.
// pdf-lib does not currently support reading metadata streams.
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 pdfBytes = await pdfDoc.save({ objectsPerTick: Infinity });
// const path = await writePdf(pdfBytes);
// return { base64Pdf: `file://${path}` };
const base64Pdf = await pdfDoc.saveAsBase64({ dataUri: true });
return { base64Pdf };
};