Working (only tested Split yet but im tired)

This commit is contained in:
Felix Kaspar
2023-11-14 23:14:08 +01:00
parent d7feec32dd
commit 85d61fddf0
12 changed files with 164 additions and 136 deletions

View File

@@ -1,6 +1,6 @@
import { PDFDocument } from 'pdf-lib';
import { PdfFile, fromPdfLib } from '../wrappers/PdfFile.js';
import { PdfFile, RepresentationType } from '../wrappers/PdfFile.js';
import { detectEmptyPages } from "./common/detectEmptyPages.js";
@@ -21,12 +21,11 @@ export async function sortPagesWithPreset(params: SortPagesWithPresetParamsType)
throw new Error("Operation not supported");
}
const byteFile = await file.convertToPdfLibFile();
if (!byteFile?.pdfLib) return byteFile;
const pdflibDocument = await file.pdflibDocument;
const pageCount = byteFile.pdfLib.getPageCount();
const pageCount = pdflibDocument.getPageCount();
const sortIndecies = sortFunction(pageCount);
return selectPages({file:byteFile, pagesToExtractArray:sortIndecies});
return selectPages({file: file, pagesToExtractArray: sortIndecies});
}
export type RearrangePagesParamsType = {
@@ -37,11 +36,10 @@ export type RearrangePagesParamsType = {
export async function rearrangePages(params: RearrangePagesParamsType): Promise<PdfFile> {
const { file, fancyPageSelector } = params;
const byteFile = await file.convertToPdfLibFile();
if (!byteFile?.pdfLib) return byteFile;
const pdflibDocument = await file.pdflibDocument;
const pagesToExtractArray = parseFancyPageSelector(fancyPageSelector, byteFile.pdfLib.getPageCount());
const newDocument = selectPages({file:byteFile, pagesToExtractArray});
const pagesToExtractArray = parseFancyPageSelector(fancyPageSelector, pdflibDocument.getPageCount());
const newDocument = selectPages({file: file, pagesToExtractArray});
return newDocument;
};
@@ -52,23 +50,22 @@ export type SelectPagesParamsType = {
export async function selectPages(params: SelectPagesParamsType): Promise<PdfFile> {
const { file, pagesToExtractArray } = params;
const byteFile = await file.convertToPdfLibFile();
if (!byteFile?.pdfLib) return byteFile;
const pdflibDocument = await file.pdflibDocument;
const subDocument = await PDFDocument.create();
// Check that array max number is not larger pdf pages number
if(Math.max(...pagesToExtractArray) >= byteFile.pdfLib.getPageCount()) {
throw new Error(`The PDF document only has ${byteFile.pdfLib.getPageCount()} pages and you tried to extract page ${Math.max(...pagesToExtractArray)}`);
if(Math.max(...pagesToExtractArray) >= pdflibDocument.getPageCount()) {
throw new Error(`The PDF document only has ${pdflibDocument.getPageCount()} pages and you tried to extract page ${Math.max(...pagesToExtractArray)}`);
}
const copiedPages = await subDocument.copyPages(byteFile.pdfLib, pagesToExtractArray);
const copiedPages = await subDocument.copyPages(pdflibDocument, pagesToExtractArray);
for (let i = 0; i < copiedPages.length; i++) {
subDocument.addPage(copiedPages[i]);
}
return fromPdfLib(subDocument, file.filename);
return new PdfFile(file.originalFilename, subDocument, RepresentationType.PDFLibDocument, file.filename);
}
export type RemovePagesParamsType = {
@@ -78,11 +75,10 @@ export type RemovePagesParamsType = {
export async function removePages(params: RemovePagesParamsType): Promise<PdfFile> {
const { file, pagesToRemoveArray } = params;
const byteFile = await file.convertToPdfLibFile();
if (!byteFile?.pdfLib) return byteFile;
const pdflibDocument = await file.pdflibDocument;
const pagesToExtractArray = invertSelection(pagesToRemoveArray, byteFile.pdfLib.getPageIndices())
return selectPages({file:byteFile, pagesToExtractArray});
const pagesToExtractArray = invertSelection(pagesToRemoveArray, pdflibDocument.getPageIndices())
return selectPages({file: file, pagesToExtractArray});
}
export type RemoveBlankPagesParamsType = {