phpword页眉替换修改

<?php

/**
 * PHPWord
 *
 * Copyright (c) 2011 PHPWord
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category   PHPWord
 * @package    PHPWord
 * @copyright  Copyright (c) 010 PHPWord
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
 * @version    Beta 0.6.3, 08.07.2011
 */

/**
 * PHPWord_DocumentProperties
 *
 * @category   PHPWord
 * @package    PHPWord
 * @copyright  Copyright (c) 2009 - 2011 PHPWord (http://www.codeplex.com/PHPWord)
 */
class PHPWord_Template {

    /**
     * ZipArchive
     *
     * @var ZipArchive
     */
    private $_objZip;

    /**
     * Temporary Filename
     *
     * @var string
     */
    private $_tempFileName;

    /**
     * Document XML
     *
     * @var string
     */
    private $_documentXML;
    private $tempDocumentHeaders = array();
    private $tempDocumentFooters = array();
    private $tempDocumentMainPart;
    protected $_path;

    /**
     * Create a new Template Object
     *
     * @param string $strFilename
     */
    public function __construct($strFilename, $fileName) {
        $this->_path = dirname($strFilename);
        $this->_tempFileName = $this->_path . DIRECTORY_SEPARATOR . $fileName . '.docx';

        copy($strFilename, $this->_tempFileName); // Copy the source File to the temp File
        $this->_objZip = new ZipArchive();
        $this->_objZip->open($this->_tempFileName);
        $index = 1;
        while (false !== $this->_objZip->locateName($this->getHeaderName($index))) {
            $this->tempDocumentHeaders[$index] = $this->fixBrokenMacros(
                    $this->_objZip->getFromName($this->getHeaderName($index))
            );
            $index++;
        }
        $index = 1;
        while (false !== $this->_objZip->locateName($this->getFooterName($index))) {
            $this->tempDocumentFooters[$index] = $this->fixBrokenMacros(
                    $this->_objZip->getFromName($this->getFooterName($index))
            );
            $index++;
        }
        $this->_documentXML = $this->fixBrokenMacros($this->_objZip->getFromName($this->getMainPartName()));
    }

    /**
     * Finds parts of broken macros and sticks them together.
     * Macros, while being edited, could be implicitly broken by some of the word processors.
     *
     * @param string $documentPart The document part in XML representation.
     *
     * @return string
     */
    protected function fixBrokenMacros($documentPart) {
        $fixedDocumentPart = $documentPart;
//        pp($fixedDocumentPart);
        $fixedDocumentPart = preg_replace_callback(
                '|\$[^{]*\{[^}]*\}|U', function ($match) {
            return strip_tags($match[0]);
        }, $fixedDocumentPart
        );

        return $fixedDocumentPart;
    }

    /**
     * @return string
     */
    protected function getMainPartName() {
        return 'word/document.xml';
    }

    /**
     * Get the name of the footer file for $index.
     *
     * @param integer $index
     *
     * @return string
     */
    protected function getFooterName($index) {
        return sprintf('word/footer%d.xml', $index);
    }

    /**
     * Get the name of the header file for $index.
     *
     * @param integer $index
     *
     * @return string
     */
    protected function getHeaderName($index) {
        return sprintf('word/header%d.xml', $index);
    }

    /**
     * Set a Template value
     *
     * @param mixed $search
     * @param mixed $replace
     */
    public function setValue($search, $replace) {
        if (substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') {
            $search = '${' . $search . '}';
        }

//        if(!is_array($replace)) {
//            $replace = utf8_encode($replace);
//        }

        $this->_documentXML = str_replace($search, $replace, $this->_documentXML);
    }

    /**
     * Set a Template header
     *
     * @param mixed $search
     * @param mixed $replace
     */
    public function setHeader($search, $replace) {
        if (substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') {
            $search = '${' . $search . '}';
        }
        foreach ($this->tempDocumentHeaders as $index => $xml) {
            $this->tempDocumentHeaders[$index] = str_replace($search, $replace, $xml);
        }
    }

    /**
     * Replacement Download
     *
     * @return string
     *
     */
    public function saveData($strFilename) {
        if (file_exists($this->_path . DIRECTORY_SEPARATOR . $strFilename)) {
            unlink($this->_path . DIRECTORY_SEPARATOR . $strFilename);
        }
        foreach ($this->tempDocumentHeaders as $index => $xml) {
            $this->_objZip->addFromString($this->getHeaderName($index), $xml);
        }

        $this->_objZip->addFromString($this->getMainPartName(), $this->_documentXML);
        foreach ($this->tempDocumentFooters as $index => $xml) {
            $this->_objZip->addFromString($this->getFooterName($index), $xml);
        }
        if ($this->_objZip->close() === false) {
            throw new Exception('Could not close zip file.');
        }
        redirect('/' . $this->_tempFileName);
    }

}

?>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值