forked from Hopding/pdf-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest8.js
44 lines (31 loc) · 1.22 KB
/
test8.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
import { PDFDocument, rgb, StandardFonts } from 'pdf-lib';
import { fetchAsset, writePdf } from './assets';
export default async () => {
const [inputPdfBytes] = await Promise.all([
fetchAsset('pdfs/with_newline_whitespace_in_indirect_object_numbers.pdf'),
]);
const pdfDoc = await PDFDocument.load(inputPdfBytes);
const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica);
const pages = pdfDoc.getPages();
const [firstPage] = pages;
const { width, height } = firstPage.getSize();
const text = 'pdf-lib is awesome!';
const textWidth = helveticaFont.widthOfTextAtSize(text, 75);
firstPage.moveTo(width / 2 - textWidth / 2, height - 100);
firstPage.setFont(helveticaFont);
firstPage.setFontSize(75);
firstPage.setFontColor(rgb(1, 0, 0));
firstPage.drawText(text);
pages.forEach((page, idx) => {
page.moveTo(10, 10);
page.setFont(helveticaFont);
page.setFontSize(17);
page.setFontColor(rgb(1, 0, 0));
page.drawText(`${idx + 1} / ${pages.length}`);
});
const base64Pdf = await pdfDoc.saveAsBase64({ dataUri: true });
return { base64Pdf };
// const pdfBytes = await pdfDoc.save();
// const path = await writePdf(pdfBytes);
// return { base64Pdf: `file://${path}` };
};