Newer
Older
import verifyPDF from "@ninja-labs/verify-pdf";
import { PdfData } from "pdfdataextract";
import { config } from "./config";
import { IgetMetaResponse } from "./types";
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
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);
const pages = pdfDoc.getPages();
const firstPage = pages[0];
const { width, height } = firstPage.getSize();
firstPage.drawImage(img, {
x: firstPage.getWidth() / 2 - img.width / 2,
y: firstPage.getHeight() / 2 - img.height / 2,
width: img.width,
height: img.height,
// firstPage.drawImage(img, {
// x: 0,
// y: 0,
// width: firstPage.getWidth(),
// height: firstPage.getHeight(),
// });
const pdfBytes = await pdfDoc.save();
return pdfBytes;
};