Alexander Fuchs
Alexander Fuchs

Reputation: 1358

PHPWord corrupted file?

My basic PHPWord setup is working.

This is my code:

    <?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
?>

it works well for this Word file.

However when I change something in my word file PHPWord delivers a corupted file. It has something to do with XML Errors. My question is, how can I edit my word file and get a perfectly working file without errors? Is there a tool to fix XML?

Upvotes: 4

Views: 4994

Answers (2)

Spookytheboy
Spookytheboy

Reputation: 238

I'm having the same issue and this is the first response through a Google search.

I've discovered that using a "deleteBlock()" function to remove an unneeded section will do something to the template that makes it unable to be opened with MS Word / Google Docs. I'm able to open with Mac Pages just fine, but for some reason the deleteBlock() function is doing something weird with the export.

My edit was instead of using a deleteBlock(), I did:

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

("Hobbies" was just the name of the section I was avoiding on case by case exports)

  1. Effectively removing the {} block wrappers and
  2. Setting the internal variable / injection point to nothing.

This seemed to resolve my issue. Just a heads up for anyone in the future who finds this and needs help troubleshooting. :^}

Upvotes: 6

Alexander Fuchs
Alexander Fuchs

Reputation: 1358

I found an answer, while editing the word file word inserts different xml elements between words. I had to edit the file manually in an editor making sure the replace values were not seperated by tags.

Upvotes: 1

Related Questions