arrangePages

v2-wip
Felix Kaspar 2 weeks ago
parent dbadfe50e6
commit fae524f8da
  1. 82
      shared-operations/src/functions/arrangePages.ts
  2. 2
      shared-operations/src/functions/common/detectQRCodePages.ts

@ -1,26 +1,64 @@
import { Operator, Progress, oneToOne } from ".";
import Joi from "@stirling-tools/joi";
import { JoiPDFFileSchema } from "../wrappers/PdfFileJoi";
import i18next from "i18next";
import { PdfFile } from "../wrappers/PdfFile";
import { Sorts } from "./common/pageIndexesSorting";
import { getPages } from "./common/getPagesByIndex";
import { parsePageIndexSpecification } from "./common/pageIndexesUtils";
export interface ArrangePagesParamsType {
file: PdfFile;
arrangementConfig: string; // a member of Sorts, or a page index specification
}
export async function arrangePages(params: ArrangePagesParamsType) {
const { file, arrangementConfig } = params;
const pdfLibDocument = await file.pdfLibDocument;
const pageCount = pdfLibDocument.getPageCount();
let sortIndexes: number[];
if (arrangementConfig in Sorts) {
const sortFunction = Sorts[arrangementConfig];
sortIndexes = sortFunction(pageCount);
} else {
sortIndexes = parsePageIndexSpecification(arrangementConfig, pageCount);
export class ArrangePages extends Operator {
static type = "arrangePages";
/**
* Validation & Localisation
*/
protected static inputSchema = JoiPDFFileSchema.label(i18next.t("inputs.pdffile.name")).description(i18next.t("inputs.pdffile.description"));
protected static valueSchema = Joi.object({
arrangementConfig: Joi.string().valid(...[
"REVERSE_ORDER",
"DUPLEX_SORT",
"BOOKLET_SORT",
"SIDE_STITCH_BOOKLET_SORT",
"ODD_EVEN_SPLIT",
"REMOVE_FIRST",
"REMOVE_LAST",
"REMOVE_FIRST_AND_LAST"
]).required()
.label(i18next.t("values.arrangementConfig.friendlyName", { ns: "arrangePages" })).description(i18next.t("values.arrangementConfig.description", { ns: "arrangePages" }))
.example("REVERSE_ORDER").example("DUPLEX_SORT").example("BOOKLET_SORT").required()
});
protected static outputSchema = JoiPDFFileSchema.label(i18next.t("outputs.pdffile.name")).description(i18next.t("outputs.pdffile.description"));
static schema = Joi.object({
input: ArrangePages.inputSchema,
values: ArrangePages.valueSchema.required(),
output: ArrangePages.outputSchema
}).label(i18next.t("friendlyName", { ns: "arrangePages" })).description(i18next.t("description", { ns: "arrangePages" }));
/**
* Logic
*/
/** Detect and remove white pages */
async run(input: PdfFile[], progressCallback: (state: Progress) => void): Promise<PdfFile[]> {
return oneToOne<PdfFile, PdfFile>(input, async (input, index, max) => {
const pdfLibDocument = await input.pdfLibDocument;
const pageCount = pdfLibDocument.getPageCount();
const sortFunction = Sorts[this.actionValues.arrangementConfig];
let sortIndexes = sortFunction(pageCount);
const newFile = await getPages(input, sortIndexes);
newFile.filename += "arrangedPages";
progressCallback({ curFileProgress: 1, operationProgress: index/max });
return newFile;
});
}
const newFile = await getPages(file, sortIndexes);
newFile.filename += "arrangedPages";
return newFile;
}
}

@ -16,7 +16,7 @@ export async function detectQRCodePages(file: PdfFile) {
// console.log("images:", images);
for (const image of images) {
const data = await checkForQROnImage(image);
if(data == "https://github.com/Frooodle/Stirling-PDF") {
if(["https://github.com/Stirling-Tools/Stirling-PDF", "https://github.com/Frooodle/Stirling-PDF"].includes(data)) {
pagesWithQR.push(i);
}
}

Loading…
Cancel
Save