本文介绍了从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,但是我只能找到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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 07:48
查看更多