import { Box, Typography, FormControlLabel, Switch } from '@mui/material'; import { useEffect, useState } from 'react'; import ToolFileResult from '@components/result/ToolFileResult'; import TextFieldWithDesc from '@components/options/TextFieldWithDesc'; import ToolContent from '@components/ToolContent'; import { ToolComponentProps } from '@tools/defineTool'; import { parsePageRanges, rotatePdf } from './service'; import { CardExampleType } from '@components/examples/ToolExamples'; import { PDFDocument } from 'pdf-lib'; import ToolPdfInput from '@components/input/ToolPdfInput'; import SimpleRadio from '@components/options/SimpleRadio'; import { InitialValuesType, RotationAngle } from './types'; import { useTranslation } from 'react-i18next'; const initialValues: InitialValuesType = { rotationAngle: 90, applyToAllPages: true, pageRanges: '' }; const exampleCards: CardExampleType[] = [ { title: 'Rotate All Pages 90°', description: 'Rotate all pages in the PDF by 90 degrees clockwise', sampleText: '', sampleResult: '', sampleOptions: { rotationAngle: 90, applyToAllPages: true, pageRanges: '' } }, { title: 'Rotate Specific Pages 180°', description: 'Rotate pages 1 and 3 by 180 degrees', sampleText: '', sampleResult: '', sampleOptions: { rotationAngle: 180, applyToAllPages: false, pageRanges: '1,3' } }, { title: 'Rotate Page Range 270°', description: 'Rotate pages 2 through 5 by 270 degrees', sampleText: '', sampleResult: '', sampleOptions: { rotationAngle: 270, applyToAllPages: false, pageRanges: '2-5' } } ]; export default function RotatePdf({ title, longDescription }: ToolComponentProps) { const { t } = useTranslation(); const [input, setInput] = useState(null); const [result, setResult] = useState(null); const [isProcessing, setIsProcessing] = useState(false); const [totalPages, setTotalPages] = useState(0); const [pageRangePreview, setPageRangePreview] = useState(''); // Get the total number of pages when a PDF is uploaded useEffect(() => { const getPdfInfo = async () => { if (!input) { setTotalPages(0); return; } try { const arrayBuffer = await input.arrayBuffer(); const pdf = await PDFDocument.load(arrayBuffer); setTotalPages(pdf.getPageCount()); } catch (error) { console.error('Error getting PDF info:', error); setTotalPages(0); } }; getPdfInfo(); }, [input]); const onValuesChange = (values: InitialValuesType) => { const { pageRanges, applyToAllPages } = values; if (applyToAllPages) { setPageRangePreview( totalPages > 0 ? t('pdf.rotatePdf.allPagesWillBeRotated', { count: totalPages }) : '' ); return; } if (!totalPages || !pageRanges?.trim()) { setPageRangePreview(''); return; } try { const count = parsePageRanges(pageRanges, totalPages).length; setPageRangePreview(t('pdf.rotatePdf.pagesWillBeRotated', { count })); } catch (error) { setPageRangePreview(''); } }; const compute = async (values: InitialValuesType, input: File | null) => { if (!input) return; try { setIsProcessing(true); const rotatedPdf = await rotatePdf(input, values); setResult(rotatedPdf); } catch (error) { throw new Error('Error rotating PDF: ' + error); } finally { setIsProcessing(false); } }; const angleOptions: { value: RotationAngle; label: string }[] = [ { value: 90, label: t('pdf.rotatePdf.angleOptions.clockwise90') }, { value: 180, label: t('pdf.rotatePdf.angleOptions.upsideDown180') }, { value: 270, label: t('pdf.rotatePdf.angleOptions.counterClockwise270') } ]; return ( } resultComponent={ } getGroups={({ values, updateField }) => [ { title: t('pdf.rotatePdf.rotationSettings'), component: ( {t('pdf.rotatePdf.rotationAngle')} {angleOptions.map((angleOption) => ( { updateField('rotationAngle', angleOption.value); }} /> ))} { updateField('applyToAllPages', e.target.checked); }} /> } label={t('pdf.rotatePdf.applyToAllPages')} /> {!values.applyToAllPages && ( {totalPages > 0 && ( {t('pdf.rotatePdf.pdfPageCount', { count: totalPages })} )} { updateField('pageRanges', val); }} description={t('pdf.rotatePdf.pageRangesDescription')} placeholder={t('pdf.rotatePdf.pageRangesPlaceholder')} /> {pageRangePreview && ( {pageRangePreview} )} )} ) } ]} onValuesChange={onValuesChange} toolInfo={{ title: t('pdf.rotatePdf.toolInfo.title'), description: t('pdf.rotatePdf.toolInfo.description') }} /> ); }