PhpWord не заменяет текст с помощью TemplateProcessor

Я столкнулся с той же проблемой, что и @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.

Функция save и saveAs в phpword:

    /**
 * 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 7.4.7 (cli) (built: Jun 12 2020 07:48:26) ( NTS )

Я использую PCLZIP вместо zipArchive, потому что он генерирует проблема с памятью. заранее спасибо


person Moho    schedule 23.06.2020    source источник