137 lines
5.3 KiB
TypeScript
Raw Normal View History

2024-01-04 20:17:54 -05:00
import * as PDFJS from "pdfjs-dist";
import pdfJSWorkerURL from "pdfjs-dist/build/pdf.worker.min.mjs?url";
const isBrowser = import.meta.env.SSR === false
if(isBrowser){
PDFJS.GlobalWorkerOptions.workerSrc = pdfJSWorkerURL;
}
2024-01-04 20:17:54 -05:00
import type { PDFDocumentProxy as PDFJSDocument } from "pdfjs-dist/types/src/display/api";
import { PDFDocument as PDFLibDocument } from "pdf-lib";
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) {
2024-01-04 20:17:54 -05:00
case RepresentationType.Uint8Array:
return new Promise((resolve) => {
resolve(this.representation as Uint8Array);
});
case RepresentationType.PDFLibDocument:
return new Promise(async (resolve) => {
const uint8Array = await (this.representation as PDFLibDocument).save();
this.uint8Array = uint8Array;
resolve(uint8Array);
});
case RepresentationType.PDFJSDocument:
return new Promise(async (resolve) => {
const uint8Array = await (this.representation as PDFJSDocument).getData();
this.uint8Array = uint8Array;
resolve(uint8Array);
});
default:
console.error("unhandeled PDF type: " + typeof this.representation );
throw Error("unhandeled PDF type");
2023-11-14 20:22:37 +01:00
}
}
set uint8Array(value: Uint8Array) {
this.representation = value;
this.representationType = RepresentationType.Uint8Array;
}
get pdfLibDocument() : Promise<PDFLibDocument> {
switch (this.representationType) {
2024-01-04 20:17:54 -05:00
case RepresentationType.PDFLibDocument:
return new Promise((resolve) => {
resolve(this.representation as PDFLibDocument);
});
default:
return new Promise(async (resolve) => {
const uint8Array = await this.uint8Array;
const pdfLibDoc = await PDFLibDocument.load(uint8Array, {
updateMetadata: false,
2023-11-14 20:22:37 +01:00
});
2024-01-04 20:17:54 -05:00
this.pdfLibDocument = pdfLibDoc;
resolve(pdfLibDoc);
});
2023-11-14 20:22:37 +01:00
}
}
set pdfLibDocument(value: PDFLibDocument) {
2023-11-14 20:22:37 +01:00
this.representation = value;
this.representationType = RepresentationType.PDFLibDocument;
}
get pdfJsDocument() : Promise<PDFJSDocument> {
switch (this.representationType) {
2024-01-04 20:17:54 -05:00
case RepresentationType.PDFJSDocument:
return new Promise((resolve) => {
resolve(this.representation as PDFJSDocument);
});
default:
return new Promise(async (resolve) => {
const pdfjsDoc = await PDFJS.getDocument({ data: await this.uint8Array, isOffscreenCanvasSupported: false }).promise;
2024-01-04 20:17:54 -05:00
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) {
if (originalFilename.toLowerCase().endsWith(".pdf"))
originalFilename = originalFilename.slice(0, -4);
2023-11-14 20:22:37 +01:00
this.originalFilename = originalFilename;
2023-11-14 20:22:37 +01:00
this.filename = filename ? filename : originalFilename;
if (this.filename.toLowerCase().endsWith(".pdf"))
2023-11-27 23:35:18 +01:00
this.filename = this.filename.slice(0, -4);
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-27 23:35:18 +01: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-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) => {
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) => {
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
}