gpt4 book ai didi

PHPWord 损坏的文件?

转载 作者:可可西里 更新时间:2023-11-01 01:00:28 24 4
gpt4 key购买 nike

我的基本 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 .

但是,当我更改我的 word 文件中的某些内容时,PHPWord 会提供损坏的文件。它与 XML 错误有关。我的问题是,我怎样才能编辑我的word文件并获得一个完美无误的工作文件?有修复 XML 的工具吗?

最佳答案

我遇到了同样的问题,这是通过 Google 搜索得到的第一个响应。

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

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

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

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

  1. 有效移除 {} block 包装器和
  2. 将内部变量/注入(inject)点设置为空。

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

关于PHPWord 损坏的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28651655/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com