2023-11-17 00:32:09 +03:00
|
|
|
|
|
|
|
import { PdfFile } from '../wrappers/PdfFile.js';
|
|
|
|
import { getPages } from './common/getPagesByIndex.js';
|
2023-11-17 13:15:20 +03:00
|
|
|
import { parsePageIndexSpecification } from './common/pageIndexesUtils'
|
2023-11-17 00:32:09 +03:00
|
|
|
|
|
|
|
export type ExtractPagesParamsType = {
|
|
|
|
file: PdfFile;
|
2023-11-17 20:38:45 +01:00
|
|
|
pageIndecies: 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-17 20:38:45 +01:00
|
|
|
const { file, pageIndecies: pageIndecies } = params;
|
2023-11-17 00:32:09 +03:00
|
|
|
const pdfLibDocument = await file.pdfLibDocument;
|
|
|
|
|
2023-11-17 20:38:45 +01:00
|
|
|
var indecies = pageIndecies;
|
2023-11-17 00:32:09 +03:00
|
|
|
|
2023-11-17 20:38:45 +01:00
|
|
|
if (!Array.isArray(indecies)) {
|
|
|
|
indecies = parsePageIndexSpecification(indecies, pdfLibDocument.getPageCount());
|
2023-11-17 00:32:09 +03:00
|
|
|
}
|
|
|
|
|
2023-11-17 20:38:45 +01:00
|
|
|
const newFile = await getPages(file, indecies);
|
2023-11-17 00:45:37 +03:00
|
|
|
newFile.filename += "_extractedPages"
|
|
|
|
return newFile;
|
2023-11-17 00:32:09 +03:00
|
|
|
}
|