toocooltohavefriends

phpword的开源链接在这里:https://github.com/PHPOffice/PHPWord,PHPword是很多服务端技术为php的网站上的word下载的功能支撑技术。

其原理并不难以理解,因为word可以解析xml形式的数据,所以phpword本质是生成一个xml文件。

相关介绍可以参考:https://support.office.com/zh-cn/article/Open-XML-%E6%A0%BC%E5%BC%8F%E5%92%8C%E6%96%87%E4%BB%B6%E6%89%A9%E5%B1%95%E5%90%8D-5200d93c-3449-4380-8e11-31ef14555b18

在phpword中,生成目录只能依靠建立标题,然后对标题进行索引生成可触发链接的目录。所以你只需要调用addTitle函数即可生成目录,网上中文文档中说的addTOC函数本身不会生成目录,其只是修改的目录的格式,例如字号、字体、颜色之类。

以上逻辑在生成单篇word文档时没有任何问题,但是如果你想生成多篇word文档,每篇文档的目录只是当前word的目录,就会发现很严重的目录冗余。经过分析可以看出其冗余内容为把前面生成的word的目录补充。开发人员的思维这里一定想到一个关键字:static。

 

没错。

在其PHPWord_TOC类中,所有的变量都是static。刚才去github看了下这个bug可能已经修改,我使用的文件是这个:

  1 <?php
  2 /**
  3  * PHPWord
  4  *
  5  * Copyright (c) 2011 PHPWord
  6  *
  7  * This library is free software; you can redistribute it and/or
  8  * modify it under the terms of the GNU Lesser General Public
  9  * License as published by the Free Software Foundation; either
 10  * version 2.1 of the License, or (at your option) any later version.
 11  *
 12  * This library is distributed in the hope that it will be useful,
 13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 15  * Lesser General Public License for more details.
 16  *
 17  * You should have received a copy of the GNU Lesser General Public
 18  * License along with this library; if not, write to the Free Software
 19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 20  *
 21  * @category   PHPWord
 22  * @package    PHPWord
 23  * @copyright  Copyright (c) 010 PHPWord
 24  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
 25  * @version    Beta 0.6.3, 08.07.2011
 26  */
 27 
 28 
 29 /**
 30  * PHPWord_TOC
 31  *
 32  * @category   PHPWord
 33  * @package    PHPWord_TOC
 34  * @copyright  Copyright (c) 2011 PHPWord
 35  */
 36 class PHPWord_TOC {
 37     
 38     /**
 39      * Title Elements
 40      *
 41      * @var array
 42      */
 43     private static $_titles = array();
 44     
 45     /**
 46      * TOC Style
 47      *
 48      * @var array
 49      */
 50     private static $_styleTOC;
 51     
 52     /**
 53      * Font Style
 54      *
 55      * @var array
 56      */
 57     private static $_styleFont;
 58     
 59     /**
 60      * Title Anchor
 61      *
 62      * @var array
 63      */
 64     private static $_anchor = 252634154;
 65     
 66     /**
 67      * Title Bookmark
 68      *
 69      * @var array
 70      */
 71     private static $_bookmarkId = 0;
 72     
 73     
 74     /**
 75      * Create a new Table-of-Contents Element
 76      * 
 77      * @param array $styleFont
 78      * @param array $styleTOC
 79      */
 80     public function __construct($styleFont = null, $styleTOC = null) {
 81         self::$_styleTOC = new PHPWord_Style_TOC();
 82         self::$_bookmarkId=0;
 83         self::$_anchor=252634154;
 84         self::$_titles=array();
 85         if(!is_null($styleTOC) && is_array($styleTOC)) {
 86             foreach($styleTOC as $key => $value) {
 87                 if(substr($key, 0, 1) != \'_\') {
 88                     $key = \'_\'.$key;
 89                 }
 90                 self::$_styleTOC->setStyleValue($key, $value);
 91             }
 92         }
 93         
 94         if(!is_null($styleFont)) {
 95             if(is_array($styleFont)) {
 96                 self::$_styleFont = new PHPWord_Style_Font();
 97                 
 98                 foreach($styleFont as $key => $value) {
 99                     if(substr($key, 0, 1) != \'_\') {
100                         $key = \'_\'.$key;
101                     }
102                     self::$_styleFont->setStyleValue($key, $value);
103                 }
104             } else {
105                 self::$_styleFont = $styleFont;
106             }
107         }
108     }
109     
110     /**
111     * Add a Title
112     * 
113     * @return array
114     */
115     public static function addTitle($text, $depth = 0) {
116         $anchor = \'_Toc\'.++self::$_anchor;
117         $bookmarkId = self::$_bookmarkId++;
118         
119         $title = array();
120         $title[\'text\'] = $text;
121         $title[\'depth\'] = $depth;
122         $title[\'anchor\'] = $anchor;
123         $title[\'bookmarkId\'] = $bookmarkId;
124         
125         self::$_titles[] = $title;
126         
127         return array($anchor, $bookmarkId);
128     }
129     
130     /**
131      * Get all titles
132      * 
133      * @return array
134      */
135     public static function getTitles() {
136         return self::$_titles;
137     }
138     
139     /**
140      * Get TOC Style
141      * 
142      * @return PHPWord_Style_TOC
143      */
144     public static function getStyleTOC() {
145         return self::$_styleTOC;
146     }
147     
148     /**
149      * Get Font Style
150      * 
151      * @return PHPWord_Style_Font
152      */
153     public static function getStyleFont() {
154         return self::$_styleFont;
155     }
156 }
157 ?>

解决这个bug,我的方法是增加了82、83、84行,把所有的数据都重新初始化,这样就不会有问题了。

分类:

技术点:

相关文章: