从PHPWord生成的.docx生成PDF [英] Generate PDF from .docx generated by PHPWord
问题描述
我正在使用PHPWord
从模板创建.docx
文件.它工作正常,但现在我想将生成的文件转换为PDF
.
I am creating .docx
files from a template using PHPWord
. It works fine but now I want to convert the generated file to PDF
.
首先,我尝试将tcpdf
与PHPWord
$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
时,在加载文件时出现以下异常
but when I try to load the file to convert it to PDF
I get the following exception while loading the file
致命错误:消息无法在节中添加PreserveText"的未捕获异常"BadMethodCallException".
经过研究,我发现其他人也有此错误( phpWord -无法在部分中添加PreserveText )
编辑
尝试了更多之后,我发现
Exception
仅在我的文档中有一些mail merge fields
时出现.删除它们后,Exception
不再出现,但是转换后的
After trying around some more I found out, that the
Exception
only occurs when I have somemail merge fields
in my document. Once I removed them theException
does not come up anymore, but the converted
我曾考虑过使用另一种方式来生成PDF,但是我只能找到4种方式:
I thought about using another way to generate the PDF, but I could only find 4 ways:
- 使用 OpenOffice -无法执行,因为我无法在服务器上安装任何软件.同样遵循上述方式 Strato )使用SunOS作为操作系统,这需要Linux
- 使用 phpdocx -我没有任何预算可以支付,并且演示无法创建PDF
- 使用 PHPLiveDocx -此方法有效,但每个文档的限制为250个文档每天每小时20个,我必须一次转换arround 300个文档,甚至一天可能要转换多次
- 使用 PHP-Digital-Format-Convert -输出看起来比使用
PHPWord
和tcpdf
更好,但是由于缺少图像以及大多数(不是全部!)样式,仍然无法使用
- Using OpenOffice - Impossible as I cannot install any software on the Server. Also going the way mentioned here did not work either as my hoster (Strato) uses SunOS as the OS and this needs Linux
- Using phpdocx - I do not have any budget to pay for it and the demo cannot create PDF
- Using PHPLiveDocx - This works, but has the limitation of 250 documents per day and 20 per hour and I have to convert arround 300 documents at once, maybe even multiple times a day
- Using PHP-Digital-Format-Convert - The output looks better than with
PHPWord
andtcpdf
, but still not usable as images are missing, and most (not all!) of the styles
是否有生成PDF的第五种方法?还是有什么解决方法可以使生成的PDF文档看起来不错?
Is there a 5th way to generate the PDF? Or is there any solution to make the generated PDF documents look nice?
推荐答案
您要先取消链接PDF文件,然后再保存它,而且还必须取消DOCX文档而不是PDF文件的链接.
You're trying to unlink the PDF file before saving it, and you have also to unlink the DOCX document, not the PDF one.
尝试一下.
$pdfWriter = \PhpOffice\PhpWord\IOFactory::createWriter($wordPdf , 'PDF');
$pdfWriter->save($filename.".pdf");
unlink($wordPdf);
这篇关于从PHPWord生成的.docx生成PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!