2025-05-21 21:47:44 +01:00
|
|
|
import React, { useState } from "react";
|
|
|
|
import axios from "axios";
|
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
Select,
|
|
|
|
TextInput,
|
|
|
|
Checkbox,
|
|
|
|
Notification,
|
|
|
|
Stack,
|
2025-05-27 19:22:26 +01:00
|
|
|
Paper,
|
2025-05-21 21:47:44 +01:00
|
|
|
} from "@mantine/core";
|
2025-05-27 19:22:26 +01:00
|
|
|
import { useSearchParams } from "react-router-dom";
|
2025-05-29 17:26:32 +01:00
|
|
|
import { useTranslation } from "react-i18next";
|
2025-05-21 21:47:44 +01:00
|
|
|
import DownloadIcon from "@mui/icons-material/Download";
|
2025-06-04 19:04:56 +01:00
|
|
|
import { FileWithUrl } from "../types/file";
|
|
|
|
import { fileStorage } from "../services/fileStorage";
|
2025-05-21 21:47:44 +01:00
|
|
|
|
|
|
|
export interface SplitPdfPanelProps {
|
2025-06-04 19:04:56 +01:00
|
|
|
file: { file: FileWithUrl; url: string } | null;
|
2025-05-21 21:47:44 +01:00
|
|
|
downloadUrl?: string | null;
|
|
|
|
setDownloadUrl: (url: string | null) => void;
|
|
|
|
params: {
|
|
|
|
mode: string;
|
|
|
|
pages: string;
|
|
|
|
hDiv: string;
|
|
|
|
vDiv: string;
|
|
|
|
merge: boolean;
|
|
|
|
splitType: string;
|
|
|
|
splitValue: string;
|
|
|
|
bookmarkLevel: string;
|
|
|
|
includeMetadata: boolean;
|
|
|
|
allowDuplicates: boolean;
|
|
|
|
};
|
2025-05-27 19:22:26 +01:00
|
|
|
updateParams: (newParams: Partial<SplitPdfPanelProps["params"]>) => void;
|
2025-05-21 21:47:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const SplitPdfPanel: React.FC<SplitPdfPanelProps> = ({
|
|
|
|
file,
|
|
|
|
downloadUrl,
|
|
|
|
setDownloadUrl,
|
|
|
|
params,
|
|
|
|
updateParams,
|
|
|
|
}) => {
|
2025-05-29 17:26:32 +01:00
|
|
|
const { t } = useTranslation();
|
2025-05-27 19:22:26 +01:00
|
|
|
const [searchParams] = useSearchParams();
|
|
|
|
|
2025-05-21 21:47:44 +01:00
|
|
|
const [status, setStatus] = useState("");
|
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
const [errorMessage, setErrorMessage] = useState<string | null>(null);
|
|
|
|
|
|
|
|
const {
|
2025-05-27 19:22:26 +01:00
|
|
|
mode,
|
|
|
|
pages,
|
|
|
|
hDiv,
|
|
|
|
vDiv,
|
|
|
|
merge,
|
|
|
|
splitType,
|
|
|
|
splitValue,
|
|
|
|
bookmarkLevel,
|
|
|
|
includeMetadata,
|
|
|
|
allowDuplicates,
|
2025-05-21 21:47:44 +01:00
|
|
|
} = params;
|
|
|
|
|
2025-05-27 19:22:26 +01:00
|
|
|
|
2025-05-21 21:47:44 +01:00
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
|
|
|
e.preventDefault();
|
|
|
|
if (!file) {
|
2025-05-29 17:26:32 +01:00
|
|
|
setStatus(t("noFileSelected"));
|
2025-05-21 21:47:44 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const formData = new FormData();
|
2025-06-04 19:04:56 +01:00
|
|
|
|
|
|
|
// Handle IndexedDB files
|
|
|
|
if (!file.file.id) {
|
|
|
|
setStatus(t("noFileSelected"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const storedFile = await fileStorage.getFile(file.file.id);
|
|
|
|
if (storedFile) {
|
|
|
|
const blob = new Blob([storedFile.data], { type: storedFile.type });
|
|
|
|
const actualFile = new File([blob], storedFile.name, {
|
|
|
|
type: storedFile.type,
|
|
|
|
lastModified: storedFile.lastModified
|
|
|
|
});
|
|
|
|
formData.append("fileInput", actualFile);
|
|
|
|
}
|
2025-05-21 21:47:44 +01:00
|
|
|
|
|
|
|
let endpoint = "";
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case "byPages":
|
|
|
|
formData.append("pageNumbers", pages);
|
|
|
|
endpoint = "/api/v1/general/split-pages";
|
|
|
|
break;
|
|
|
|
case "bySections":
|
|
|
|
formData.append("horizontalDivisions", hDiv);
|
|
|
|
formData.append("verticalDivisions", vDiv);
|
|
|
|
formData.append("merge", merge.toString());
|
|
|
|
endpoint = "/api/v1/general/split-pdf-by-sections";
|
|
|
|
break;
|
|
|
|
case "bySizeOrCount":
|
|
|
|
formData.append(
|
|
|
|
"splitType",
|
|
|
|
splitType === "size" ? "0" : splitType === "pages" ? "1" : "2"
|
|
|
|
);
|
|
|
|
formData.append("splitValue", splitValue);
|
|
|
|
endpoint = "/api/v1/general/split-by-size-or-count";
|
|
|
|
break;
|
|
|
|
case "byChapters":
|
|
|
|
formData.append("bookmarkLevel", bookmarkLevel);
|
|
|
|
formData.append("includeMetadata", includeMetadata.toString());
|
|
|
|
formData.append("allowDuplicates", allowDuplicates.toString());
|
|
|
|
endpoint = "/api/v1/general/split-pdf-by-chapters";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-05-29 17:26:32 +01:00
|
|
|
setStatus(t("loading"));
|
2025-05-21 21:47:44 +01:00
|
|
|
setIsLoading(true);
|
|
|
|
setErrorMessage(null);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await axios.post(endpoint, formData, { responseType: "blob" });
|
|
|
|
const blob = new Blob([response.data], { type: "application/zip" });
|
|
|
|
const url = window.URL.createObjectURL(blob);
|
|
|
|
setDownloadUrl(url);
|
2025-05-29 17:26:32 +01:00
|
|
|
setStatus(t("downloadComplete"));
|
2025-05-21 21:47:44 +01:00
|
|
|
} catch (error: any) {
|
|
|
|
console.error(error);
|
2025-06-04 23:02:53 +01:00
|
|
|
let errorMsg = t("error.pdfPassword", "An error occurred while splitting the PDF.");
|
|
|
|
if (error.response?.data && typeof error.response.data === 'string') {
|
|
|
|
errorMsg = error.response.data;
|
|
|
|
} else if (error.message) {
|
|
|
|
errorMsg = error.message;
|
|
|
|
}
|
|
|
|
setErrorMessage(errorMsg);
|
2025-05-29 17:26:32 +01:00
|
|
|
setStatus(t("error._value", "Split failed."));
|
2025-05-21 21:47:44 +01:00
|
|
|
} finally {
|
|
|
|
setIsLoading(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2025-06-06 17:20:06 +01:00
|
|
|
<form onSubmit={handleSubmit} className="app-surface p-app-md rounded-app-md">
|
2025-05-27 19:22:26 +01:00
|
|
|
<Stack gap="sm" mb={16}>
|
|
|
|
<Select
|
2025-05-29 17:26:32 +01:00
|
|
|
label={t("split-by-size-or-count.type.label", "Split Mode")}
|
2025-05-27 19:22:26 +01:00
|
|
|
value={mode}
|
|
|
|
onChange={(v) => v && updateParams({ mode: v })}
|
|
|
|
data={[
|
2025-05-29 17:26:32 +01:00
|
|
|
{ value: "byPages", label: t("split.header", "Split by Pages") + " (e.g. 1,3,5-10)" },
|
|
|
|
{ value: "bySections", label: t("split-by-sections.title", "Split by Grid Sections") },
|
|
|
|
{ value: "bySizeOrCount", label: t("split-by-size-or-count.title", "Split by Size or Count") },
|
|
|
|
{ value: "byChapters", label: t("splitByChapters.title", "Split by Chapters") },
|
2025-05-27 19:22:26 +01:00
|
|
|
]}
|
2025-05-21 21:47:44 +01:00
|
|
|
/>
|
|
|
|
|
2025-05-27 19:22:26 +01:00
|
|
|
{mode === "byPages" && (
|
2025-05-21 21:47:44 +01:00
|
|
|
<TextInput
|
2025-05-29 17:26:32 +01:00
|
|
|
label={t("split.splitPages", "Pages")}
|
|
|
|
placeholder={t("pageSelectionPrompt", "e.g. 1,3,5-10")}
|
2025-05-27 19:22:26 +01:00
|
|
|
value={pages}
|
|
|
|
onChange={(e) => updateParams({ pages: e.target.value })}
|
2025-05-21 21:47:44 +01:00
|
|
|
/>
|
2025-05-27 19:22:26 +01:00
|
|
|
)}
|
|
|
|
|
|
|
|
{mode === "bySections" && (
|
|
|
|
<Stack gap="sm">
|
|
|
|
<TextInput
|
2025-05-29 17:26:32 +01:00
|
|
|
label={t("split-by-sections.horizontal.label", "Horizontal Divisions")}
|
2025-05-27 19:22:26 +01:00
|
|
|
type="number"
|
|
|
|
min="0"
|
|
|
|
max="300"
|
|
|
|
value={hDiv}
|
|
|
|
onChange={(e) => updateParams({ hDiv: e.target.value })}
|
2025-05-29 17:26:32 +01:00
|
|
|
placeholder={t("split-by-sections.horizontal.placeholder", "Enter number of horizontal divisions")}
|
2025-05-27 19:22:26 +01:00
|
|
|
/>
|
|
|
|
<TextInput
|
2025-05-29 17:26:32 +01:00
|
|
|
label={t("split-by-sections.vertical.label", "Vertical Divisions")}
|
2025-05-27 19:22:26 +01:00
|
|
|
type="number"
|
|
|
|
min="0"
|
|
|
|
max="300"
|
|
|
|
value={vDiv}
|
|
|
|
onChange={(e) => updateParams({ vDiv: e.target.value })}
|
2025-05-29 17:26:32 +01:00
|
|
|
placeholder={t("split-by-sections.vertical.placeholder", "Enter number of vertical divisions")}
|
2025-05-27 19:22:26 +01:00
|
|
|
/>
|
|
|
|
<Checkbox
|
2025-05-29 17:26:32 +01:00
|
|
|
label={t("split-by-sections.merge", "Merge sections into one PDF")}
|
2025-05-27 19:22:26 +01:00
|
|
|
checked={merge}
|
|
|
|
onChange={(e) => updateParams({ merge: e.currentTarget.checked })}
|
|
|
|
/>
|
|
|
|
</Stack>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{mode === "bySizeOrCount" && (
|
|
|
|
<Stack gap="sm">
|
|
|
|
<Select
|
2025-05-29 17:26:32 +01:00
|
|
|
label={t("split-by-size-or-count.type.label", "Split Type")}
|
2025-05-27 19:22:26 +01:00
|
|
|
value={splitType}
|
|
|
|
onChange={(v) => v && updateParams({ splitType: v })}
|
|
|
|
data={[
|
2025-05-29 17:26:32 +01:00
|
|
|
{ value: "size", label: t("split-by-size-or-count.type.size", "By Size") },
|
|
|
|
{ value: "pages", label: t("split-by-size-or-count.type.pageCount", "By Page Count") },
|
|
|
|
{ value: "docs", label: t("split-by-size-or-count.type.docCount", "By Document Count") },
|
2025-05-27 19:22:26 +01:00
|
|
|
]}
|
|
|
|
/>
|
|
|
|
<TextInput
|
2025-05-29 17:26:32 +01:00
|
|
|
label={t("split-by-size-or-count.value.label", "Split Value")}
|
|
|
|
placeholder={t("split-by-size-or-count.value.placeholder", "e.g. 10MB or 5 pages")}
|
2025-05-27 19:22:26 +01:00
|
|
|
value={splitValue}
|
|
|
|
onChange={(e) => updateParams({ splitValue: e.target.value })}
|
|
|
|
/>
|
|
|
|
</Stack>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{mode === "byChapters" && (
|
|
|
|
<Stack gap="sm">
|
|
|
|
<TextInput
|
2025-05-29 17:26:32 +01:00
|
|
|
label={t("splitByChapters.bookmarkLevel", "Bookmark Level")}
|
2025-05-27 19:22:26 +01:00
|
|
|
type="number"
|
|
|
|
value={bookmarkLevel}
|
|
|
|
onChange={(e) => updateParams({ bookmarkLevel: e.target.value })}
|
|
|
|
/>
|
|
|
|
<Checkbox
|
2025-05-29 17:26:32 +01:00
|
|
|
label={t("splitByChapters.includeMetadata", "Include Metadata")}
|
2025-05-27 19:22:26 +01:00
|
|
|
checked={includeMetadata}
|
|
|
|
onChange={(e) => updateParams({ includeMetadata: e.currentTarget.checked })}
|
|
|
|
/>
|
|
|
|
<Checkbox
|
2025-05-29 17:26:32 +01:00
|
|
|
label={t("splitByChapters.allowDuplicates", "Allow Duplicate Bookmarks")}
|
2025-05-27 19:22:26 +01:00
|
|
|
checked={allowDuplicates}
|
|
|
|
onChange={(e) => updateParams({ allowDuplicates: e.currentTarget.checked })}
|
|
|
|
/>
|
|
|
|
</Stack>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<Button type="submit" loading={isLoading} fullWidth>
|
2025-05-29 17:26:32 +01:00
|
|
|
{isLoading ? t("loading") : t("split.submit", "Split PDF")}
|
2025-05-21 21:47:44 +01:00
|
|
|
</Button>
|
2025-05-27 19:22:26 +01:00
|
|
|
|
2025-06-06 17:20:06 +01:00
|
|
|
{status && <p className="text-xs text-text-muted">{status}</p>}
|
2025-05-27 19:22:26 +01:00
|
|
|
|
|
|
|
{errorMessage && (
|
2025-05-29 17:26:32 +01:00
|
|
|
<Notification color="red" title={t("error._value", "Error")} onClose={() => setErrorMessage(null)}>
|
2025-05-27 19:22:26 +01:00
|
|
|
{errorMessage}
|
|
|
|
</Notification>
|
|
|
|
)}
|
|
|
|
|
2025-05-29 17:26:32 +01:00
|
|
|
{status === t("downloadComplete") && downloadUrl && (
|
2025-05-27 19:22:26 +01:00
|
|
|
<Button
|
|
|
|
component="a"
|
|
|
|
href={downloadUrl}
|
|
|
|
download="split_output.zip"
|
|
|
|
leftSection={<DownloadIcon />}
|
|
|
|
color="green"
|
|
|
|
fullWidth
|
|
|
|
>
|
2025-05-29 17:26:32 +01:00
|
|
|
{t("downloadPdf", "Download Split PDF")}
|
2025-05-27 19:22:26 +01:00
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
</Stack>
|
|
|
|
</form>
|
2025-05-21 21:47:44 +01:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SplitPdfPanel;
|