Files
portfolio/app/composables/useResumePdf.ts
T
mahdiarghyani 3ecd1616e0 feat(resume): add PDF download functionality with server endpoint
- Add useResumePdf composable for client-side PDF download handling
- Implement /api/resume/pdf server endpoint for PDF generation
- Add puppeteer and @sparticuz/chromium dependencies for PDF rendering
- Add puppeteer-core for headless browser automation
- Update Vercel configuration to support PDF generation
- Include loading state management and error handling with toast notifications
- Implement blob download with automatic filename generation
- Add memory leak prevention through object URL revocation
2025-12-01 12:19:00 +03:30

52 lines
1.3 KiB
TypeScript

/**
* PDF Download Composable
* Handles PDF download logic with loading state and error handling
*/
export function useResumePdf() {
const isGenerating = ref(false)
const toast = useToast()
const { getPdfFilename } = useResumeData()
async function downloadPdf() {
isGenerating.value = true
try {
// Fetch PDF as blob (AC1)
const response = await $fetch('/api/resume/pdf', {
responseType: 'blob',
})
// Create object URL from blob (AC2)
const url = URL.createObjectURL(response as Blob)
// Create temporary anchor element and trigger download
const a = document.createElement('a')
a.href = url
a.download = getPdfFilename() // AC3: filename from composable
a.click()
// Revoke object URL to prevent memory leaks (AC8)
URL.revokeObjectURL(url)
} catch (error) {
// Error handling (AC6)
console.error('PDF generation failed:', error)
toast.add({
title: 'Error generating PDF',
description: 'Please try again',
color: 'error',
})
} finally {
// Always reset loading state (AC7)
isGenerating.value = false
}
}
// Return composable interface (AC4, AC5)
return {
isGenerating,
downloadPdf,
}
}