import puppeteer from 'puppeteer'; export interface PDFReportData { projectTitle: string; overallScore: number; totalPossible: number; grade: string; analysisDate: string; criteria: Array<{ name: string; score: number; maxScore: number; weight: number; weightedScore: number; percentage: number; feedback: string; strengths: string[]; improvements: string[]; }>; overview: { excellentItems: number; improvementItems: number; overallPerformance: number; }; improvementSuggestions: { overallSuggestions: string; maintainStrengths: Array<{ title: string; description: string; }>; keyImprovements: Array<{ title: string; subtitle?: string; description?: string; suggestions: string[]; }>; actionPlan: Array<{ phase: string; description: string; }>; }; } export class HTMLPDFGenerator { private generateHTML(data: PDFReportData): string { return ` AI 智能評審報告

AI 智能評審報告

專案名稱:${data.projectTitle}

分析日期:${data.analysisDate}

總評結果

${data.overallScore}/${data.totalPossible}
${data.grade}

統計概覽

${data.overview.excellentItems}
優秀項目
${data.overview.improvementItems}
待改進項目
${data.overview.overallPerformance}%
整體表現

評分明細

${data.criteria.map(item => ` `).join('')}
評分項目 得分 權重 加權分
${item.name} ${item.score}/${item.maxScore} ${item.weight}% ${item.weightedScore.toFixed(1)}

詳細分析

${data.criteria.map(item => `
${item.name}
得分:${item.score}/${item.maxScore} (${item.percentage.toFixed(1)}%)
AI 評語:${item.feedback}
${item.strengths.length > 0 ? `

優點:

    ${item.strengths.map(strength => `
  • ${strength}
  • `).join('')}
` : ''} ${item.improvements.length > 0 ? `

改進建議:

    ${item.improvements.map(improvement => `
  • ${improvement}
  • `).join('')}
` : ''}
`).join('')}

整體改進建議

${data.improvementSuggestions.overallSuggestions}

${data.improvementSuggestions.maintainStrengths.length > 0 ? `

繼續保持的優勢

${data.improvementSuggestions.maintainStrengths.map(strength => `
${strength.title}
${strength.description}
`).join('')}
` : ''} ${data.improvementSuggestions.keyImprovements.length > 0 ? `

重點改進方向

${data.improvementSuggestions.keyImprovements.map(improvement => `
${improvement.title}
${improvement.description ? `
${improvement.description}
` : ''} ${improvement.suggestions.length > 0 ? `
    ${improvement.suggestions.map(suggestion => `
  • ${suggestion}
  • `).join('')}
` : ''}
`).join('')}
` : ''} ${data.improvementSuggestions.actionPlan.length > 0 ? `

下一步行動計劃

${data.improvementSuggestions.actionPlan.map((action, index) => `
${index + 1}
${action.phase}
${action.description}
`).join('')}
` : ''} `; } public async generateReport(data: PDFReportData): Promise { const browser = await puppeteer.launch({ headless: true, args: [ '--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage', '--disable-accelerated-2d-canvas', '--no-first-run', '--no-zygote', '--disable-gpu' ], timeout: 30000 }); try { const page = await browser.newPage(); // 設置頁面內容 const html = this.generateHTML(data); await page.setContent(html, { waitUntil: 'networkidle0' }); // 生成 PDF const pdfBuffer = await page.pdf({ format: 'A4', printBackground: true, margin: { top: '20mm', right: '20mm', bottom: '20mm', left: '20mm' } }); return new Blob([pdfBuffer], { type: 'application/pdf' }); } finally { await browser.close(); } } } // 便捷函數 export async function generateHTMLPDFReport(data: PDFReportData): Promise { const generator = new HTMLPDFGenerator(); return generator.generateReport(data); }