forked from Hopding/pdf-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test8.ts
45 lines (36 loc) · 1.15 KB
/
test8.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
import { Assets } from '../index.ts';
// @deno-types="../dummy.d.ts"
import {
ParseSpeeds,
PDFDocument,
PDFPage,
rgb,
StandardFonts,
} from '../../../dist/pdf-lib.esm.js';
export default async (assets: Assets) => {
const { pdfs } = assets;
const pdfDoc = await PDFDocument.load(
pdfs.with_newline_whitespace_in_indirect_object_numbers,
{ parseSpeed: ParseSpeeds.Fastest },
);
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: PDFPage, idx: number) => {
page.moveTo(10, 10);
page.setFont(helveticaFont);
page.setFontSize(17);
page.setFontColor(rgb(1, 0, 0));
page.drawText(`${idx + 1} / ${pages.length}`);
});
const pdfBytes = await pdfDoc.save();
return pdfBytes;
};