【问题标题】:Convert doc or docx to pdf use tcpdf not matched将 doc 或 docx 转换为 pdf 使用 tcpdf 不匹配
【发布时间】:2022-01-27 03:00:35
【问题描述】:

我试图将文件 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 文件的一部分

我的代码有什么问题?

【问题讨论】:

    标签: php pdf tcpdf phpword phpoffice


    【解决方案1】:

    不幸的是,phpWord 非常基础,因此对于 DocX 到 PDF 的输出,您可以看到无法保留文本或分页符,也不支持列表或导出图像。

    有关当前功能列表,请参阅

    https://phpword.readthedocs.io/en/latest/intro.html#writers

    由于它运行 OpenOffice 作为转换器,您可以尝试其他 PHP 方法来直接运行转换

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多