从PHPWord生成的.docx生成PDF [英] Generate PDF from .docx generated by PHPWord

查看:1314
本文介绍了从PHPWord生成的.docx生成PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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.

首先,我尝试将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时,在加载文件时出现以下异常

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不再出现,但是转换后的PDF文件看起来很可怕.所有样式信息都消失了,我无法使用结果,因此仍然需要其他选择.

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种方式:

I thought about using another way to generate the PDF, but I could only find 4 ways:

  1. 使用 OpenOffice -无法执行,因为我无法在服务器上安装任何软件.同样遵循上述方式 Strato )使用SunOS作为操作系统,这需要Linux
  2. 使用 phpdocx -我没有任何预算可以支付,并且演示无法创建PDF
  3. 使用 PHPLiveDocx -此方法有效,但每个文档的限制为250个文档每天每小时20个,我必须一次转换arround 300个文档,甚至一天可能要转换多次
  4. 使用 PHP-Digital-Format-Convert -输出看起来比使用PHPWordtcpdf更好,但是由于缺少图像以及大多数(不是全部!)样式,仍然无法使用
  1. 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
  2. Using phpdocx - I do not have any budget to pay for it and the demo cannot create PDF
  3. 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
  4. Using PHP-Digital-Format-Convert - The output looks better than with PHPWord and tcpdf, 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆