1

When I generate an Word document replacing a variable with a HTML the generated report is showing an error and doesn't open.

$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;
}

Here $datenarray is the only html variable as shown in the example code above all others are string replacements. Please help me with the solution.enter image description here

Rohit Gowda
  • 41
  • 13
  • What gave you the impression that this could work? Word files are OOXML, not HTML. They're only superficially similar formats. – John Korchok Sep 26 '20 at 15:47
  • This is my first time interacting with word document, can you please give me an idea of how to achieve it – Rohit Gowda Sep 26 '20 at 16:06
  • I'm not too familiar with phpdocx, but here's a page that shows how to incorporate an html snippet using a call to _embedHTML_. This probably either does the translation for you, or wraps the HTML in XML that Word can understand: https://www.phpdocx.com/documentation/snippets/add-footnote-block-content-inline-html-extended – John Korchok Sep 27 '20 at 00:31

0 Answers0