Problem saving edited zip archive (docx)

后端 未结 3 1566
温柔的废话
温柔的废话 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: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();
    
        }
    }
    

提交回复
热议问题