zip download of single pdfs

This commit is contained in:
2026-05-17 02:57:39 +02:00
parent 13097b73fc
commit 4b0046a943
11 changed files with 272 additions and 27 deletions

View File

@@ -11,6 +11,11 @@ export interface SplitPdfDownload extends PdfDownload {
pageIndex: number;
}
export interface PdfBlobResult {
blob: Blob;
filename: string;
}
function revokeDownload(download: PdfDownload | null): void {
if (download) {
URL.revokeObjectURL(download.url);
@@ -37,30 +42,51 @@ export function usePdfGeneratedOutputs() {
const [exportDownload, setExportDownload] = useState<PdfDownload | null>(
null
);
const [splitZipDownload, setSplitZipDownload] = useState<PdfDownload | null>(
null
);
const splitDownloadsRef = useRef<SplitPdfDownload[]>([]);
const subsetDownloadRef = useRef<PdfDownload | null>(null);
const exportDownloadRef = useRef<PdfDownload | null>(null);
const splitZipDownloadRef = useRef<PdfDownload | null>(null);
const replaceSplitResults = useCallback((results: SplitResult[]) => {
const nextDownloads: SplitPdfDownload[] = results.map((result) => ({
...createDownload(
`split-${result.pageIndex}-${result.filename}`,
result.filename,
result.blob
),
pageIndex: result.pageIndex,
}));
const replaceSplitResults = useCallback(
(results: SplitResult[], zipResult?: PdfBlobResult) => {
const nextDownloads: SplitPdfDownload[] = results.map((result) => ({
...createDownload(
`split-${result.pageIndex}-${result.filename}`,
result.filename,
result.blob
),
pageIndex: result.pageIndex,
}));
revokeDownloads(splitDownloadsRef.current);
splitDownloadsRef.current = nextDownloads;
setSplitDownloads(nextDownloads);
}, []);
const nextZipDownload = zipResult
? createDownload('split-zip', zipResult.filename, zipResult.blob)
: null;
revokeDownloads(splitDownloadsRef.current);
revokeDownload(splitZipDownloadRef.current);
splitDownloadsRef.current = nextDownloads;
splitZipDownloadRef.current = nextZipDownload;
setSplitDownloads(nextDownloads);
setSplitZipDownload(nextZipDownload);
},
[]
);
const clearSplitResults = useCallback(() => {
revokeDownloads(splitDownloadsRef.current);
revokeDownload(splitZipDownloadRef.current);
splitDownloadsRef.current = [];
splitZipDownloadRef.current = null;
setSplitDownloads([]);
setSplitZipDownload(null);
}, []);
const replaceSubsetResult = useCallback((blob: Blob, filename: string) => {
@@ -95,14 +121,17 @@ export function usePdfGeneratedOutputs() {
revokeDownloads(splitDownloadsRef.current);
revokeDownload(subsetDownloadRef.current);
revokeDownload(exportDownloadRef.current);
revokeDownload(splitZipDownloadRef.current);
splitDownloadsRef.current = [];
subsetDownloadRef.current = null;
exportDownloadRef.current = null;
splitZipDownloadRef.current = null;
setSplitDownloads([]);
setSubsetDownload(null);
setExportDownload(null);
setSplitZipDownload(null);
}, []);
useEffect(() => {
@@ -110,6 +139,7 @@ export function usePdfGeneratedOutputs() {
revokeDownloads(splitDownloadsRef.current);
revokeDownload(subsetDownloadRef.current);
revokeDownload(exportDownloadRef.current);
revokeDownload(splitZipDownloadRef.current);
};
}, []);
@@ -117,6 +147,7 @@ export function usePdfGeneratedOutputs() {
splitDownloads,
subsetDownload,
exportDownload,
splitZipDownload,
replaceSplitResults,
clearSplitResults,
replaceSubsetResult,