Newer
Older
import { PDFName, PDFPage, PDFString } from "pdf-lib";
import { PdfData } from "pdfdataextract";
import { config } from "./config";
import { IgetMetaResponse } from "./types";
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
class PDFparser {
readonly document;
readonly config;
constructor(document: Buffer) {
this.document = document;
this.config = config;
}
getPDFMeta = async (): Promise<IgetMetaResponse> => {
try {
const pdfMeta = await PdfData.extract(this.document, config);
const signaturesMeta = await verifyPDF(this.document);
return {
verified: signaturesMeta.verified,
authenticity: signaturesMeta.authenticity,
integrity: signaturesMeta.integrity,
expired: signaturesMeta.expired,
meta: {
certs: signaturesMeta.certs,
},
pages: pdfMeta.pages,
fingerpring: pdfMeta.fingerprint,
creation_data: pdfMeta.info.CreationDate,
creator: pdfMeta.info.Creator,
author: pdfMeta.info.Author,
title: pdfMeta.info.Title,
description: pdfMeta.info.Keywords,
mod_date: pdfMeta.info.ModDate,
};
} catch (error) {
console.error(error);
throw new Error("Could not get pdf metadata");
}
};
insertQrCode = async (imgBytes: ArrayBuffer): Promise<ArrayBuffer> => {
const pdfDoc = await PDFDocument.load(this.document);
const img = await pdfDoc.embedPng(imgBytes);
x: firstPage.getWidth() / 2 - scaled.width / 2,
y: firstPage.getHeight() / 2 - scaled.height / 2,
width: scaled.width,
height: scaled.height,
const link = this.createPageLinkAnnotation(
firstPage,
"https://pdf-lib.js.org/",
{
imgXPos: firstPage.getWidth() / 2 - scaled.width / 2,
imgYPos: firstPage.getHeight() / 2 - scaled.height / 2,
imgWidth: scaled.width,
imagHeight: scaled.height,
}
);
firstPage.node.set(PDFName.of("Annots"), pdfDoc.context.obj([link]));
const pdfBytes = await pdfDoc.save();
return pdfBytes;
};
private createPageLinkAnnotation = (
page: PDFPage,
uri: string,
{ imgXPos, imgYPos, imgWidth, imagHeight }
) =>
page.doc.context.register(
page.doc.context.obj({
Type: "Annot",
Subtype: "Link",
Rect: [imgXPos, imgYPos, imgXPos + imgWidth, imgYPos + imagHeight],
A: {
Type: "Action",
S: "URI",
URI: PDFString.of(uri),
},
})
);