import React, { useState } from "react"; import axios from "axios"; import { Button, Select, TextInput, Checkbox, Notification, Stack, } from "@mantine/core"; import DownloadIcon from "@mui/icons-material/Download"; export default function SplitPdfPanel({ file, downloadUrl, setDownloadUrl }) { const [mode, setMode] = useState("byPages"); const [pageNumbers, setPageNumbers] = useState(""); const [horizontalDivisions, setHorizontalDivisions] = useState("0"); const [verticalDivisions, setVerticalDivisions] = useState("1"); const [mergeSections, setMergeSections] = useState(false); const [splitType, setSplitType] = useState("size"); const [splitValue, setSplitValue] = useState(""); const [bookmarkLevel, setBookmarkLevel] = useState("0"); const [includeMetadata, setIncludeMetadata] = useState(false); const [allowDuplicates, setAllowDuplicates] = useState(false); const [status, setStatus] = useState(""); const [isLoading, setIsLoading] = useState(false); const [errorMessage, setErrorMessage] = useState(null); const handleSubmit = async (e) => { e.preventDefault(); if (!file) { setStatus("Please upload a PDF first."); return; } const formData = new FormData(); formData.append("fileInput", file.file); let endpoint = ""; switch (mode) { case "byPages": formData.append("pageNumbers", pageNumbers); endpoint = "/api/v1/general/split-pages"; break; case "bySections": formData.append("horizontalDivisions", horizontalDivisions); formData.append("verticalDivisions", verticalDivisions); formData.append("merge", mergeSections); 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); formData.append("allowDuplicates", allowDuplicates); endpoint = "/api/v1/general/split-pdf-by-chapters"; break; default: return; } setStatus("Processing split..."); 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); setStatus("Download ready."); } catch (error) { console.error(error); setErrorMessage(error.response?.data || "An error occurred while splitting the PDF."); setStatus("Split failed."); } finally { setIsLoading(false); } }; return (
); }