Forum


Replies: 1   Views: 2046
Ms word showing document currupted in phpdocx v8.5
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by kompanions  · 15-07-2019 - 14:26

Hii,

We are using phpDocx version 8.5. we are generating dynamic word documents included text images and Mathml equations. documnetis generated successfully but when opens in MS Word it gives ducument is corrupted.

we want to add equation in table cells. document is opens well in Libreoffice in ubuntu 16.04 but not in MS Word in Windows nor in mac nor in android and other devices.  We need this urgent please help.

below is the code attached. please check.
 

function index()
    {
        $docx    = $this->docx;

        $html    = "";

        $main    = "";

        $records = $this->phpword_model->selectall();
        
        $docx->addHeading('First level title', 0);
        
        foreach ($records as $record)
        {
            $distractors = $this->phpword_model->getdistractors($record->id);

            if (count($distractors) > 0)
            {
                $optionA = array(

                    'attributeid' => '',

                    'title' => '',

                    'disttitle' => ''

                );

                $optionB = array(

                    'attributeid' => '',

                    'title' => '',

                    'disttitle' => ''

                );

                $optionC = array(

                    'attributeid' => '',

                    'title' => '',

                    'disttitle' => ''

                );

                $optionD = array(

                    'attributeid' => '',

                    'title' => '',

                    'disttitle' => ''

                );

                $correct = '';

                if (isset($distractors[0]))
                {

                    $optionA = array(

                        'attributeid' => $distractors[0]['attributeid'],

                        'title' => $distractors[0]['title'],

                        'disttitle' => $distractors[0]['disttitle']

                    );

                    if ($optionA['attributeid'] == 0)
                    {

                        $correct = 'A';

                    }

                }

                if (isset($distractors[1]))
                {

                    $optionB = array(

                        'attributeid' => $distractors[1]['attributeid'],

                        'title' => $distractors[1]['title'],

                        'disttitle' => $distractors[1]['disttitle']

                    );

                    if ($optionB['attributeid'] == 0)
                    {

                        $correct = 'B';

                    }

                }

                if (isset($distractors[2]))
                {
                    $optionC = array(

                        'attributeid' => $distractors[2]['attributeid'],

                        'title' => $distractors[2]['title'],

                        'disttitle' => $distractors[2]['disttitle']

                    );

                    if ($optionC['attributeid'] == 0)
                    {
                        $correct = 'C';
                    }

                }

                if (isset($distractors[3]))
                {
                    $optionD = array(

                        'attributeid' => $distractors[3]['attributeid'],

                        'title' => $distractors[3]['title'],

                        'disttitle' => $distractors[3]['disttitle']

                    );

                    if ($optionD['attributeid'] == 0)
                    {
                        $correct = 'D';
                    }

                }

            }

            $questem           = str_replace(' ', ' ', $record->questem);

            $question_solution = str_replace(' ', ' ', $record->question_solution);

            $optionAtitle      = str_replace(' ', ' ', $optionA['title']);

            $optionBtitle      = str_replace(' ', ' ', $optionB['title']);

            $optionCtitle      = str_replace(' ', ' ', $optionC['title']);

            $optionDtitle      = str_replace(' ', ' ', $optionD['title']);

            $questem1          = new WordFragment($docx);

            if ($questem != "")
            {

                if (strpos($questem, 'MathML') !== false)
                {

                    $questem = str_replace('<math xmlns="http://www.w3.org/1998/Math/MathML">', 'xxxx<math xmlns="http://www.w3.org/1998/Math/MathML">', $questem);

                    $questem = str_replace('</math>', '</math>xxxx', $questem);

                    $data    = explode("xxxx", $questem);

                    $arr     = array();

                    for ($i = 0; $i < count($data); $i++)
                    {

                        if (strpos($data[$i], 'MathML') !== false)
                        {

                            $questem1->addMathEquation($data[$i], 'mathml');

                        }
                        else
                        {
                            $questem1->addText(strip_tags($data[$i]));
                        }

                    }

                }
                else
                {
                    $questem1->addText(strip_tags($questem));
                }

            }

            $optionAtitle1 = new WordFragment($docx);

            if ($optionAtitle != "")
            {

                if (strpos($optionAtitle, 'MathML') !== false)
                {

                    $optionAtitle = str_replace('<math xmlns="http://www.w3.org/1998/Math/MathML">', 'xxxx<math xmlns="http://www.w3.org/1998/Math/MathML">', $optionAtitle);

                    $optionAtitle = str_replace('</math>', '</math>xxxx', $optionAtitle);

                    $data         = explode("xxxx", $optionAtitle);

                    for ($i = 0; $i < count($data); $i++)
                    {

                        if (strpos($data[$i], 'MathML') !== false)
                        {
                            $optionAtitle1->addMathEquation($data[$i], 'mathml');
                        }
                        else
                        {
                            $optionAtitle1->addText(strip_tags($data[$i]));
                        }

                    }

                }
                else
                {
                    $optionAtitle1->addText(strip_tags($optionAtitle));
                }

            }

            $optionBtitle1 = new WordFragment($docx);

            if ($optionBtitle != "")
            {

                if (strpos($optionBtitle, 'MathML') !== false)
                {

                    $optionBtitle = str_replace('<math xmlns="http://www.w3.org/1998/Math/MathML">', 'xxxx<math xmlns="http://www.w3.org/1998/Math/MathML">', $optionBtitle);

                    $optionBtitle = str_replace('</math>', '</math>xxxx', $optionBtitle);

                    $data         = explode("xxxx", $optionBtitle);

                    for ($i = 0; $i < count($data); $i++)
                    {

                        if (strpos($data[$i], 'MathML') !== false)
                        {
                            $optionBtitle1->addMathEquation($data[$i], 'mathml');
                        }
                        else
                        {
                            $optionBtitle1->addText(strip_tags($data[$i]));
                        }

                    }

                }
                else
                {
                    $optionBtitle1->addText(strip_tags($optionBtitle));
                }

            }

            $optionCtitle1 = new WordFragment($docx);

            if ($optionCtitle != "")
            {

                if (strpos($optionCtitle, 'MathML') !== false)
                {

                    $optionCtitle = str_replace('<math xmlns="http://www.w3.org/1998/Math/MathML">', 'xxxx<math xmlns="http://www.w3.org/1998/Math/MathML">', $optionCtitle);

                    $optionCtitle = str_replace('</math>', '</math>xxxx', $optionCtitle);

                    $data         = explode("xxxx", $optionCtitle);

                    for ($i = 0; $i < count($data); $i++)
                    {

                        if (strpos($data[$i], 'MathML') !== false)
                        {
                            $optionCtitle1->addMathEquation($data[$i], 'mathml');
                        }
                        else
                        {
                            $optionCtitle1->addText(strip_tags($data[$i]));
                        }

                    }

                }
                else
                {
                    $optionCtitle1->addText(strip_tags($optionCtitle));
                }

            }

            $optionDtitle1 = new WordFragment($docx);

            if ($optionDtitle != "")
            {

                if (strpos($optionDtitle, 'MathML') !== false)
                {

                    $optionDtitle = str_replace('<math xmlns="http://www.w3.org/1998/Math/MathML">', 'xxxx<math xmlns="http://www.w3.org/1998/Math/MathML">', $optionDtitle);

                    $optionDtitle = str_replace('</math>', '</math>xxxx', $optionDtitle);

                    $data         = explode("xxxx", $optionDtitle);

                    for ($i = 0; $i < count($data); $i++)
                    {

                        if (strpos($data[$i], 'MathML') !== false)
                        {
                            $optionDtitle1->addMathEquation($data[$i], 'mathml');
                        }
                        else
                        {
                            $optionDtitle1->addText(strip_tags($data[$i]));
                        }

                    }

                }
                else
                {
                    $optionDtitle1->addText(strip_tags($optionDtitle));
                }

            }

            $question_solution1 = new WordFragment($docx);

            if ($question_solution != "")
            {

                if (strpos($question_solution, 'MathML') !== false)
                {

                    $question_solution = str_replace('<math xmlns="http://www.w3.org/1998/Math/MathML">', 'xxxx<math xmlns="http://www.w3.org/1998/Math/MathML">', $question_solution);

                    $question_solution = str_replace('</math>', '</math>xxxx', $question_solution);
                    
                    $data              = explode("xxxx", $question_solution);
                    
                    for ($i = 0; $i < count($data); $i++)
                    {
                    
                        if (strpos($data[$i], 'MathML') !== false)
                        {
                    
                            $question_solution1->addMathEquation($data[$i], 'mathml');
                    
                        }
                    
                        else
                        {
                    
                            $question_solution1->addText(strip_tags($data[$i]));
                    
                        }
                    
                    }
                }
                
                else
                {
                    $question_solution1->addText(strip_tags($question_solution));
                }
                
            }
            
            $valuesTable = array(
            
                array(
                
                    'Question',
                    
                    $questem1
                
                ),
                
                array(
                
                    'Option A',
                
                    $optionAtitle1
                
                ),
                
                array(
                
                    'Option B',
                
                    $optionBtitle1
                
                ),
                
                array(
                
                    'Option C',
                
                    $optionCtitle1
                
                ),
                
                array(
                
                    'Option D',
                
                    $optionDtitle1
                
                ),
                
                array(
                
                    'Cognitive Level',
                
                    $record->bloomlvltitle
                
                ),
                
                array(
                
                    'Solution',
                
                    $question_solution1
                
                )
            
            );
            
            $paramsTable = array(
            
                'tableAlign' => 'left',
            
                'columnWidths' => array(
            
                    1000,
            
                    2500,
            
                    3000
            
                )
            
            );
            
            $docx->addTable($valuesTable, $paramsTable);
            
        }
        
        $rand = rand();
        
        $docx->createDocxAndDownload($rand . '_qdocx');
    }