PhpWord 不会用 TemplateProcessor 替换文本

PhpWord doesn't replace text with TemplateProcessor

我在 here.

中遇到了与@pindol 相同的问题

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

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中的save和saveAs函数:

    /**
 * Saves the result document.
 *
 * @throws \PhpOffice\PhpWord\Exception\Exception
 *
 * @return string
 */
public function save()
{
    foreach ($this->tempDocumentHeaders as $index => $xml) {
        $this->savePartWithRels($this->getHeaderName($index), $xml);
    }

    $this->savePartWithRels($this->getMainPartName(), $this->tempDocumentMainPart);

    foreach ($this->tempDocumentFooters as $index => $xml) {
        $this->savePartWithRels($this->getFooterName($index), $xml);
    }

    $this->zipClass->addFromString($this->getDocumentContentTypesName(), $this->tempDocumentContentTypes);

    // Close zip file
    if (false === $this->zipClass->close()) {
        throw new Exception('Could not close zip file.'); // @codeCoverageIgnore
    }

    return $this->tempDocumentFilename;
}

/**
 * @param string $fileName
 * @param string $xml
 */
protected function savePartWithRels($fileName, $xml)
{
    $this->zipClass->addFromString($fileName, $xml);
    if (isset($this->tempDocumentRelations[$fileName])) {
        $relsFileName = $this->getRelationsName($fileName);
        $this->zipClass->addFromString($relsFileName, $this->tempDocumentRelations[$fileName]);
    }
}

/**
 * Saves the result document to the user defined file.
 *
 * @since 0.8.0
 *
 * @param string $fileName
 */
public function saveAs($fileName)
{
    $tempFileName = $this->save();

    if (file_exists($fileName)) {
        unlink($fileName);
    }

    /*
     * Note: we do not use `rename` function here, because it loses file ownership data on Windows platform.
     * As a result, user cannot open the file directly getting "Access denied" message.
     *
     * @see https://github.com/PHPOffice/PHPWord/issues/532
     */
    copy($tempFileName, $fileName);
    unlink($tempFileName);
}

我在 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,因为他生成了 memory issue。 提前致谢

我 运行 遇到了同样的问题并设法解决了!

我在 MacOS 上工作,使用的是 Apache2 的发布版本,我不知道你是否仍然遇到这个问题并且在与我相同的环境中,但无论如何我都会发帖以防其他人遇到同样的问题.

步骤:

1。下载 php@8.0

要下载 php@8.0,我 运行 以下命令 (source):

brew tap shivammathur/php
brew install shivammathur/php/php@8.0
brew link --overwrite --force php@8.0

Not php@8.1, this version (as of february 2022) causes DEPRECATED messages to show up GitHub Issue

2。更新 Apache2 配置

然后我根据 brew 安装程序消息更新了我的 /etc/apache2/httpd.conf 文件

在编辑配置文件之前一定要备份它们!

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

I changed the LoadModule path to /usr/local/opt/php@8.0/lib/httpd/modules/libphp.so as I had multiple versions of php downloaded

然后使用 :

重新启动 Apache2
sudo apachectl restart

3。使用 ZipArchive

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

I can't assure that your problems using ZipArchive will disappear using php@8.0 as you seem to be having an other problem than I was, which was that ZipArchive was simply not found on my system.

结论:

我的猜测是问题来自 Settings::setZipClass(Settings::PCLZIP) 无法创建正常的 OOXML(但仅在出于某种原因使用 TemplateProcessor 时才出现?因为它在使用 new PhpWord() 创建新文档时运行良好)