新增生成pdf
This commit is contained in:
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
namespace app\common\utils\pdf\service;
|
||||
|
||||
use TCPDF;
|
||||
use Exception;
|
||||
|
||||
class PdfGeneratorService
|
||||
{
|
||||
private $templatePath;
|
||||
private $defaultOptions;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
// 设置模板基础路径
|
||||
$this->templatePath = app_path() . '/common/utils/pdf/template/';
|
||||
// 设置 TCPDF 默认选项
|
||||
$this->defaultOptions = [
|
||||
'page_orientation' => 'P', // PDF_PAGE_ORIENTATION
|
||||
'unit' => 'mm', // PDF_UNIT
|
||||
'page_format' => 'A4', // PDF_PAGE_FORMAT
|
||||
'unicode' => true,
|
||||
'encoding' => 'UTF-8',
|
||||
// --- 修改字体设置 ---
|
||||
// 'font' => 'dejavusans', // DejaVu 字体有时可能需要额外配置
|
||||
'font' => 'stsongstdlight', // 使用 TCPDF 内置的简体中文支持字体
|
||||
'font_style' => '', // 字体样式 (e.g., 'B' for bold)
|
||||
'font_size' => 10,
|
||||
// --- 字体设置结束 ---
|
||||
'creator' => 'Your Company Internal System',
|
||||
'author' => 'Your Company',
|
||||
'margin_left' => 15,
|
||||
'margin_right' => 15,
|
||||
'margin_top' => 20,
|
||||
'margin_bottom' => 20,
|
||||
'margin_header' => 5,
|
||||
'margin_footer' => 10,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染模板并返回 HTML 字符串
|
||||
*
|
||||
* @param string $templateName 模板文件名 (不含 .php)
|
||||
* @param array $data 注入模板的数据
|
||||
* @return string 渲染后的 HTML
|
||||
* @throws Exception
|
||||
*/
|
||||
private function renderTemplate(string $templateName, array $data): string
|
||||
{
|
||||
$templateFile = $this->templatePath . $templateName . '.php';
|
||||
|
||||
if (!file_exists($templateFile)) {
|
||||
throw new Exception("PDF template file not found: {$templateFile}");
|
||||
}
|
||||
|
||||
// 提取数据变量到当前作用域,供模板使用
|
||||
extract($data);
|
||||
|
||||
// 开启输出缓冲
|
||||
ob_start();
|
||||
// 包含模板文件
|
||||
include $templateFile;
|
||||
// 获取缓冲区内容并清理缓冲区
|
||||
$htmlContent = ob_get_clean();
|
||||
|
||||
if ($htmlContent === false) {
|
||||
throw new Exception("Failed to render PDF template: {$templateName}");
|
||||
}
|
||||
|
||||
return $htmlContent;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成 PDF
|
||||
*
|
||||
* @param string $templateName 模板文件名 (不含 .php)
|
||||
* @param array $data 注入模板的数据
|
||||
* @param array $options (可选)覆盖默认的 TCPDF 选项
|
||||
* @return string PDF 二进制数据
|
||||
* @throws Exception
|
||||
*/
|
||||
public function generate(string $templateName, array $data, array $options = []): string
|
||||
{
|
||||
// 合并选项
|
||||
$options = array_merge($this->defaultOptions, $options);
|
||||
|
||||
// 渲染 HTML
|
||||
$html = $this->renderTemplate($templateName, $data);
|
||||
|
||||
// 创建 TCPDF 实例
|
||||
$pdf = new TCPDF(
|
||||
$options['page_orientation'],
|
||||
$options['unit'],
|
||||
$options['page_format'],
|
||||
$options['unicode'],
|
||||
$options['encoding']
|
||||
);
|
||||
|
||||
// 设置文档信息
|
||||
$pdf->SetCreator($options['creator']);
|
||||
$pdf->SetAuthor($options['author']);
|
||||
$pdf->SetTitle($templateName . ' - Generated PDF');
|
||||
|
||||
// --- 修改字体设置 ---
|
||||
// 使用支持中文的字体
|
||||
// 'stsongstdlight' 是 TCPDF 内置的简体中文 CJK 字体
|
||||
// 'cid0cs' 也是支持中文的字体,可以尝试
|
||||
$pdf->SetFont($options['font'], $options['font_style'], $options['font_size'], '', true);
|
||||
// --- 字体设置结束 ---
|
||||
|
||||
// 设置页边距
|
||||
$pdf->SetMargins($options['margin_left'], $options['margin_top'], $options['margin_right']);
|
||||
$pdf->SetHeaderMargin($options['margin_header']);
|
||||
$pdf->SetFooterMargin($options['margin_footer']);
|
||||
|
||||
// 设置自动分页
|
||||
$pdf->SetAutoPageBreak(TRUE, $options['margin_bottom']);
|
||||
|
||||
// 设置图像比例因子
|
||||
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
|
||||
|
||||
// 添加首页
|
||||
$pdf->AddPage();
|
||||
|
||||
// 写入 HTML 内容
|
||||
$pdf->writeHTML($html, true, false, true, false, '');
|
||||
|
||||
// 结束最后一页
|
||||
$pdf->endPage();
|
||||
|
||||
// 返回 PDF 字符串
|
||||
return $pdf->Output('', 'S'); // 'S' 返回字符串
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存 PDF 到文件
|
||||
*
|
||||
* @param string $templateName 模板文件名
|
||||
* @param array $data 注入模板的数据
|
||||
* @param string $filePath 保存的完整文件路径
|
||||
* @param array $options (可选)TCPDF 选项
|
||||
* @throws Exception
|
||||
*/
|
||||
public function saveToFile(string $templateName, array $data, string $filePath, array $options = [])
|
||||
{
|
||||
$options = array_merge($this->defaultOptions, $options);
|
||||
$html = $this->renderTemplate($templateName, $data);
|
||||
|
||||
$pdf = new TCPDF(
|
||||
$options['page_orientation'],
|
||||
$options['unit'],
|
||||
$options['page_format'],
|
||||
$options['unicode'],
|
||||
$options['encoding']
|
||||
);
|
||||
$pdf->SetCreator($options['creator']);
|
||||
$pdf->SetAuthor($options['author']);
|
||||
$pdf->SetTitle($templateName . ' - Saved PDF');
|
||||
|
||||
// --- 修改字体设置 ---
|
||||
$pdf->SetFont($options['font'], $options['font_style'], $options['font_size'], '', true);
|
||||
// --- 字体设置结束 ---
|
||||
|
||||
$pdf->SetMargins($options['margin_left'], $options['margin_top'], $options['margin_right']);
|
||||
$pdf->SetAutoPageBreak(TRUE, $options['margin_bottom']);
|
||||
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
|
||||
$pdf->AddPage();
|
||||
$pdf->writeHTML($html, true, false, true, false, '');
|
||||
$pdf->endPage();
|
||||
|
||||
// 确保目录存在
|
||||
$dir = dirname($filePath);
|
||||
if (!is_dir($dir)) {
|
||||
mkdir($dir, 0755, true);
|
||||
}
|
||||
|
||||
$pdf->Output($filePath, 'F'); // 'F' 保存到文件
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
namespace app\common\utils\pdf\service\document;
|
||||
|
||||
use app\common\utils\pdf\service\PdfGeneratorService;
|
||||
use Exception;
|
||||
|
||||
class ReimbursementDocumentService
|
||||
{
|
||||
private $pdfGenerator;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user