mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-06-22 23:45:02 +00:00
remove pages and cleanup
This commit is contained in:
parent
df10eacf92
commit
2420e59cd8
@ -1,17 +1,22 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param selection An array of page indexes already selected.
|
* @param selection An array of page indexes already selected.
|
||||||
* @param pages A list of page indexes, or the number of total pages in the document (which will be converted into a list of page indexes).
|
* @param pageCount The number of pages of the pdfDocument.
|
||||||
* @returns An inverted selection array of page indexes.
|
* @returns An inverted selection array of page indexes.
|
||||||
*/
|
*/
|
||||||
export function invertSelection(selection: number[], pages: number|number[]): number[] {
|
export function invertSelection(selection: number[], pageCount: number): number[] {
|
||||||
const indexes = Array.isArray(pages) ? pages : [...Array(pages).keys()];
|
const newSelection = [];
|
||||||
const pageIndexesCopy = [...indexes];
|
for (let pageIndex = 0; pageIndex < pageCount; pageIndex++) {
|
||||||
return pageIndexesCopy.filter(x => !selection.includes(x));
|
if(!selection.includes(pageIndex))
|
||||||
|
newSelection.push(pageIndex);
|
||||||
|
}
|
||||||
|
return newSelection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// TODO: Port this to CommaArrayJoiExt.ts
|
||||||
/**
|
/**
|
||||||
* Parse the page selector string used in the 'PDF Page Organizer'
|
* Parse the page selector string
|
||||||
* @param specification
|
* @param specification
|
||||||
* @param totalPages
|
* @param totalPages
|
||||||
* @returns
|
* @returns
|
||||||
|
@ -7,7 +7,6 @@ import { JoiPDFFileSchema } from "../wrappers/PdfFileJoi";
|
|||||||
import i18next from "i18next";
|
import i18next from "i18next";
|
||||||
|
|
||||||
import { getPages } from "./common/getPagesByIndex";
|
import { getPages } from "./common/getPagesByIndex";
|
||||||
import { parsePageIndexSpecification } from "./common/pageIndexesUtils";
|
|
||||||
import CommaArrayJoiExt from "../wrappers/CommaArrayJoiExt";
|
import CommaArrayJoiExt from "../wrappers/CommaArrayJoiExt";
|
||||||
|
|
||||||
export class ExtractPages extends Operator {
|
export class ExtractPages extends Operator {
|
||||||
@ -39,15 +38,8 @@ export class ExtractPages extends Operator {
|
|||||||
/** PDF extraction, specify pages from one pdf and output them to a new pdf */
|
/** PDF extraction, specify pages from one pdf and output them to a new pdf */
|
||||||
async run(input: PdfFile[], progressCallback: (state: Progress) => void): Promise<PdfFile[]> {
|
async run(input: PdfFile[], progressCallback: (state: Progress) => void): Promise<PdfFile[]> {
|
||||||
return oneToOne<PdfFile, PdfFile>(input, async (input, index, max) => {
|
return oneToOne<PdfFile, PdfFile>(input, async (input, index, max) => {
|
||||||
const pdfLibDocument = await input.pdfLibDocument;
|
|
||||||
|
|
||||||
let indexes = this.actionValues.pageIndexes;
|
const newFile = await getPages(input, this.actionValues.pageIndexes);
|
||||||
|
|
||||||
if (!Array.isArray(indexes)) {
|
|
||||||
indexes = parsePageIndexSpecification(indexes, pdfLibDocument.getPageCount());
|
|
||||||
}
|
|
||||||
|
|
||||||
const newFile = await getPages(input, indexes);
|
|
||||||
newFile.filename += "_extractedPages";
|
newFile.filename += "_extractedPages";
|
||||||
progressCallback({ curFileProgress: 1, operationProgress: index/max });
|
progressCallback({ curFileProgress: 1, operationProgress: index/max });
|
||||||
|
|
||||||
|
@ -1,21 +1,56 @@
|
|||||||
|
|
||||||
import { PdfFile } from "../wrappers/PdfFile";
|
import { PdfFile } from "../wrappers/PdfFile";
|
||||||
|
import { Operator, Progress, oneToOne } from ".";
|
||||||
|
|
||||||
|
import Joi from "@stirling-tools/joi";
|
||||||
|
import { JoiPDFFileSchema } from "../wrappers/PdfFileJoi";
|
||||||
|
|
||||||
|
import i18next from "i18next";
|
||||||
|
|
||||||
import { getPages } from "./common/getPagesByIndex";
|
import { getPages } from "./common/getPagesByIndex";
|
||||||
import { invertSelection, parsePageIndexSpecification } from "./common/pageIndexesUtils";
|
import CommaArrayJoiExt from "../wrappers/CommaArrayJoiExt";
|
||||||
|
|
||||||
export interface RemovePagesParamsType {
|
import { invertSelection } from "./common/pageIndexesUtils";
|
||||||
file: PdfFile;
|
|
||||||
pageSelector: string;
|
export class RemovePages extends Operator {
|
||||||
}
|
static type = "removePages";
|
||||||
export async function removePages(params: RemovePagesParamsType) {
|
|
||||||
const { file, pageSelector } = params;
|
/**
|
||||||
const pdfDoc = await file.pdfLibDocument;
|
* Validation & Localisation
|
||||||
const pageCount = pdfDoc.getPageCount();
|
*/
|
||||||
|
|
||||||
const pageSelection = parsePageIndexSpecification(pageSelector, pageCount);
|
protected static inputSchema = JoiPDFFileSchema.label(i18next.t("inputs.pdffile.name")).description(i18next.t("inputs.pdffile.description"));
|
||||||
const pagesToKeep = invertSelection(pageSelection, pageCount);
|
protected static valueSchema = Joi.object({
|
||||||
|
pageIndexes: CommaArrayJoiExt.comma_array().items(Joi.number().integer()).required()
|
||||||
const newFile = await getPages(file, pagesToKeep);
|
.label(i18next.t("values.pageIndexes.friendlyName", { ns: "removePages" })).description(i18next.t("values.pageIndexes.description", { ns: "removePages" }))
|
||||||
newFile.filename += "_removedPages";
|
.example("1").example("1, 2, 3, 4").example("4, 2, 4, 3").required()
|
||||||
return newFile;
|
});
|
||||||
|
protected static outputSchema = JoiPDFFileSchema.label(i18next.t("outputs.pdffile.name")).description(i18next.t("outputs.pdffile.description"));
|
||||||
|
|
||||||
|
static schema = Joi.object({
|
||||||
|
input: RemovePages.inputSchema,
|
||||||
|
values: RemovePages.valueSchema.required(),
|
||||||
|
output: RemovePages.outputSchema
|
||||||
|
}).label(i18next.t("friendlyName", { ns: "removePages" })).description(i18next.t("description", { ns: "removePages" }));
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logic
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** PDF extraction, specify pages from one pdf and output them to a new pdf */
|
||||||
|
async run(input: PdfFile[], progressCallback: (state: Progress) => void): Promise<PdfFile[]> {
|
||||||
|
return oneToOne<PdfFile, PdfFile>(input, async (input, index, max) => {
|
||||||
|
|
||||||
|
const pdfDoc = await input.pdfLibDocument;
|
||||||
|
const pageCount = pdfDoc.getPageCount();
|
||||||
|
|
||||||
|
const pagesToKeep = invertSelection(this.actionValues.pageIndexes, pageCount);
|
||||||
|
|
||||||
|
const newFile = await getPages(input, pagesToKeep);
|
||||||
|
newFile.filename += "_removedPages";
|
||||||
|
progressCallback({ curFileProgress: 1, operationProgress: index/max });
|
||||||
|
|
||||||
|
return newFile;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
@ -6,13 +6,13 @@ export default Joi.extend((joi) => {
|
|||||||
type: 'comma_array',
|
type: 'comma_array',
|
||||||
base: joi.array(),
|
base: joi.array(),
|
||||||
messages: {
|
messages: {
|
||||||
'million.base': '{{#label}} must be a comma seperated list',
|
'comma_array.base': '{{#label}} must be a comma seperated list',
|
||||||
},
|
},
|
||||||
coerce: {
|
coerce: {
|
||||||
from: 'string',
|
from: 'string',
|
||||||
method(value, helpers) {
|
method(value, helpers) {
|
||||||
|
|
||||||
if (typeof value !== 'string' || !/(\d+)(,\s*\d+)*/.test(value)) {
|
if (typeof value !== 'string' || !/(\d+)(,\s*\d+)*/.test(value)) { // is string and in format "[number], [number]"
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user