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-14 03:26:42 +03: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-14 20:22:37 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
resolve(this.representation as Uint8Array);
|
|
|
|
});
|
2023-11-14 23:14:08 +01:00
|
|
|
case RepresentationType.PDFLibDocument:
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
var uint8Array = await (this.representation as PDFLibDocument).save();
|
|
|
|
this.uint8Array = uint8Array;
|
|
|
|
resolve(uint8Array);
|
|
|
|
});
|
|
|
|
case RepresentationType.PDFJSDocument:
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
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-14 20:22:37 +01:00
|
|
|
get pdflibDocument() : Promise<PDFLibDocument> {
|
2023-11-14 23:14:08 +01:00
|
|
|
switch (this.representationType) {
|
|
|
|
case RepresentationType.PDFLibDocument:
|
2023-11-14 20:22:37 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
resolve(this.representation as PDFLibDocument);
|
|
|
|
});
|
|
|
|
default:
|
|
|
|
return new Promise(async (resolve, reject) => {
|
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
|
|
|
});
|
|
|
|
this.pdflibDocument = pdfLibDoc;
|
|
|
|
resolve(pdfLibDoc);
|
2023-11-14 20:22:37 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set pdflibDocument(value: PDFLibDocument) {
|
|
|
|
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-14 23:14:08 +01:00
|
|
|
get pdfjsDocument() : Promise<PDFJSDocument> {
|
|
|
|
switch (this.representationType) {
|
|
|
|
case RepresentationType.PDFJSDocument:
|
2023-11-14 20:22:37 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
resolve(this.representation as PDFJSDocument);
|
|
|
|
});
|
|
|
|
default:
|
|
|
|
return new Promise(async (resolve, reject) => {
|
2023-11-14 23:14:08 +01:00
|
|
|
const pdfjsDoc = await PDFJS.getDocument(await this.uint8Array).promise;
|
|
|
|
this.pdfjsDocument = pdfjsDoc;
|
|
|
|
resolve(pdfjsDoc);
|
2023-11-14 20:22:37 +01:00
|
|
|
});
|
|
|
|
}
|
2023-11-12 04:46:09 +03:00
|
|
|
}
|
2023-11-14 23:14:08 +01: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-14 20:22:37 +01:00
|
|
|
this.originalFilename = originalFilename;
|
|
|
|
this.filename = filename ? filename : originalFilename;
|
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-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-14 20:22:37 +01:00
|
|
|
|
2023-11-14 03:26:42 +03:00
|
|
|
export const PdfFileSchema = Joi.any().custom((value, helpers) => {
|
|
|
|
if (!(value instanceof PdfFile)) {
|
|
|
|
throw new Error('value is not a PdfFile');
|
|
|
|
}
|
|
|
|
return value;
|
2023-11-14 20:22:37 +01:00
|
|
|
}, "PdfFile validation");
|