1

当我生成一个用 HTML 替换变量的 Word 文档时,生成的报告显示错误并且无法打开。

$datenarray = "<div class='form-group col-sm-12 subquestion'  id='sadasd'><label for='sadasd' class='col-sm-3 dasda'>saasdasd</label><div class='col-sm-6'><div class='input-group' data_id=asdasd>fasfgsgfjhagfj</div>";

$file_name = "BSB";
$template_file = "../../Documentation/Templates/example.docx";
$fileName = 'temp' . '//'  . $code . " . mt_rand(10,99) . ".docx";
$fullpath = $FileServer . $fileName;

try
{
    copy($template_file, $fullpath);

    $zip = new ZipArchive;

    if($zip->open($fullpath) == true)
    {
        $key_file_name = 'word/document.xml';
        $message = $zip->getFromName($key_file_name);
        
        $message = str_ireplace("test1",    $test1,                     $message);
        $message = str_ireplace("test2",    $test2,                     $message);
        $message = str_ireplace("test3",    $test3,                     $message);
        $message = str_ireplace("test4",    $test4,                     $message);
        $message = str_ireplace("test5",    $test5,                     $message);
        $message = str_ireplace("test6",    $test6,                     $message);
        $message = str_ireplace("test7",    $test7,                     $message);
        $message = str_ireplace("test8",    $test8,                     $message);
        $message = str_ireplace("test9",    $test9,                     $message);
        $message = str_ireplace("test10",   $datenarray,                $message);
        
        $message = str_ireplace("&",        "&amp;",    $message);
        
        //Replace the content with the new content created above.
        $zip->addFromString($key_file_name, $message);
        $zip->close();
            
        //That's it, you have the new Word document in the Result Directory with placeholders replaced with new content
    }
    echo $FileServer_http . $fileName;
}
catch (Exception $e) {
    $error_message =  "Error creating the Word Document";
    echo $e;
}

这里 $datenarray 是唯一的 html 变量,如上面的示例代码所示,所有其他变量都是字符串替换。请帮我解决问题。在此处输入图像描述

4

0 回答 0