mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-06-22 23:45:02 +00:00
arrangePages
This commit is contained in:
parent
dbadfe50e6
commit
fae524f8da
@ -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 { PdfFile } from "../wrappers/PdfFile";
|
||||||
import { Sorts } from "./common/pageIndexesSorting";
|
import { Sorts } from "./common/pageIndexesSorting";
|
||||||
import { getPages } from "./common/getPagesByIndex";
|
import { getPages } from "./common/getPagesByIndex";
|
||||||
import { parsePageIndexSpecification } from "./common/pageIndexesUtils";
|
|
||||||
|
|
||||||
export interface ArrangePagesParamsType {
|
export class ArrangePages extends Operator {
|
||||||
file: PdfFile;
|
static type = "arrangePages";
|
||||||
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) {
|
* Validation & Localisation
|
||||||
const sortFunction = Sorts[arrangementConfig];
|
*/
|
||||||
sortIndexes = sortFunction(pageCount);
|
|
||||||
} else {
|
protected static inputSchema = JoiPDFFileSchema.label(i18next.t("inputs.pdffile.name")).description(i18next.t("inputs.pdffile.description"));
|
||||||
sortIndexes = parsePageIndexSpecification(arrangementConfig, pageCount);
|
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);
|
// console.log("images:", images);
|
||||||
for (const image of images) {
|
for (const image of images) {
|
||||||
const data = await checkForQROnImage(image);
|
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);
|
pagesWithQR.push(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user