2023-11-17 00:32:09 +03:00
2024-05-10 23:01:18 +02:00
import { PdfFile , RepresentationType } from "../wrappers/PdfFile" ;
import { Operator , Progress , oneToOne } from "." ;
import Joi from "@stirling-tools/joi" ;
import { JoiPDFFileSchema } from "../wrappers/PdfFileJoi" ;
import i18next from "i18next" ;
2024-02-23 23:55:29 +01:00
import { getPages } from "./common/getPagesByIndex" ;
2024-01-04 20:17:54 -05:00
import { parsePageIndexSpecification } from "./common/pageIndexesUtils" ;
2023-11-17 00:32:09 +03:00
2024-01-04 20:17:54 -05:00
export interface ExtractPagesParamsType {
2023-11-17 00:32:09 +03:00
file : PdfFile ;
2023-11-19 11:38:55 +03:00
pageIndexes : string | number [ ] ;
2023-11-17 00:32:09 +03:00
}
2023-11-17 00:45:37 +03:00
export async function extractPages ( params : ExtractPagesParamsType ) : Promise < PdfFile > {
2023-11-19 11:38:55 +03:00
const { file , pageIndexes } = params ;
2023-11-17 00:32:09 +03:00
const pdfLibDocument = await file . pdfLibDocument ;
2024-01-04 20:17:54 -05:00
let indexes = pageIndexes ;
2023-11-17 00:32:09 +03:00
2023-11-19 11:38:55 +03:00
if ( ! Array . isArray ( indexes ) ) {
indexes = parsePageIndexSpecification ( indexes , pdfLibDocument . getPageCount ( ) ) ;
2023-11-17 00:32:09 +03:00
}
2023-11-19 11:38:55 +03:00
const newFile = await getPages ( file , indexes ) ;
2024-01-04 20:17:54 -05:00
newFile . filename += "_extractedPages" ;
2023-11-17 00:45:37 +03:00
return newFile ;
2023-11-17 00:32:09 +03:00
}
2024-05-10 23:01:18 +02:00
export class ExtractPages extends Operator {
static type = "extractPages" ;
/ * *
* Validation & Localisation
* /
protected static inputSchema = JoiPDFFileSchema . label ( i18next . t ( "inputs.pdffile.name" ) ) . description ( i18next . t ( "inputs.pdffile.description" ) ) ;
protected static valueSchema = Joi . object ( {
pageIndexes : Joi.array ( ) . items ( Joi . number ( ) . integer ( ) ) . required ( )
. label ( i18next . t ( "values.pageIndexes.friendlyName" , { ns : "extractPages" } ) ) . description ( i18next . t ( "values.pageIndexes.description" , { ns : "extractPages" } ) )
. example ( "3" ) . example ( "4" ) . required ( )
} ) ;
protected static outputSchema = JoiPDFFileSchema . label ( i18next . t ( "outputs.pdffile.name" ) ) . description ( i18next . t ( "outputs.pdffile.description" ) ) ;
static schema = Joi . object ( {
input : ExtractPages.inputSchema ,
values : ExtractPages.valueSchema.required ( ) ,
output : ExtractPages.outputSchema
} ) . label ( i18next . t ( "friendlyName" , { ns : "extractPages" } ) ) . description ( i18next . t ( "description" , { ns : "extractPages" } ) ) ;
/ * *
* Logic
* /
/** PDF-Imposition, PDF-N-Up: Put multiple pages of the input document into a single page of the output document. - see: {@link https://en.wikipedia.org/wiki/N-up} */
async run ( input : PdfFile [ ] , progressCallback : ( state : Progress ) = > void ) : Promise < PdfFile [ ] > {
return oneToOne < PdfFile , PdfFile > ( input , async ( input , index , max ) = > {
const pdfLibDocument = await input . pdfLibDocument ;
let indexes = this . actionValues . pageIndexes ;
if ( ! Array . isArray ( indexes ) ) {
indexes = parsePageIndexSpecification ( indexes , pdfLibDocument . getPageCount ( ) ) ;
}
const newFile = await getPages ( input , indexes ) ;
newFile . filename += "_extractedPages" ;
progressCallback ( { curFileProgress : 1 , operationProgress : index / max } ) ;
return newFile ;
} ) ;
}
}