Working (only tested Split yet but im tired)
This commit is contained in:
@@ -6,7 +6,7 @@ import { Image } from 'image-js';
|
||||
import { getImagesOnPage } from "./getImagesOnPage.js";
|
||||
|
||||
export async function detectEmptyPages(file: PdfFile, whiteThreashold: number): Promise<number[]> {
|
||||
const pdfDoc = await file.pdfjsDocuemnt;
|
||||
const pdfDoc = await file.pdfjsDocument;
|
||||
|
||||
const emptyPages: number[] = [];
|
||||
for (let i = 1; i <= pdfDoc.numPages; i++) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
|
||||
import { PDFDocument } from 'pdf-lib';
|
||||
import { PdfFile } from '../wrappers/PdfFile';
|
||||
import { PdfFile, RepresentationType } from '../wrappers/PdfFile';
|
||||
|
||||
export type MergeParamsType = {
|
||||
files: PdfFile[];
|
||||
@@ -15,5 +15,5 @@ export async function mergePDFs(params: MergeParamsType): Promise<PdfFile> {
|
||||
copiedPages.forEach((page) => mergedPdf.addPage(page));
|
||||
}
|
||||
|
||||
return new PdfFile("mergedPDF", mergedPdf);
|
||||
return new PdfFile("mergedPDF", mergedPdf, RepresentationType.PDFLibDocument);
|
||||
};
|
||||
@@ -16,6 +16,8 @@ export async function splitOn(params: SplitOnParamsType) {
|
||||
const { file, type, whiteThreashold } = params;
|
||||
|
||||
let splitAtPages: number[] = [];
|
||||
|
||||
console.log("File: ", file);
|
||||
|
||||
switch (type) {
|
||||
case "BAR_CODE":
|
||||
@@ -36,6 +38,8 @@ export async function splitOn(params: SplitOnParamsType) {
|
||||
|
||||
console.log("Split At Pages: ", splitAtPages);
|
||||
|
||||
console.log("File: ", file);
|
||||
|
||||
// Remove detected Pages & Split
|
||||
const pdfDoc = await file.pdflibDocument;
|
||||
const numberOfPages = pdfDoc.getPageCount();
|
||||
@@ -66,7 +70,9 @@ export async function splitOn(params: SplitOnParamsType) {
|
||||
return subDocuments;
|
||||
|
||||
async function getPagesWithQRCode(file: PdfFile) {
|
||||
const pdfDoc = await file.pdfjsDocuemnt;
|
||||
console.log("FileInQRPrev: ", file);
|
||||
const pdfDoc = await file.pdfjsDocument;
|
||||
console.log("FileInQRAfter: ", file);
|
||||
|
||||
const pagesWithQR: number[] = [];
|
||||
for (let i = 0; i < pdfDoc.numPages; i++) {
|
||||
@@ -74,7 +80,7 @@ export async function splitOn(params: SplitOnParamsType) {
|
||||
const page = await pdfDoc.getPage(i + 1);
|
||||
|
||||
const images = await getImagesOnPage(page);
|
||||
console.log("images:", images);
|
||||
// console.log("images:", images);
|
||||
for (const image of images) {
|
||||
const data = await checkForQROnImage(image);
|
||||
if(data == "https://github.com/Frooodle/Stirling-PDF") {
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
|
||||
import { PdfFile, fromPdfLib } from '../wrappers/PdfFile';
|
||||
import { PdfFile } from '../wrappers/PdfFile';
|
||||
|
||||
export type UpdateMetadataParams = {
|
||||
file: PdfFile,
|
||||
@@ -17,7 +17,7 @@ export type UpdateMetadataParams = {
|
||||
}
|
||||
|
||||
export async function updateMetadata(params: UpdateMetadataParams): Promise<PdfFile> {
|
||||
const pdfDoc = await params.file.getAsPdfLib();
|
||||
const pdfDoc = await params.file.pdflibDocument;
|
||||
|
||||
if (params.deleteAll) {
|
||||
pdfDoc.setAuthor("");
|
||||
@@ -49,5 +49,5 @@ export async function updateMetadata(params: UpdateMetadataParams): Promise<PdfF
|
||||
|
||||
// TODO add trapped and custom metadata. May need another library
|
||||
|
||||
return fromPdfLib(pdfDoc, params.file.filename);
|
||||
return params.file;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user