亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PhpWord 不會用 TemplateProcessor 替換文本

PhpWord 不會用 TemplateProcessor 替換文本

PHP
婷婷同學_ 2023-06-24 17:30:03
我在這里遇到了與@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,因為它會產生內存問題。提前致謝
查看完整描述

1 回答

?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

我遇到了同樣的問題并設法解決了它!

我使用 Apache2 的發布版本在 MacOS 上工作,我不知道您是否仍然遇到這個問題并且與我處于相同的環境中,但無論如何我都會發布以防其他人遇到同樣的問題。

腳步 :

1.下載[email protected]

要下載 [email protected],我運行了以下命令(源代碼):

brew?tap?shivammathur/php
brew?install?shivammathur/php/[email protected][email protected]

不是 [email protected],此版本(截至 2022 年 2 月)會導致顯示 DEPRECATED 消息GitHub Issue

2.更新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/[email protected]/lib/httpd/modules/libphp.so因為我下載了多個版本的 php

然后使用以下命令重新啟動 Apache2:

sudo?apachectl?restart

3.使用ZipArchive

刪除Settings::setZipClass(Settings::PCLZIP);并使用 ZipArchive。

我不能保證您使用 ZipArchive 的問題會使用 [email protected] 消失,因為您似乎遇到了與我不同的問題,那就是在我的系統上根本找不到 ZipArchive。

結論 :

我的猜測是,問題在于Settings::setZipClass(Settings::PCLZIP)無法創建正常的 OOXML(但僅在出于某種原因使用 TemplateProcessor 時?因為它在使用 創建新文檔時工作得很好new PhpWord()


查看完整回答
反對 回復 2023-06-24
  • 1 回答
  • 0 關注
  • 560 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號