mirror of
https://github.com/arsvendg/Stirling-PDF.git
synced 2026-07-16 19:33:11 +02:00
Feature/v2/extract pages (#4828)
# Description of Changes - Add the extract pages tool - Componentize our bulk selection logic and warning messaages --- ## Checklist ### General - [ ] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [ ] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md) (if applicable) - [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md) (if applicable) - [ ] I have performed a self-review of my own code - [ ] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### Translations (if applicable) - [ ] I ran [`scripts/counter_translation.py`](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/docs/counter_translation.md) ### UI Changes (if applicable) - [ ] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [ ] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md#6-testing) for more details.
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import apiClient from '@app/services/apiClient';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { ToolType, useToolOperation } from '@app/hooks/tools/shared/useToolOperation';
|
||||
import { createStandardErrorHandler } from '@app/utils/toolErrorHandler';
|
||||
import { ExtractPagesParameters, defaultParameters } from '@app/hooks/tools/extractPages/useExtractPagesParameters';
|
||||
import { pdfWorkerManager } from '@app/services/pdfWorkerManager';
|
||||
import { parseSelection } from '@app/utils/bulkselection/parseSelection';
|
||||
|
||||
// Convert advanced page selection expression into CSV of explicit one-based page numbers
|
||||
async function resolveSelectionToCsv(expression: string, file: File): Promise<string> {
|
||||
// Load PDF to determine max pages
|
||||
const arrayBuffer = await file.arrayBuffer();
|
||||
const pdf = await pdfWorkerManager.createDocument(arrayBuffer, { disableAutoFetch: true, disableStream: true });
|
||||
try {
|
||||
const maxPages = pdf.numPages;
|
||||
const pages = parseSelection(expression || '', maxPages);
|
||||
return pages.join(',');
|
||||
} finally {
|
||||
pdfWorkerManager.destroyDocument(pdf);
|
||||
}
|
||||
}
|
||||
|
||||
export const extractPagesOperationConfig = {
|
||||
toolType: ToolType.custom,
|
||||
operationType: 'extractPages',
|
||||
customProcessor: async (parameters: ExtractPagesParameters, files: File[]): Promise<File[]> => {
|
||||
const outputs: File[] = [];
|
||||
|
||||
for (const file of files) {
|
||||
// Resolve selection into CSV acceptable by backend
|
||||
const csv = await resolveSelectionToCsv(parameters.pageNumbers, file);
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('fileInput', file);
|
||||
formData.append('pageNumbers', csv);
|
||||
|
||||
const response = await apiClient.post('/api/v1/general/rearrange-pages', formData, { responseType: 'blob' });
|
||||
|
||||
// Name output file with suffix
|
||||
const base = (file.name || 'document.pdf').replace(/\.[^.]+$/, '');
|
||||
const outName = `${base}_extracted_pages.pdf`;
|
||||
const outFile = new File([response.data], outName, { type: 'application/pdf' });
|
||||
outputs.push(outFile);
|
||||
}
|
||||
|
||||
return outputs;
|
||||
},
|
||||
defaultParameters,
|
||||
} as const;
|
||||
|
||||
export const useExtractPagesOperation = () => {
|
||||
const { t } = useTranslation();
|
||||
return useToolOperation<ExtractPagesParameters>({
|
||||
...extractPagesOperationConfig,
|
||||
getErrorMessage: createStandardErrorHandler(t('extractPages.error.failed', 'Failed to extract pages'))
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
import { BaseParameters } from '@app/types/parameters';
|
||||
import { useBaseParameters, BaseParametersHook } from '@app/hooks/tools/shared/useBaseParameters';
|
||||
|
||||
export interface ExtractPagesParameters extends BaseParameters {
|
||||
pageNumbers: string;
|
||||
}
|
||||
|
||||
export const defaultParameters: ExtractPagesParameters = {
|
||||
pageNumbers: '',
|
||||
};
|
||||
|
||||
export type ExtractPagesParametersHook = BaseParametersHook<ExtractPagesParameters>;
|
||||
|
||||
export const useExtractPagesParameters = (): ExtractPagesParametersHook => {
|
||||
return useBaseParameters({
|
||||
defaultParameters,
|
||||
endpointName: 'rearrange-pages',
|
||||
validateFn: (p) => (p.pageNumbers || '').trim().length > 0,
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user