Newer
Older
// import { PDFName, PDFPage, PDFString, PDFDocument, PDFImage } from "pdf-lib";
import { nopodofo as npdf } from "nopodofo";
import { PdfData } from "pdfdataextract";
import { config } from "./config";
import { IGetMetaResponse } from "./types";
import { verifyPDF } from "./lib";
import { formatPdfTime } from "./lib/timeUtils";
import { AppError, GeneralError } from "./lib/errors";
import { isPDF } from "./lib/generalUtils";
class PDFparser {
readonly document;
readonly config;
constructor(document: Buffer) {
this.document = document;
this.config = config;
}
getPDFMeta = async (): Promise<IGetMetaResponse> => {
if (!(this.document instanceof Buffer)) {
throw new AppError("Document is not Buffer");
}
if (!isPDF(this.document)) {
throw new AppError("Only pdf file type is supported");
}
try {
const signaturesMeta = await verifyPDF(this.document);
const pdfMeta = await PdfData.extract(this.document, config);
title: pdfMeta.info.Title || "Unknown",
author: pdfMeta.info.Author || "Unknown",
creation_date: formatPdfTime(pdfMeta.info.CreationDate),
mod_date: formatPdfTime(pdfMeta.info.ModDate),
result["expired"] = signaturesMeta.expired;
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const test = await pdf(this.document);
// const pdfDoc = await doc.load(
// this.document,
// { forUpdate: true },
// (e, data: any) => {
// const test = data.getPageCount();
// console.log({ test });
// const firest = data.getPage(1);
// console.log(firest);
// }
// );
// // const pdfDoc = await PDFDocument.load(this.document);
// let img: PDFImage;
// try {
// img = await pdfDoc.embedPng(imgBytes);
// } catch (error) {
// console.log(error);
// }
// const scaled = img.scale(scaleFactor);
// const pages = pdfDoc.getPageCount();
// for (let index = 0; index < pages.length; index++) {
// const page = pages[index];
// const x =
// typeof coords[index + 1] !== "undefined"
// ? parseFloat(coords[index + 1].x)
// : null;
// const y =
// typeof coords[index + 1] !== "undefined"
// ? parseFloat(coords[index + 1].y)
// : null;
// if (x && y) {
// page.drawImage(img, {
// x,
// y,
// width: scaled.width,
// height: scaled.height,
// });
// const link = this.createPageLinkAnnotation(page, url, {
// imgXPos: x,
// imgYPos: y,
// imgWidth: scaled.width,
// imagHeight: scaled.height,
// });
// page.node.set(PDFName.of("Annots"), pdfDoc.context.obj([link]));
// }
// }
// const pdfBytes = await pdfDoc.save();
return new ArrayBuffer(1);
// 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),
},
})
);