109 lines
4.2 KiB
TypeScript
Raw Normal View History

import * as PDFJS from 'pdfjs-dist';
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';
import Joi from 'joi';
export enum RepresentationType {
Uint8Array,
PDFLibDocument,
PDFJSDocument
}
export class PdfFile {
2023-11-14 20:22:37 +01:00
private representation: Uint8Array | PDFLibDocument | PDFJSDocument;
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-14 20:22:37 +01:00
get uint8Array() : Promise<Uint8Array> {
switch (this.representationType) {
case RepresentationType.Uint8Array:
2023-11-14 20:22:37 +01:00
return new Promise((resolve, reject) => {
resolve(this.representation as Uint8Array);
});
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:
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;
this.representationType = RepresentationType.Uint8Array;
}
2023-11-14 20:22:37 +01:00
get pdflibDocument() : Promise<PDFLibDocument> {
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) => {
var uint8Array = await this.uint8Array;
var pdfLibDoc = await PDFLibDocument.load(uint8Array, {
2023-11-14 20:22:37 +01:00
updateMetadata: false,
});
this.pdflibDocument = pdfLibDoc;
resolve(pdfLibDoc);
2023-11-14 20:22:37 +01:00
});
}
}
set pdflibDocument(value: PDFLibDocument) {
this.representation = value;
this.representationType = RepresentationType.PDFLibDocument;
}
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) => {
const pdfjsDoc = await PDFJS.getDocument(await this.uint8Array).promise;
this.pdfjsDocument = pdfjsDoc;
resolve(pdfjsDoc);
2023-11-14 20:22:37 +01:00
});
}
}
set pdfjsDocument(value: PDFJSDocument) {
2023-11-14 20:22:37 +01:00
this.representation = value;
this.representationType = RepresentationType.PDFJSDocument;
2023-11-14 20:22:37 +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-14 20:22:37 +01:00
this.representation = representation;
this.representationType = representationType;
}
2023-11-14 20:22:37 +01:00
static fromMulterFile(value: Express.Multer.File): PdfFile {
return new PdfFile(value.originalname, value.buffer as Uint8Array, RepresentationType.Uint8Array);
}
2023-11-14 20:22:37 +01:00
static fromMulterFiles(values: Express.Multer.File[]): PdfFile[] {
return values.map(v => PdfFile.fromMulterFile(v));
}
}
2023-11-14 20:22:37 +01: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");