pdfGenerator = new PdfGeneratorService(); } /** * 根据报销单 ID 生成 PDF 数据 * (这里模拟从数据库获取数据) * * @param int $reimbursementId * @return string PDF 二进制数据 * @throws Exception */ public function generatePdfById(int $reimbursementId): string { // --- 模拟从数据库获取数据 --- // 实际应用中,这里应该是查询数据库 $reimbursementData = $this->getMockReimbursementData($reimbursementId); if (!$reimbursementData) { throw new Exception("Reimbursement record not found for ID: {$reimbursementId}"); } // --- 数据获取结束 --- // 调用核心 PDF 服务生成 PDF // 'reimbursement_template' 对应 app/view/pdf/reimbursement_template.php return $this->pdfGenerator->generate('reimbursement', $reimbursementData); } /** * 模拟获取报销单数据 * @param int $id * @return array|null */ private function getMockReimbursementData(int $id) { if ($id <= 0) return null; return [ 'reimbursement_id' => 'REIM-' . str_pad($id, 5, '0', STR_PAD_LEFT), 'applicant_name' => '张三', 'department' => '技术部', 'application_date' => '2023-10-26', 'total_amount' => 1250.75, 'reason' => "参加北京技术研讨会的差旅费用。\n包括交通、住宿和餐饮。", 'items' => [ ['date' => '2023-10-20', 'category' => '交通费', 'description' => '高铁二等座', 'amount' => 520.00, 'remark' => ''], ['date' => '2023-10-21', 'category' => '住宿费', 'description' => 'XX酒店 2 晚', 'amount' => 680.00, 'remark' => '含早餐'], ['date' => '2023-10-21', 'category' => '餐饮费', 'description' => '午餐晚餐', 'amount' => 50.75, 'remark' => '发票已附'], ] ]; } /** * 保存报销单 PDF 到服务器 * @param int $reimbursementId * @param string $savePath 保存路径 (目录) * @throws Exception */ public function savePdfToFile(int $reimbursementId, string $savePath) { $reimbursementData = $this->getMockReimbursementData($reimbursementId); if (!$reimbursementData) { throw new Exception("Reimbursement record not found for ID: {$reimbursementId}"); } $filename = 'reimbursement_' . $reimbursementData['reimbursement_id'] . '_' . date('Ymd_His') . '.pdf'; $fullPath = rtrim($savePath, '/') . '/' . $filename; $this->pdfGenerator->saveToFile('reimbursement_template', $reimbursementData, $fullPath); } }