我的基本 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> </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
?>
它适用于这个 Word文件。
但是,当我更改 word 文件中的某些内容时,PHPWord 会提供损坏的文件。它与 XML 错误有关。我的问题是,我怎样才能编辑我的 word 文件并获得一个没有错误的完美工作文件?有修复 XML 的工具吗?