Home  >  Article  >  Backend Development  >  解决PHPWord中文乱码及设置中文字体、常见使用问题

解决PHPWord中文乱码及设置中文字体、常见使用问题

PHP中文网
PHP中文网Original
2017-06-06 13:39:475198browse

解决PHPWord中文乱码及设置中文字体、常见使用问题

最近一个项目开发要用到PHP技术导出Word文档,比较了几种方案,首先是使用Microsoft Office自带的ActiveX/COM组件,比如Word.Application,这种方式的优点是格式兼容度高,可以生成纯doc的Word2003格式文档,缺点一是比较占资源(调用会启动一个WINWORD.EXE进程),不适合Web多用户访问使用;二是PHP这种Web开发技术大多数是跑在Linux服务器上,当然也就无法使用Windows下的技术了,平台可移植和兼容性不好。第二种生成Word的方案是生成Word兼容的网页格式,然后以Word方式打开,这种方案总体上感觉怪怪的,毕竟文件格式是HTML的,而且格式兼容度不好,不过这种方式的优点是节省服务器资源,能够快速生成;最后一种方案也就是今天的主角,采用PHPWord生成Word2007(docx)格式的文档,现在基本上微软Office Word 2003以后的版本均兼容这种格式了,对于2003版本来说,仅需要下载安装个兼容格式包(下载地址),也能正常打开这类文件,当然如果你使用的是最新版本的Office(包括但不限于Office 2007、Office 2010)则不需要安装此格式包。

好了,下面我就介绍一下PHPWord,大家可以通过PHPword下载并获得关于项目的更多信息。

我在使用过程中主要遇到了中文乱码的问题,结合网上大神们的指导,通过下面的方式解决了这类问题,希望对大家有所帮助。

1、增加东亚字体支持

打开并编辑路径/Writer/Word2007/Base.php文件内容,大概在第349行(行数随着版本可能会有变化)大概函数_writeTextStyle内添加:

$objWriter->writeAttribute('w:eastAsia', $font)

比如我的修改片段基本是下面这样:

// Font
if($font != 'Arial') {
    $objWriter->startElement('w:rFonts');
        $objWriter->writeAttribute('w:eastAsia', $font); // 添加这行
        $objWriter->writeAttribute('w:ascii', $font);
        $objWriter->writeAttribute('w:hAnsi', $font);
        $objWriter->writeAttribute('w:cs', $font);
    $objWriter->endElement();
}

2. 解决中文乱码问题编辑PHPWord/Template.php,找到代码$replace = utf8_encode($replace);,删除或者注释掉这行代码,添加$replace = iconv( 'gbk','utf-8', $replace);,比如代码改为如下:

/**
 * 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);
        $replace =iconv('gbk', 'utf-8', $replace); // 注释掉上面行后添加这行
    }
 
    $this->_documentXML = str_replace($search, $replace, $this->_documentXML);
}

调用方式如下:

/**
 * Add a Text Element
 * 
 * @param string $text
 * @param mixed $styleFont
 * @param mixed $styleParagraph
 * @return PHPWord_Section_Text
 */
public function addText($text, $styleFont = null, $styleParagraph = null) {
    //$givenText = utf8_encode($text);
    $givenText = iconv('gbk', 'utf-8', $text); // 注释掉上面行后添加这行
    $text = new PHPWord_Section_Text($givenText, $styleFont, $styleParagraph);
    $this->_elementCollection[] = $text;
    return $text;
}

上面的代码主要解决模板的问题,下面同样的道理,解决Section添加文本的问题,找到代码$givenText = utf8_encode($text);,删除或者注释掉这行代码,添加$givenText = iconv('gbk', 'utf-8', $text);,比如代码如下:

调用方式和上面的模板调用大同小异,这边就不列举了。

折腾了这么多,突然发现网上还有另外一个版本的PhpWord,项目类名大小写上略有不同,隶属于PHPOffice/PHPWord,GitHub项目地址(文档)。这个版本的PHPWord内容更加丰富,支持的功能也比较多(包括行间距,缩进和首行缩进等),最后我也采取的这个版本的PHPWord,值得注意的是这两个版本的PHPWord在API接口上基本一致,可以通用。但是有些API,在PHPOffice/PHPWord里是不推荐的,比如createSection需要改成addSection,另外应用这个版本的PHPWord不需要像上面那样做任何中文支持的修改,比较省事。

这两个PHPWord项目的官方都提供了较详细的使用例子和文档,这里就不介绍了。

最后提示的是:在模板模式下loadTemplate,只能使用setValue等模板操作方法,不能再添加段落或者段落修改了。这个略有不便。

对于PHPOffice/PHPWord我提供一个简单的例子供参考(当然官方例子更多):

require_once 'PhpOffice/PhpWord/PhpWord.php'; // 包含头文件
use PhpOffice\PhpWord\Autoloader;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\IOFactory;
 
require_once __DIR__ . '/PhpOffice/PhpWord/Autoloader.php';
Autoloader::register();
Settings::loadConfig();
 
// Create a new PHPWord Object
$PHPWord = new \PhpOffice\PhpWord\PhpWord();
$PHPWordHelper= new \PhpOffice\PhpWord\Shared\Font();
 
$PHPWord->setDefaultFontName('仿宋'); // 全局字体
$PHPWord->setDefaultFontSize(16);     // 全局字号为3号
 
// 设置文档的属性,这些在对文档右击属性可以看到,也可以省去这些步骤
$properties = $PHPWord->getDocumentProperties();
$properties->setCreator('张三');   // 创建者
$properties->setCompany('某公司'); // 公司
$properties->setTitle('某某文档'); // 标题
$properties->setDescription('http://wangye.org'); // 描述
$properties->setLastModifiedBy('李四'); // 最后修改
$properties->setCreated( time() );      // 创建时间
$properties->setModified( time() );     // 修改时间
 
// 添加3号仿宋字体到'FangSong16pt'留着下面使用
$PHPWord->addFontStyle('FangSong16pt', array('name'=>'仿宋', 'size'=>16));
 
// 添加段落样式到'Normal'以备下面使用
$PHPWord->addParagraphStyle(
  'Normal',array(
    'align'=>'both',
    'spaceBefore' => 0,
    'spaceAfter' => 0,
    'spacing'=>$PHPWordHelper->pointSizeToTwips(2.8),
    'lineHeight' => 1.19,  // 行间距
    'indentation' => array( // 首行缩进
      'firstLine' => $PHPWordHelper->pointSizeToTwips(32)
    )
  )
);
 
// Section样式:上3.5厘米、下3.8厘米、左3厘米、右3厘米,页脚3厘米
// 注意这里厘米(centimeter)要转换为twips单位
$sectionStyle = array(
    'orientation' => null,
    'marginLeft' => $PHPWordHelper->centimeterSizeToTwips(3),
    'marginRight' => $PHPWordHelper->centimeterSizeToTwips(3),
    'marginTop' => $PHPWordHelper->centimeterSizeToTwips(3.5),
    'marginBottom' => $PHPWordHelper->centimeterSizeToTwips(3.8),
    'pageNumberingStart' => 1, // 页码从1开始
    'footerHeight' => $PHPWordHelper->centimeterSizeToTwips(3),
);
 
$section = $PHPWord->addSection($sectionStyle); // 添加一节
 
// 下面这句是输入文档内容,注意这里用到了刚才我们添加的
// 字体样式FangSong16pt和段落样式Normal
$section->addText('文档内容', 'FangSong16pt', 'Normal');
$section->addTextBreak(1); // 新起一个空白段落
 
$objWriter = IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('/path/to/file'); // 保存到/path/to/file路径下
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn