PHPWord生成多篇word文档的时候目录文件冗余bug解决方案

本文详细介绍了使用PHPWord生成Word文档目录的方法及原理,并针对多文档目录冗余的问题提供了有效的解决方案。

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行,把所有的数据都重新初始化,这样就不会有问题了。

转载于:https://www.cnblogs.com/toocooltohavefriends/p/5494958.html

PHPWord Beta 0.6.2 开发者指南 目 录 首先我们要了解文档最基本的信息和设置: 4 计量单位:缇(twips) 4 字体设置 4 文档属性设置 4 新建文档 5 添加页面 5 页面样式 5 页面样式属性 6 文本 7 添加文本 7 添加文本资源 7 文本样式 8 样式属性列表 9 添加换行符 10 添加分页符 10 列表 10 添加列表 10 列表样式 11 列表样式属性列表 11 超链接 11 添加超链接 11 超链接样式 12 图片 13 添加图片 13 图片样式 13 图片样式属性 13 添加GD生成图片 14 添加水印 14 添加对象 15 添加标题 15 添加目录 16 表格 17 添加表格 17 添加行 17 添加单元格 17 单元格样式 19 表格样式 20 页脚 22 页眉 23 模版 23 其他问题修改 25 解决文本缩进问题 25 表格对齐和表格缩进 27 图片缩进和绝对相对悬浮定位 30 首先我们要了解文档最基本的信息和设置:  因为是国外编辑的类库,存在对中文支持的问题,使用前,我们需要进行一些修正: 1、解决编码问题,PHPword 会对输入的文字进行utf8_encode编码转化,如果你使用GBK、GB2312或者utf8编码的话就会出现乱码,如果你用utf8编码,就查找类库中所有方法中的 utf8_encode 转码将其删除,如果你采用GBK或者GB2312编码,使用iconv进行编码转换。 2、解决中文字体支持,在writer/word2007/base.php中 312行添加 $objWriter->writeAttribute('w:eastAsia',$font) 3、启动php zip支持,windows环境下在php配置文件php.ini中,将extension=php_zip.dll前面的分号“;”去除;(如果没有,请添加extension=php_zip.dll此行并确保php_zip.dll文件存在相应的目录),然后同样在php.ini文件中,将 zlib.output_compression = Off 改为zlib.output_compression = On ; 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值