forked from Hopding/pdf-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest7.js
74 lines (57 loc) · 2.13 KB
/
test7.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
import { PDFDocument, StandardFonts, degrees } from 'pdf-lib';
import { fetchAsset, writePdf } from './assets';
const createDonorPdf = async () => {
const pdfDoc = await PDFDocument.create();
const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica);
const page = pdfDoc.addPage([500, 500]);
page.moveTo(50, 225);
page.setFont(helveticaFont);
page.setFontSize(50);
page.drawText('I am upside down!');
page.setRotation(degrees(180));
return pdfDoc;
};
export default async () => {
const [
withMissingEndstreamEolAndPollutedCtmBytes,
normalBytes,
withUpdateSectionsBytes,
linearizedWithObjectStreamsBytes,
withLargePageCountBytes,
] = await Promise.all([
fetchAsset('pdfs/with_missing_endstream_eol_and_polluted_ctm.pdf'),
fetchAsset('pdfs/normal.pdf'),
fetchAsset('pdfs/with_update_sections.pdf'),
fetchAsset('pdfs/linearized_with_object_streams.pdf'),
fetchAsset('pdfs/with_large_page_count.pdf'),
]);
const pdfDoc = await PDFDocument.load(
withMissingEndstreamEolAndPollutedCtmBytes,
);
const allDonorPdfBytes = [
normalBytes,
withUpdateSectionsBytes,
linearizedWithObjectStreamsBytes,
withLargePageCountBytes,
];
for (let idx = 0, len = allDonorPdfBytes.length; idx < len; idx++) {
const donorBytes = allDonorPdfBytes[idx];
const donorPdf = await PDFDocument.load(donorBytes);
const [donorPage] = await pdfDoc.copyPages(donorPdf, [0]);
pdfDoc.addPage(donorPage);
}
const anotherDonorPdf = await createDonorPdf();
const [anotherDonorPage] = await pdfDoc.copyPages(anotherDonorPdf, [0]);
pdfDoc.insertPage(1, anotherDonorPage);
const savedBytes = await pdfDoc.save();
const sizeOfCreatedPdf = savedBytes.length;
let sizeOfAllDonorPdfs = (await anotherDonorPdf.save()).length;
for (let idx = 0, len = allDonorPdfBytes.length; idx < len; idx++) {
sizeOfAllDonorPdfs += allDonorPdfBytes[idx].length;
}
const base64Pdf = await pdfDoc.saveAsBase64({ dataUri: true });
return { base64Pdf };
// const pdfBytes = await pdfDoc.save();
// const path = await writePdf(pdfBytes);
// return { base64Pdf: `file://${path}` };
};