gpt4 book ai didi

php - 从 PHPWord 生成的 .docx 生成 PDF

转载 作者:可可西里 更新时间:2023-11-01 12:21:55 24 4
gpt4 key购买 nike

我正在使用 PHPWord 从模板创建 .docx 文件。它工作正常,但现在我想将生成的文件转换为 PDF

首先,我尝试将 tcpdfPHPWord 结合使用

$wordPdf = \PhpOffice\PhpWord\IOFactory::load($filename.".docx");

\PhpOffice\PhpWord\Settings::setPdfRendererPath(dirname(__FILE__)."/../../Office/tcpdf");
\PhpOffice\PhpWord\Settings::setPdfRendererName('TCPDF');

$pdfWriter = \PhpOffice\PhpWord\IOFactory::createWriter($wordPdf , 'PDF');
if (file_exists($filename.".pdf")) unlink($filename.".pdf");
$pdfWriter->save($filename.".pdf");

但是当我尝试加载文件以将其转换为 PDF 时,我在加载文件时遇到以下异常

Fatal error: Uncaught exception 'BadMethodCallException' with message 'Cannot add PreserveText in Section.'

经过一些研究,我发现其他一些人也有这个错误(phpWord - Cannot add PreserveText in Section)

编辑

After trying around some more I found out, that the Exception only occurs when I have some mail merge fields in my document. Once I removed them the Exception does not come up anymore, but the converted PDF files look horrible. All style information are gone and I can't use the result, so the need for an alternative stays.



我想过用另一种方式生成PDF,但我只能找到4种方式:

  1. 使用 OpenOffice - 不可能,因为我无法在服务器上安装任何软件。也按照提到的方式here也没有工作,因为我的主机(Strato)使用 SunOS 作为操作系统,这需要 Linux
  2. 使用 phpdocx - 我没有任何预算来支付它,演示无法创建 PDF
  3. 使用 PHPLiveDocx - 这可行,但有每天 250 个文档和每小时 20 个文档的限制,我必须一次转换大约 300 个文档,甚至可能一天转换多次
  4. 使用 PHP-Digital-Format-Convert - 输出看起来比使用 PHPWordtcpdf 更好,但仍然无法使用,因为缺少图像,而且大多数(不是全部!)样式<

是否有生成 PDF 的第 5 种方法?或者有没有什么办法可以让生成的PDF文档好看一些?

最佳答案

我用了Gears/pdf将 phpword 生成的 docx 文件转换为 PDF:

$success = Gears\Pdf::convert(
'file_path/file_name.docx',
'file_path/file_name.pdf');

关于php - 从 PHPWord 生成的 .docx 生成 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33084148/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com