我尝试将文件 doc 或 docx 转换为 pdf,但结果与原始文件 doc/docx 不匹配,并且文件 pdf 中没有样式。我不知道为什么,因为这里我使用 tcpdf 和 phpword
这是我要转换的代码:
$filetarget = FileHelper::normalizePath($pathdirectory.'/'.$filename);
$objReader = \PhpOffice\PhpWord\IOFactory::createReader('Word2007');
$contents = $objReader->load($filetarget);
$tcpdfPath = Yii::getAlias('@baseApp') . '/vendor/tecnickcom/tcpdf';
\PhpOffice\PhpWord\Settings::setPdfRendererPath($tcpdfPath);
\PhpOffice\PhpWord\Settings::setPdfRendererName('TCPDF');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($contents,'PDF');
$fileresult = str_replace('.docx', '.pdf', $filetarget);
$objWriter->save($fileresult);
$toPdf = FileHelper::normalizePath($fileresult);
这是从docx转换为pdf后的结果的一部分
这是原始 docx 文件的一部分
我的代码有什么问题吗?
不幸的是,phpWord 非常基础,因此对于 DocX 到 PDF 输出,您可以看到它无法保留文本或分页符,也不支持列表或导出图像。
有关当前功能列表,请参阅
https://phpword.readthedocs.io/en/latest/intro.html#writers
由于它运行 OpenOffice 作为转换器,您可以尝试其他 PHP 方法来直接运行转换
我也遇到同样的问题 有什么解决办法吗?