代码之家  ›  专栏  ›  技术社区  ›  Alexander Fuchs

PHPWord文件损坏?

  •  4
  • Alexander Fuchs  · 技术社区  · 10 年前

    我的基本PHPWord设置正在运行。

    这是我的代码:

        <?php
      require_once 'PhpWord/Autoloader.php';
        \PhpOffice\PhpWord\Autoloader::register();
    
    
    
        function getEndingNotes($writers)
    {
        $result = '';
        // Do not show execution time for index
        if (!IS_INDEX) {
            $result .= date('H:i:s') . " Done writing file(s)" . EOL;
            $result .= date('H:i:s') . " Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MB" . EOL;
        }
        // Return
        if (CLI) {
            $result .= 'The results are stored in the "results" subdirectory.' . EOL;
        } else {
            if (!IS_INDEX) {
                $types = array_values($writers);
                $result .= '<p>&nbsp;</p>';
                $result .= '<p>Results: ';
                foreach ($types as $type) {
                    if (!is_null($type)) {
                        $resultFile = 'results/' . SCRIPT_FILENAME . '.' . $type;
                        if (file_exists($resultFile)) {
                            $result .= "<a href='{$resultFile}' class='btn btn-primary'>{$type}</a> ";
                        }
                    }
                }
                $result .= '</p>';
            }
        }
        return $result;
    }
    // Template processor instance creation
    $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('template.docx');
    
    
    // Variables on different parts of document
    //$templateProcessor->setValue('vorname', htmlspecialchars('John')); // On section/content
    //$templateProcessor->setValue('nachname', htmlspecialchars('Doe')); // On footer
    //$templateProcessor->setValue('funktion', htmlspecialchars('Manager'));
    
    
    // Simple table
    $templateProcessor->cloneRow('rowValue', 10);
    
    //clone our things
    // Will clone everything between ${tag} and ${/tag}, the number of times. By default, 1.
    $templateProcessor->cloneBlock('CLONEME', 5);
    
    
    //delete things
    // Everything between ${tag} and ${/tag}, will be deleted/erased.
    $templateProcessor->deleteBlock('DELETEME');
    
    
    
    // Saving the document as OOXML file...
    $temp_file = tempnam(sys_get_temp_dir(), 'PHPWord');
    ob_clean();
    $templateProcessor->saveAs($temp_file);
    getEndingNotes(array('Word2007' => 'docx'));
    
    
    header("Content-Disposition: attachment; filename='cv.docx'");
    readfile($temp_file); // or echo file_get_contents($temp_file);
    unlink($temp_file);  // remove temp file
    ?>
    

    它很适合这个词 file .

    然而,当我更改单词文件中的某些内容时,PHPWord会传递一个损坏的文件。它与XML错误有关。我的问题是,如何编辑我的word文件并获得一个完美的工作文件而不出错? 是否有修复XML的工具?

    2 回复  |  直到 10 年前
        1
  •  6
  •   Spookytheboy    7 年前

    我也遇到了同样的问题,这是谷歌搜索的第一次回应。

    我发现,使用“deleteBlock()”函数删除不需要的部分会对模板造成一些影响,使其无法使用MS Word/Google Docs打开。我可以用Mac Pages很好地打开,但由于某种原因,deleteBlock()函数在导出时做了一些奇怪的事情。

    我的编辑不是使用deleteBlock(),而是:

    $templateProcessor->cloneBlock('HOBBYBLOCK', 0);
    

    (“爱好”只是我在逐案出口中避免的部分的名称)

    1. 有效地移除{}块包装器和
    2. 将内部变量/注入点设置为零。

    这似乎解决了我的问题。请提醒未来发现此问题并需要帮助排除故障的任何人。:^}

        2
  •  1
  •   Alexander Fuchs    10 年前

    我找到了一个答案,在编辑word文件时word在单词之间插入了不同的xml元素。我必须在编辑器中手动编辑文件,以确保替换值不被标记分隔。