PhpWord 不会用 TemplateProcessor 替换文本

我在这里遇到了与@pindol相同的问题。


我在页面问题中遵循了用户提供的一些步骤。因此,我测试了 TemplateProcessor 中的函数以查看输出,最后,使用 var_dump 测试了 $TemplateProcessor 漏洞:


object(PhpOffice\PhpWord\TemplateProcessor)#1259 (9) { ["zipClass":protected]=> object(PhpOffice\PhpWord\Shared\ZipArchive)#1008 (5) { ["numFiles"]=> int(11) ["filename"]=> string(18) "/tmp/PhpWord5qX68E" ["tempDir":"PhpOffice\PhpWord\Shared\ZipArchive":private]=> string(4) "/tmp" ["zip":"PhpOffice\PhpWord\Shared\ZipArchive":private]=> object(PclZip)#1046 (5) { ["zipname"]=> string(18) "/tmp/PhpWord5qX68E" ["zip_fd"]=> int(0) ["error_code"]=> int(0) ["error_string"]=> string(0) "" ["magic_quotes_status"]=> int(-1) } ["usePclzip":"PhpOffice\PhpWord\Shared\ZipArchive":private]=> bool(true) } ["tempDocumentFilename":protected]=> string(18) "/tmp/PhpWord5qX68E" ["tempDocumentMainPart":protected]=> string(2690) " Test : the test is perfect" ["tempDocumentSettingsPart":protected]=> string(2842) " " ["tempDocumentHeaders":protected]=> array(0) { } ["tempDocumentFooters":protected]=> array(0) { } ["tempDocumentRelations":protected]=> array(1) { ["word/document.xml"]=> string(817) " " } ["tempDocumentContentTypes":protected]=> string(1312) " " ["tempDocumentNewImages":protected]=> array(0) { } } 

正如我们在 ["tempDocumentMainPart":protected] 中看到的,变量已成功替换为以下函数:


    $templateProcessor = new TemplateProcessor('results/test.docx');

    $templateProcessor->setValue('test', 'the test is perfect'); 

    var_dump($templateProcessor);

    exit();

    $templateProcessor->saveAs('results/results.docx');

    return response()->download('results/results.docx');

但是当我尝试另存为或下载文件时,下载的文件中的变量没有更改。它与提供的模板相同,当我尝试打开它时收到一条错误消息(下载后):


Word found unreadable content in results.docx. Do you want to recover the contents of this document? If the source for this document is reliable, click Yes.

我在 PhpWord 0.16.0 和 0.17.0 中对此进行了测试,并将其与 laravel 一起使用(我不认为问题出在 laravel 上)。PHP版本:

PHP 7.4.7 (cli) (built: Jun 12 2020 07:48:26) ( NTS )

我使用 PCLZIP 而不是 zipArchive,因为它会产生内存问题。提前致谢


婷婷同学_
浏览 643回答 1
1回答

慕田峪7331174

我遇到了同样的问题并设法解决了它!我使用 Apache2 的发布版本在 MacOS 上工作,我不知道您是否仍然遇到这个问题并且与我处于相同的环境中,但无论如何我都会发布以防其他人遇到同样的问题。脚步 :1.下载php@8.0要下载 php@8.0,我运行了以下命令(源代码):brew tap shivammathur/php brew install shivammathur/php/php@8.0brew link --overwrite --force php@8.0不是 php@8.1,此版本(截至 2022 年 2 月)会导致显示 DEPRECATED 消息GitHub Issue2.更新Apache2配置然后我/etc/apache2/httpd.conf根据brew安装程序消息更新了我的文件(在编辑配置文件之前务必备份它们!)To enable PHP in Apache add the following to httpd.conf and restart Apache:     LoadModule php_module /usr/local/opt/php/lib/httpd/modules/libphp.so     <FilesMatch \.php$>         SetHandler application/x-httpd-php     </FilesMatch> Finally, check DirectoryIndex includes index.php     DirectoryIndex index.php index.html我将 LoadModule 路径更改为,/usr/local/opt/php@8.0/lib/httpd/modules/libphp.so因为我下载了多个版本的 php然后使用以下命令重新启动 Apache2:sudo apachectl restart3.使用ZipArchive删除Settings::setZipClass(Settings::PCLZIP);并使用 ZipArchive。我不能保证您使用 ZipArchive 的问题会使用 php@8.0 消失,因为您似乎遇到了与我不同的问题,那就是在我的系统上根本找不到 ZipArchive。结论 :我的猜测是,问题在于Settings::setZipClass(Settings::PCLZIP)无法创建正常的 OOXML(但仅在出于某种原因使用 TemplateProcessor 时?因为它在使用 创建新文档时工作得很好new PhpWord())
打开App,查看更多内容
随时随地看视频慕课网APP