You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否通过PHP从表单输入生成PDF?政务网站议程/纪要功能咨询

PHP从表单输入生成PDF的实现方案

完全可以用PHP实现从HTML表单输入生成会议议程、纪要这类PDF文档,行业内有几个成熟的类库可以直接落地,下面是具体的方案和注意事项:

常用PHP PDF生成类库

  • TCPDF:功能最全面的类库之一,支持Unicode、多种页面尺寸、自定义字体、表格、图片嵌入等,适合生成结构复杂的政府会议文档,比如带签章、多章节的纪要
  • FPDF:轻量简洁,学习成本低,适合基础的PDF生成需求,但原生对Unicode支持有限,需要额外扩展(比如tFPDF)来处理中文等非英文字符
  • Dompdf:可以直接将HTML/CSS转换为PDF,非常适合把表单提交的内容拼接成网页结构后快速生成PDF,能最大程度还原网页样式,对会议议程这种结构化内容生成很友好

快速实现示例(以Dompdf为例)

1. 安装Dompdf

通过Composer安装:

composer require dompdf/dompdf

2. 核心代码实现

<?php
// 引入Dompdf自动加载文件
require_once 'vendor/autoload.php';
use Dompdf\Dompdf;
use Dompdf\Options;

// 安全过滤表单输入,防止XSS注入
$meetingTitle = htmlspecialchars($_POST['meeting_title'] ?? '');
$meetingDate = htmlspecialchars($_POST['meeting_date'] ?? '');
// 处理多选项的议程项
$agendaItems = isset($_POST['agenda_items']) ? array_map('htmlspecialchars', $_POST['agenda_items']) : [];
$meetingMinutes = htmlspecialchars($_POST['meeting_minutes'] ?? '');

// 拼接会议文档的HTML结构
$htmlContent = <<<HTML
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <style>
        body { font-family: "SimSun", serif; }
        .document-header { text-align: center; margin-bottom: 30px; }
        .document-header h1 { color: #1a508b; margin: 0; }
        .section { margin: 25px 0; }
        .section-title { border-bottom: 1px solid #ccc; padding-bottom: 8px; color: #2d3748; }
        .agenda-list { line-height: 1.8; padding-left: 20px; }
        .minutes-content { line-height: 1.7; text-indent: 2em; }
    </style>
</head>
<body>
    <div class="document-header">
        <h1>{$meetingTitle}</h1>
        <p style="margin-top: 10px;"><strong>会议日期:</strong>{$meetingDate}</p>
    </div>

    <div class="section">
        <h2 class="section-title">会议议程</h2>
        <ul class="agenda-list">
            <?php foreach ($agendaItems as $item): ?>
                <li><?= $item ?></li>
            <?php endforeach; ?>
        </ul>
    </div>

    <div class="section">
        <h2 class="section-title">会议纪要</h2>
        <div class="minutes-content">{$meetingMinutes}</div>
    </div>
</body>
</html>
HTML;

// 初始化Dompdf配置
$pdfOptions = new Options();
$pdfOptions->set('isHtml5ParserEnabled', true);
$pdfOptions->set('isPhpEnabled', true);
// 支持中文,需确保服务器有对应字体
$pdfOptions->set('defaultFont', 'SimSun');

$dompdf = new Dompdf($pdfOptions);
// 加载HTML内容
$dompdf->loadHtml($htmlContent);
// 设置纸张规格
$dompdf->setPaper('A4', 'portrait');
// 渲染PDF
$dompdf->render();

// 输出PDF供下载,Attachment设为true表示下载,false则在线预览
$dompdf->stream("{$meetingTitle}_会议文档.pdf", ["Attachment" => true]);
?>

安全注意事项(针对政府机构场景)

  • 输入过滤:所有表单输入必须经过htmlspecialchars或更严格的过滤,防止XSS注入,避免恶意代码通过HTML解析影响PDF生成或服务器安全
  • 权限控制:确保生成功能仅对授权的内部人员开放,需结合登录验证、角色权限校验(比如仅会议管理员可操作)
  • 文件存储:如果需要将PDF保存到服务器,需设置严格的文件权限(比如仅服务器进程可读写),并避免存储在web可直接访问的目录下
  • 数据验证:对表单字段做格式校验(比如会议日期必须是有效格式、议程内容长度限制),保证生成的文档数据合规

内容的提问来源于stack exchange,提问作者Matthew McColm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 13:17:01