这是一个非常适合用来巩固PHP进阶技能的实战项目,由于我是AI模型,我不需要“使用”这个项目,但我可以为你设计并提供一个完整、可运行的项目案例蓝图和核心代码片段。

这个案例将教你如何利用 FPDF 库,仅通过PHP原生代码(无需HTML转PDF的复杂库)生成一张专业的PDF发票。
项目名称:SimpleInvoice - PHP PDF发票生成器
核心目标与功能
- 输入:通过PHP数组或表单提交发票数据(公司信息、客户信息、商品明细、税率等)。
- 输出:生成一个可下载的PDF文件,并自动在浏览器中显示。
- 核心功能:
- 支持中文(通过嵌入字体)。
- 自动计算小计、税额、总计。
- 清晰的表格布局与标题对齐。
技术栈
- PHP (版本 >= 7.0)
- FPDF 库 (轻量级,直接生成PDF,无需依赖其他扩展)
- tFPDF (FPDF的扩展,用于轻松支持UTF-8和中文)
项目结构
invoice-project/
├── fpdf/ # FPDF + tFPDF 库文件 (需下载)
│ └── tcpdf.php (或 tFPDF.php)
├── font/ # 存放中文字体文件 (如 Microsoft YaHei.ttf)
├── config.php # 字体与公司默认信息配置
├── invoice.php # 核心PDF生成类
└── index.php # 演示入口 (模拟数据并生成PDF)
关键代码实现 (核心逻辑)
步骤1:配置 (config.php)
<?php
define('SELLER_NAME', '梦想科技有限公司');
define('SELLER_ADDRESS', '北京市朝阳区科技园88号');
define('SELLER_PHONE', '010-88886666');
define('COMPANY_LOGO', ''); // 可选:logo路径
define('DEFAULT_TAX_RATE', 0.13); // 13% 增值税
?>
步骤2:核心生成类 (invoice.php)
<?php
require_once('fpdf/tcpdf.php'); // 使用tFPDF支持UTF-8
class InvoiceGenerator extends tFPDF {
function generateInvoice($data) {
// 1. 初始化
$this->AddPage();
$this->AddUniFont(); // 注册中文字体
// 2. 输出公司头部
$this->SetFont('droidsansfallback', 'B', 16);
$this->Cell(0, 10, 'INVOICE / 发票', 0, 1, 'R');
$this->SetFont('droidsansfallback', '', 10);
$this->Cell(0, 6, '发票号: ' . $data['invoice_no'], 0, 1, 'R');
$this->Cell(0, 6, '日期: ' . $data['date'], 0, 1, 'R');
$this->Ln(10);
// 3. 显示买卖双方信息 (左侧买家,右侧卖家)
$this->SetFont('droidsansfallback', 'B', 12);
$this->Cell(0, 6, '卖家: ' . SELLER_NAME, 0, 1);
$this->SetFont('droidsansfallback', '', 10);
$this->Cell(0, 6, SELLER_ADDRESS, 0, 1);
$this->Cell(0, 6, '电话: ' . SELLER_PHONE, 0, 1);
$this->Ln(5);
$this->SetFont('droidsansfallback', 'B', 12);
$this->Cell(0, 6, '买家: ' . $data['buyer']['name'], 0, 1);
$this->SetFont('droidsansfallback', '', 10);
$this->Cell(0, 6, $data['buyer']['address'], 0, 1);
$this->Ln(10);
// 4. 绘制商品明细表格
$header = ['商品名称', '数量', '单价', '金额'];
$w = array(70, 30, 40, 40); // 列宽
$this->SetFillColor(200, 220, 255);
$this->SetFont('droidsansfallback', 'B', 10);
for ($i = 0; $i < count($header); $i++) {
$this->Cell($w[$i], 7, $header[$i], 1, 0, 'C', true);
}
$this->Ln();
$this->SetFont('droidsansfallback', '', 10);
$total = 0;
foreach ($data['items'] as $item) {
$amount = $item['qty'] * $item['price'];
$total += $amount;
$this->Cell($w[0], 6, $item['name'], 'LR');
$this->Cell($w[1], 6, $item['qty'], 'LR', 0, 'C');
$this->Cell($w[2], 6, '$' . number_format($item['price'], 2), 'LR', 0, 'R');
$this->Cell($w[3], 6, '$' . number_format($amount, 2), 'LR', 0, 'R');
$this->Ln();
}
// 底部划线
$this->Cell(array_sum($w), 0, '', 'T');
$this->Ln(10);
// 5. 计算税额与总计
$tax = $total * DEFAULT_TAX_RATE;
$grand_total = $total + $tax;
$this->SetFont('droidsansfallback', '', 10);
$this->Cell(140, 6, '小计 (不含税):', 0, 0, 'R');
$this->Cell(40, 6, '$' . number_format($total, 2), 0, 1, 'R');
$this->Cell(140, 6, '税额 (13%):', 0, 0, 'R');
$this->Cell(40, 6, '$' . number_format($tax, 2), 0, 1, 'R');
$this->SetFont('droidsansfallback', 'B', 14);
$this->Cell(140, 8, '总计:', 0, 0, 'R');
$this->Cell(40, 8, '$' . number_format($grand_total, 2), 0, 1, 'R');
return $this->Output('invoice_' . $data['invoice_no'] . '.pdf', 'I'); // 'I' 为内联显示
}
// 注册中文字体
function AddUniFont() {
$fontPath = __DIR__ . '/font/msyh.ttf'; // 确保有msyh.ttf (微软雅黑)
$this->AddFont('droidsansfallback', '', $fontPath, true);
}
}
?>
步骤3:测试入口 (index.php)
<?php
require_once('invoice.php');
// 模拟数据
$invoiceData = [
'invoice_no' => 'INV-20241001-001',
'date' => '2024-10-01',
'buyer' => [
'name' => '上海某贸易公司',
'address' => '上海市浦东新区陆家嘴金融中心'
],
'items' => [
['name' => 'PHP高性能服务器 (48核心)', 'qty' => 2, 'price' => 15000.00],
['name' => '企业级NAS存储 (100TB)', 'qty' => 1, 'price' => 35000.00],
['name' => '千兆交换机 (48口)', 'qty' => 4, 'price' => 2800.00]
]
];
$pdf = new InvoiceGenerator('P', 'mm', 'A4');
$pdf->generateInvoice($invoiceData);
?>
需要你自己完成的部分
- 下载字体:将微软雅黑 (
msyh.ttf) 或思源黑体放入/font/目录。 - 安装FPDF/tFPDF:从 tFPDF官网 下载
tcpdf.php放入fpdf/文件夹。 - 运行测试:用PHP内置服务器
php -S localhost:8000访问index.php。
扩展建议
- 添加Logo:在构造函数中用
Image()方法添加公司Logo。 - 条码:使用FPDF的条形码扩展。
- 邮件发送:生成PDF后通过
PHPMailer自动发送。 - 数据库源:从MySQL中读取数据生成发票。
这个案例涵盖了表格绘制、中文字体处理、数据计算、文件输出等关键点,足够你独立完成一个实用的发票生成功能,如果你在实际编码中遇到问题(例如字体加载失败或对齐问题),可以随时继续问我!