import { useState } from "preact/hooks"; interface PdfDownloadButtonProps { className?: string; } export default function PdfDownloadButton({ className = "", }: PdfDownloadButtonProps) { const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const handleDownload = async () => { setIsLoading(true); setError(null); try { const response = await fetch("/api/resume/pdf"); if (!response.ok) { throw new Error( `Failed to generate PDF: ${response.status} ${response.statusText}`, ); } const blob = await response.blob(); const url = window.URL.createObjectURL(blob); // Create a temporary link element and trigger download const link = document.createElement("a"); link.href = url; link.download = "Atridad_Lahiji_Resume.pdf"; document.body.appendChild(link); link.click(); // Clean up document.body.removeChild(link); window.URL.revokeObjectURL(url); } catch (err) { console.error("Error downloading PDF:", err); setError(err instanceof Error ? err.message : "Failed to download PDF"); } finally { setIsLoading(false); } }; return (
{error &&
{error}
}
); }