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

求助:使用PHPWord库实现Word到PDF无格式丢失的转换

解决PHPWord转换证书类Word到PDF格式丢失问题

尝试将证书类Word文档转换为PDF时出现格式丢失,转换后的PDF与原文档格式不符。使用PHPWord库处理,附上代码但未找到有效解决方案,希望实现无格式丢失的转换。

用户提供的代码:

<?php
require_once 'vendor/autoload.php'; // Include PHPWord autoload.php

use PhpOffice\PhpWord\TemplateProcessor;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\IOFactory;

// Specify the PDF rendering library and its path
\PhpOffice\PhpWord\Settings::setPdfRenderer(\PhpOffice\PhpWord\Settings::PDF_RENDERER_DOMPDF, 'vendor/dompdf/dompdf');

// Load the template document
$templateFile = 'docs/certification-of-appreciation.docx'; // Path to your Word template
$templateProcessor = new TemplateProcessor($templateFile);

// Replace placeholders with dynamic content
$templateProcessor->setValue('USERNAME', $_SESSION['worker_name']); // Replace 'USERNAME' with your placeholder for the user's name
// You might need to replace 'SUPERVISOR_SIGNATURE' with the appropriate placeholder for the supervisor's signature

// Save the processed document
$processedFilePath = 'docs/certification-of-appreciation_processed.docx'; // Path to save the processed Word document
$templateProcessor->saveAs($processedFilePath);

// Load the processed document
$processedPhpWord = IOFactory::load($processedFilePath);

// Convert Word to PDF
$pdfWriter = IOFactory::createWriter($processedPhpWord, 'PDF');
$pdfFilePath = 'docs/Certificate.pdf'; // Path to save the PDF file
$pdfWriter->save($pdfFilePath);

// Send the PDF to the browser
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="Certificate.pdf"');
readfile($pdfFilePath);

// Clean up: delete temporary files if needed
unlink($processedFilePath);
unlink($pdfFilePath);

?>

实用解决方案

1. 更换PDF渲染引擎

DOMPDF对复杂排版支持有限,证书类文档建议换成TCPDF或MPDF,兼容性更好:

// 换成TCPDF(需先执行 composer require tecnickcom/tcpdf)
\PhpOffice\PhpWord\Settings::setPdfRenderer(\PhpOffice\PhpWord\Settings::PDF_RENDERER_TCPDF, 'vendor/tecnickcom/tcpdf');

// 或者换成MPDF(需先执行 composer require mpdf/mpdf)
\PhpOffice\PhpWord\Settings::setPdfRenderer(\PhpOffice\PhpWord\Settings::PDF_RENDERER_MPDF, 'vendor/mpdf/mpdf');

2. 确保字体正确加载

证书常用特殊字体,需在代码中指定字体路径,避免渲染时替换为默认字体:

// 加载模板前添加字体配置
$fontDir = realpath('fonts/'); // 替换为你的字体目录
Settings::setFontDir($fontDir);
Settings::setFonts([
    '证书字体名称' => [
        'normal' => '字体文件名-Regular.ttf',
        'bold' => '字体文件名-Bold.ttf'
    ]
]);

注意模板中使用的字体名称要和配置一致。

3. 直接用外部工具转换(推荐复杂文档)

绕过PHPWord的二次解析,用LibreOffice命令行直接转换处理后的docx,兼容性最佳:

$processedFilePath = 'docs/certification-of-appreciation_processed.docx';
$pdfFilePath = 'docs/Certificate.pdf';

// Windows系统命令(路径根据你的LibreOffice安装位置调整)
$command = "\"C:\Program Files\LibreOffice\program\soffice.exe\" --headless --convert-to pdf \"{$processedFilePath}\" --outdir \"docs/\"";
// Linux系统命令
// $command = "libreoffice --headless --convert-to pdf {$processedFilePath} --outdir docs/";

exec($command);

前提是服务器已安装LibreOffice,且PHP有权限执行系统命令。

4. 简化Word模板格式

避免在模板中使用浮动文本框、复杂嵌套表格、特殊样式等易丢失的元素,改用固定表格布局、嵌入式图片。

5. 匹配PDF页面设置

确保PDF输出的页面大小和方向与原Word文档一致:

$pdfWriter = IOFactory::createWriter($processedPhpWord, 'PDF');
$pdfWriter->setPaperSize('A4'); // 替换为你的文档尺寸
$pdfWriter->setPaperOrientation('portrait'); // 纵向或landscape横向
$pdfWriter->save($pdfFilePath);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 03:14:57