import verifyPDF from "@ninja-labs/verify-pdf"; import { PdfData } from "pdfdataextract"; import { config } from "./config"; import { IgetMetaResponse } from "./types"; const { PDFDocument } = require("pdf-lib"); 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 imagePage = pdfDoc.insertPage(0); 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; }; } export default PDFparser;