Problem saving edited zip archive (docx)

后端 未结 3 1569
温柔的废话
温柔的废话 2021-01-24 18:14

So here is my code:

open(\'test.docx\') === TRUE) {

 $xmlString = $zip->getFromName(\'word/document.xml\');
 $x         


        
相关标签:
3条回答
  • 2021-01-24 18:50

    If close() returns false, there was an error writing out the archive.

    Use getStatusString to get the exact error message.

    0 讨论(0)
  • 2021-01-24 18:51

    Ok, I have used a class I found at phpclasses:

    http://phpclasses.web4u.cz/package/6278-PHP-Edit-a-Zip-archive-in-pure-PHP-no-temporary-files.html

    Here is the working code:

    private function GenerateDocx($theTemplate, array $theReplacemenArray, $theOutputFile)
    {
        $aSearchArray = array();
        foreach(range('A','Z') as $aLetter) {
            $aSearchArray[] = str_repeat($aLetter, 5);
        }
        $aArrayCountDifference = count($aSearchArray) - count($theReplacemenArray);
        $aSearchArray = array_slice($aSearchArray, 0, -$aArrayCountDifference);     
    
        require_once('tbszip.php');
        $tbszip = new clsTbsZip();
        $tbszip->Open($theTemplate);
    
        $aXmlPath = 'word/document.xml';
    
        if (true === $tbszip->FileExists($aXmlPath)) {
    
            $aXmlString = $tbszip->FileRead($aXmlPath);
    
            $aXmlString = str_replace($aSearchArray, $theReplacemenArray, $aXmlString);
    
            if (1 != $tbszip->FileReplace($aXmlPath, $aXmlString)) {
                throw new Exception('FileReplace() failed.');
            }
    
            $tbszip->Flush(TBSZIP_FILE, $theOutputFile);
    
            $tbszip->Close();
    
        }
    }
    
    0 讨论(0)
  • 2021-01-24 19:01

    Add sleep(1) before $zip->addFromString('word/document.xml', $xmlString);

    It works on my Ubuntu 12.04

    Don't forget to type your variable in same time when you create a docx file, I mean never type FIRST_AND_LAST_NAME and then you add a symbol $ after that. It creates different XML code.

    0 讨论(0)
提交回复
热议问题