mirror of
https://github.com/mmahdium/portfolio.git
synced 2026-08-14 12:12:48 +03:30
- 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
52 lines
1.3 KiB
TypeScript
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,
|
|
}
|
|
}
|