Added support for PDFJS to PdfFile. Migrated some operations to use this new wrapper
This commit is contained in:
@@ -1,27 +1,28 @@
|
||||
|
||||
import { PDFDocument } from 'pdf-lib';
|
||||
|
||||
import { createSubDocument } from "./common/createSubDocument.js";
|
||||
import { selectPages } from "./subDocumentFunctions";
|
||||
import { PdfFile } from '../wrappers/PdfFile';
|
||||
|
||||
export async function splitPDF(snapshot: string | Uint8Array | ArrayBuffer, splitAfterPageArray: number[]): Promise<Uint8Array[]> {
|
||||
export async function splitPDF(file: PdfFile, splitAfterPageArray: number[]): Promise<PdfFile[]> {
|
||||
const byteFile = await file.convertToPdfLibFile();
|
||||
if (!byteFile?.pdfLib) return [];
|
||||
|
||||
const pdfDoc = await PDFDocument.load(snapshot)
|
||||
|
||||
const numberOfPages = pdfDoc.getPages().length;
|
||||
const numberOfPages = byteFile.pdfLib.getPages().length;
|
||||
|
||||
let pagesArray: number[] = [];
|
||||
let splitAfter = splitAfterPageArray.shift();
|
||||
const subDocuments: Uint8Array[] = [];
|
||||
const subDocuments: PdfFile[] = [];
|
||||
|
||||
for (let i = 0; i < numberOfPages; i++) {
|
||||
if(splitAfter && i > splitAfter && pagesArray.length > 0) {
|
||||
subDocuments.push(await createSubDocument(pdfDoc, pagesArray));
|
||||
subDocuments.push(await selectPages(byteFile, pagesArray));
|
||||
splitAfter = splitAfterPageArray.shift();
|
||||
pagesArray = [];
|
||||
}
|
||||
pagesArray.push(i);
|
||||
}
|
||||
subDocuments.push(await createSubDocument(pdfDoc, pagesArray));
|
||||
subDocuments.push(await selectPages(byteFile, pagesArray));
|
||||
pagesArray = [];
|
||||
|
||||
return subDocuments;
|
||||
|
||||
Reference in New Issue
Block a user