import { Download, Loader2 } from "lucide-react"; import { useEffect, useMemo, useState } from "react"; import { formatHeaders } from "@/lib/api"; import { useFileStore } from "@/stores/file-store"; const PAGE_SIZES: Record = { A4: [595.28, 841.89], Letter: [612, 792], A3: [841.89, 1190.55], A5: [419.53, 595.28], }; const PREVIEW_HEIGHT = 220; function PdfPagePreview({ pageSize, orientation, margin, file, }: { pageSize: string; orientation: "portrait" | "landscape"; margin: number; file: File | null; }) { const [imgSize, setImgSize] = useState<{ w: number; h: number } | null>(null); const imgUrl = useMemo(() => (file ? URL.createObjectURL(file) : null), [file]); useEffect(() => { return () => { if (imgUrl) URL.revokeObjectURL(imgUrl); }; }, [imgUrl]); useEffect(() => { if (!imgUrl) { setImgSize(null); return; } const img = new Image(); img.onload = () => setImgSize({ w: img.naturalWidth, h: img.naturalHeight }); img.src = imgUrl; }, [imgUrl]); let [pageW, pageH] = PAGE_SIZES[pageSize] ?? PAGE_SIZES.A4; if (orientation === "landscape") [pageW, pageH] = [pageH, pageW]; const scale = PREVIEW_HEIGHT / pageH; const previewW = pageW * scale; const previewH = PREVIEW_HEIGHT; const marginScaled = margin * scale; const contentW = previewW - marginScaled * 2; const contentH = previewH - marginScaled * 2; let imgStyle: React.CSSProperties | null = null; if (imgSize && imgUrl && contentW > 0 && contentH > 0) { const imgScale = Math.min(contentW / imgSize.w, contentH / imgSize.h, 1); const scaledW = imgSize.w * imgScale; const scaledH = imgSize.h * imgScale; imgStyle = { width: scaledW, height: scaledH, position: "absolute" as const, left: marginScaled + (contentW - scaledW) / 2, top: marginScaled + (contentH - scaledH) / 2, objectFit: "contain" as const, }; } return (

Preview

{/* Margin area — dashed inner border */} {margin > 0 && (
)} {/* Image thumbnail */} {imgStyle && imgUrl && ( Preview )} {/* Empty state placeholder */} {!imgUrl && (
No image
)}
); } export function ImageToPdfSettings() { const { files, selectedIndex, processing, error, setProcessing, setError } = useFileStore(); const [pageSize, setPageSize] = useState<"A4" | "Letter" | "A3" | "A5">("A4"); const [orientation, setOrientation] = useState<"portrait" | "landscape">("portrait"); const [margin, setMargin] = useState(20); const [downloadUrl, setDownloadUrl] = useState(null); const handleProcess = async () => { if (files.length === 0) return; setProcessing(true); setError(null); setDownloadUrl(null); try { const formData = new FormData(); for (const file of files) { formData.append("file", file); } formData.append("settings", JSON.stringify({ pageSize, orientation, margin })); const res = await fetch("/api/v1/tools/image-to-pdf", { method: "POST", headers: formatHeaders(), body: formData, }); if (!res.ok) { const body = await res.json().catch(() => ({})); throw new Error(body.error || `Failed: ${res.status}`); } const result = await res.json(); setDownloadUrl(result.downloadUrl); } catch (err) { setError(err instanceof Error ? err.message : "PDF creation failed"); } finally { setProcessing(false); } }; const hasFiles = files.length > 0; return (

{files.length} image{files.length !== 1 ? "s" : ""} will be combined into a PDF, one image per page.

Orientation

{margin}pt
setMargin(Number(e.target.value))} className="w-full mt-1" />
0 ? (files[selectedIndex] ?? files[0]) : null} /> {error &&

{error}

} {downloadUrl && ( Download PDF )}
); }