2023-11-12 04:46:09 +03:00
|
|
|
import * as PDFJS from 'pdfjs-dist';
|
2023-11-14 23:14:08 +01:00
|
|
|
import type { PDFDocumentProxy as PDFJSDocument } from 'pdfjs-dist/types/src/display/api';
|
2023-11-14 20:22:37 +01:00
|
|
|
import { PDFDocument as PDFLibDocument } from 'pdf-lib';
|
2023-11-21 14:57:58 +01:00
|
|
|
import Joi from 'joi';
|
2023-11-11 23:43:39 +03:00
|
|
|
|
2023-11-14 23:14:08 +01:00
|
|
|
export enum RepresentationType {
|
|
|
|
Uint8Array,
|
|
|
|
PDFLibDocument,
|
|
|
|
PDFJSDocument
|
|
|
|
}
|
|
|
|
|
2023-11-11 23:43:39 +03:00
|
|
|
export class PdfFile {
|
2023-11-14 20:22:37 +01:00
|
|
|
private representation: Uint8Array | PDFLibDocument | PDFJSDocument;
|
2023-11-14 23:14:08 +01:00
|
|
|
private representationType: RepresentationType;
|
2023-11-14 20:22:37 +01:00
|
|
|
originalFilename: string;
|
2023-11-12 16:57:53 +03:00
|
|
|
filename: string;
|
2023-11-11 23:43:39 +03:00
|
|
|
|
2023-11-14 20:22:37 +01:00
|
|
|
get uint8Array() : Promise<Uint8Array> {
|
2023-11-14 23:14:08 +01:00
|
|
|
switch (this.representationType) {
|
|
|
|
case RepresentationType.Uint8Array:
|
2023-11-15 15:33:04 +03:00
|
|
|
return new Promise((resolve) => {
|
2023-11-14 20:22:37 +01:00
|
|
|
resolve(this.representation as Uint8Array);
|
|
|
|
});
|
2023-11-14 23:14:08 +01:00
|
|
|
case RepresentationType.PDFLibDocument:
|
2023-11-15 15:33:04 +03:00
|
|
|
return new Promise(async (resolve) => {
|
2023-11-14 23:14:08 +01:00
|
|
|
var uint8Array = await (this.representation as PDFLibDocument).save();
|
|
|
|
this.uint8Array = uint8Array;
|
|
|
|
resolve(uint8Array);
|
|
|
|
});
|
|
|
|
case RepresentationType.PDFJSDocument:
|
2023-11-15 15:33:04 +03:00
|
|
|
return new Promise(async (resolve) => {
|
2023-11-14 23:14:08 +01:00
|
|
|
var uint8Array = await (this.representation as PDFJSDocument).getData();
|
|
|
|
this.uint8Array = uint8Array;
|
|
|
|
resolve(uint8Array);
|
|
|
|
});
|
2023-11-14 20:22:37 +01:00
|
|
|
default:
|
2023-11-14 23:14:08 +01:00
|
|
|
console.error("unhandeled PDF type: " + typeof this.representation as string);
|
2023-11-14 20:22:37 +01:00
|
|
|
throw Error("unhandeled PDF type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set uint8Array(value: Uint8Array) {
|
|
|
|
this.representation = value;
|
2023-11-14 23:14:08 +01:00
|
|
|
this.representationType = RepresentationType.Uint8Array;
|
2023-11-11 23:43:39 +03:00
|
|
|
}
|
2023-11-12 04:46:09 +03:00
|
|
|
|
2023-11-15 02:38:07 +03:00
|
|
|
get pdfLibDocument() : Promise<PDFLibDocument> {
|
2023-11-14 23:14:08 +01:00
|
|
|
switch (this.representationType) {
|
|
|
|
case RepresentationType.PDFLibDocument:
|
2023-11-15 15:33:04 +03:00
|
|
|
return new Promise((resolve) => {
|
2023-11-14 20:22:37 +01:00
|
|
|
resolve(this.representation as PDFLibDocument);
|
|
|
|
});
|
|
|
|
default:
|
2023-11-15 15:33:04 +03:00
|
|
|
return new Promise(async (resolve) => {
|
2023-11-14 23:14:08 +01:00
|
|
|
var uint8Array = await this.uint8Array;
|
|
|
|
var pdfLibDoc = await PDFLibDocument.load(uint8Array, {
|
2023-11-14 20:22:37 +01:00
|
|
|
updateMetadata: false,
|
2023-11-14 23:14:08 +01:00
|
|
|
});
|
2023-11-15 02:38:07 +03:00
|
|
|
this.pdfLibDocument = pdfLibDoc;
|
2023-11-14 23:14:08 +01:00
|
|
|
resolve(pdfLibDoc);
|
2023-11-14 20:22:37 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2023-11-15 02:38:07 +03:00
|
|
|
set pdfLibDocument(value: PDFLibDocument) {
|
2023-11-14 20:22:37 +01:00
|
|
|
this.representation = value;
|
2023-11-14 23:14:08 +01:00
|
|
|
this.representationType = RepresentationType.PDFLibDocument;
|
2023-11-12 04:46:09 +03:00
|
|
|
}
|
|
|
|
|
2023-11-15 02:38:07 +03:00
|
|
|
get pdfJsDocument() : Promise<PDFJSDocument> {
|
2023-11-14 23:14:08 +01:00
|
|
|
switch (this.representationType) {
|
|
|
|
case RepresentationType.PDFJSDocument:
|
2023-11-15 15:33:04 +03:00
|
|
|
return new Promise((resolve) => {
|
2023-11-14 20:22:37 +01:00
|
|
|
resolve(this.representation as PDFJSDocument);
|
|
|
|
});
|
|
|
|
default:
|
2023-11-15 15:33:04 +03:00
|
|
|
return new Promise(async (resolve) => {
|
2023-11-14 23:14:08 +01:00
|
|
|
const pdfjsDoc = await PDFJS.getDocument(await this.uint8Array).promise;
|
2023-11-15 02:38:07 +03:00
|
|
|
this.pdfJsDocument = pdfjsDoc;
|
2023-11-14 23:14:08 +01:00
|
|
|
resolve(pdfjsDoc);
|
2023-11-14 20:22:37 +01:00
|
|
|
});
|
|
|
|
}
|
2023-11-12 04:46:09 +03:00
|
|
|
}
|
2023-11-15 02:38:07 +03:00
|
|
|
set pdfJsDocument(value: PDFJSDocument) {
|
2023-11-14 20:22:37 +01:00
|
|
|
this.representation = value;
|
2023-11-14 23:14:08 +01:00
|
|
|
this.representationType = RepresentationType.PDFJSDocument;
|
2023-11-14 20:22:37 +01:00
|
|
|
}
|
|
|
|
|
2023-11-14 23:14:08 +01:00
|
|
|
constructor(originalFilename: string, representation: Uint8Array | PDFLibDocument | PDFJSDocument, representationType: RepresentationType, filename?: string) {
|
2023-11-16 02:24:10 +03:00
|
|
|
if (originalFilename.toLowerCase().endsWith(".pdf"))
|
|
|
|
originalFilename = originalFilename.slice(0, -4);
|
2023-11-14 20:22:37 +01:00
|
|
|
this.originalFilename = originalFilename;
|
2023-11-16 02:24:10 +03:00
|
|
|
|
2023-11-14 20:22:37 +01:00
|
|
|
this.filename = filename ? filename : originalFilename;
|
2023-11-16 02:24:10 +03:00
|
|
|
if (this.filename.toLowerCase().endsWith(".pdf"))
|
2023-11-27 23:35:18 +01:00
|
|
|
this.filename = this.filename.slice(0, -4);
|
2023-11-12 04:46:09 +03:00
|
|
|
|
2023-11-14 20:22:37 +01:00
|
|
|
this.representation = representation;
|
2023-11-14 23:14:08 +01:00
|
|
|
this.representationType = representationType;
|
2023-11-12 04:46:09 +03:00
|
|
|
}
|
2023-11-14 20:22:37 +01:00
|
|
|
|
|
|
|
static fromMulterFile(value: Express.Multer.File): PdfFile {
|
2023-11-14 23:14:08 +01:00
|
|
|
return new PdfFile(value.originalname, value.buffer as Uint8Array, RepresentationType.Uint8Array);
|
2023-11-27 23:35:18 +01:00
|
|
|
|
2023-11-12 04:46:09 +03:00
|
|
|
}
|
2023-11-14 20:22:37 +01:00
|
|
|
static fromMulterFiles(values: Express.Multer.File[]): PdfFile[] {
|
|
|
|
return values.map(v => PdfFile.fromMulterFile(v));
|
2023-11-11 23:43:39 +03:00
|
|
|
}
|
2023-11-15 02:27:21 +03:00
|
|
|
|
|
|
|
static async cacheAsUint8Arrays(files: PdfFile[]): Promise<Map<PdfFile, Uint8Array>> {
|
|
|
|
const docCache = new Map<PdfFile, Uint8Array>();
|
|
|
|
await Promise.all(files.map(async (file) => {
|
|
|
|
const pdfLibDocument = await file.uint8Array;
|
|
|
|
docCache.set(file, pdfLibDocument);
|
|
|
|
}));
|
|
|
|
return docCache;
|
|
|
|
}
|
|
|
|
static async cacheAsPdfLibDocuments(files: PdfFile[]): Promise<Map<PdfFile, PDFLibDocument>> {
|
|
|
|
const docCache = new Map<PdfFile, PDFLibDocument>();
|
|
|
|
await Promise.all(files.map(async (file) => {
|
2023-11-15 02:38:07 +03:00
|
|
|
const pdfLibDocument = await file.pdfLibDocument;
|
2023-11-15 02:27:21 +03:00
|
|
|
docCache.set(file, pdfLibDocument);
|
|
|
|
}));
|
|
|
|
return docCache;
|
|
|
|
}
|
|
|
|
static async cacheAsPdfJsDocuments(files: PdfFile[]): Promise<Map<PdfFile, PDFJSDocument>> {
|
|
|
|
const docCache = new Map<PdfFile, PDFJSDocument>();
|
|
|
|
await Promise.all(files.map(async (file) => {
|
2023-11-15 02:38:07 +03:00
|
|
|
const pdfLibDocument = await file.pdfJsDocument;
|
2023-11-15 02:27:21 +03:00
|
|
|
docCache.set(file, pdfLibDocument);
|
|
|
|
}));
|
|
|
|
return docCache;
|
|
|
|
}
|
2023-11-27 23:35:18 +01:00
|
|
|
}
|