ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Yii继承PHPWord

2019-08-06 15:00:42  阅读:309  来源: 互联网

标签:继承 section addText Yii twips PHPWord 样式 添加 table


修正

中文支持的问题,使用前如果发现乱码,需要进行一些修正:

  • 解决编码问题,PHPword 会对输入的文字进行utf8_encode编码转化,如果你使用GBK、GB2312或者utf8编码的话就会出现乱码,如果你用utf8编码,就查找类库中所有方法中的 utf8_encode 转码将其删除,如果你采用GBK或者GB2312编码,使用iconv进行编码转换。
  • 解决中文字体支持,在writer/word2007/base.php中 312行添加 $objWriter->writeAttribute('w:eastAsia',$font)
  • 启动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 ; 

计量单位:缇(twips)

PHPWord最基本的计量单位:“缇”(twips),我们常常在文件中看到或使用计量单位“缇”,它是开源办公软件中最基本的计量单位,“缇”是"TWentieth of an Inch Point"的简写,意思 1/20磅,与其他常用剂量单位的换算是1缇=1/1,440英寸
1缇=1/567厘米
1缇=1/15像素

字体设置

文档默认字体是Arial,字号10号,我们可以通过以下方法设置默认字体和字号:
注,该库存在中文字体支持问题,解决方法:见文档开头

$PHPWord->setDefaultFontName('Tahoma');
$PHPWord->setDefaultFontSize(12);

文档属性设置

我们可以设置下列文档属性

名称类型描述
Creator String 创建者
Company String 公司
Title String 标题
Description String 描述
Category String 分类
Last modified by String 最后修改者
Created Datetime 创建时间
Modified Datetime 修改时间
Subject String 主题
Keywords String 关键词

我们可以通过以下方法设置文档属性

$properties = $PHPWord->getProperties();
$properties->setCreator('My name'); 
$properties->setCompany('My factory');
$properties->setTitle('My title');
$properties->setDescription('My description'); 
$properties->setCategory('My category');
$properties->setLastModifiedBy('My name');
$properties->setCreated( mktime(0, 0, 0, 3, 12, 2010) );
$properties->setModified( mktime(0, 0, 0, 3, 14, 2010) );
$properties->setSubject('My subject'); 
$properties->setKeywords('my, key, word');

新建文档

添加页面

添加默认页面(默认页面方向和页边距):

$section = $PHPWord->createSection();

页面样式

调整页面样式和布局有两种方法:
1.创建样式数组:

$sectionStyle = array('orientation' => null,
       'marginLeft' => 900,
       'marginRight' => 900,
       'marginTop' => 900,
       'marginBottom' => 900);
$section = $PHPWord->createSection($sectionStyle);

2.直接调用样式属性设置方法进行设置:

$section = $PHPWord->createSection();
$sectionStyle = $section->getSettings();
$sectionStyle->setLandscape();
$sectionStyle->setPortrait();
$sectionStyle->setMarginLeft(900);
$sectionStyle->setMarginRight(900);
$sectionStyle->setMarginTop(900);
$sectionStyle->setMarginBottom(900);

页面样式属性

注意:所有的属性对大小写敏感 !

属性描述
orientation 页面方向:默认竖向:null 横向:landscape
marginTop 上边距,单位:twips.
marginLeft 左边距,单位:twips.
marginRight 右边距,单位:twips.
marginBottom 下边距,单位:twips..
borderTopSize 上边框尺寸,单位:twips.
borderTopColor 上边框颜色
borderLeftSize 左边框尺寸,单位 :twips.
borderLeftColor 左边框颜色
borderRightSize 右边框尺寸,单位:twips.
borderRightColor 右边框颜色
borderBottomSize 底边框尺寸,单位:twips.
borderBottomColor 底边框颜色

页面高度和宽度是自动设置的,你可以通过以下两个属性来修改,但不推荐进行修改。

属性描述
pageSizeW 页面宽度,单位: twips.
pageSizeH 页面高度,单位:twips.

文本

添加文本

向文档添加文本使用方法函数: addText.(注意PHPword 会对输入的文字进行utf8_encode编码转化,如果你使用GBK、GB2312或者utf8编码的话就会出现乱码,如果你用utf8编码,就查找类库中所有方法中的 utf8_encode 转码将其删除,如果你采用GBK或者GB2312编码,使用iconv进行编码转换。)

$section->addText( $text, [$fontStyle], [$paragraphStyle] );
addText()  
参数 类型 描述
$text String 文本内容.
$fontStyle String / Array 字体样式.
$paragraphStyle String / Array 段落样式

添加文本资源

文本资源可以包含文本链接,可以统一赋予段落样式,添加文本资源使用函数方法createTextrun.

createTextRun()  
参数 类型 描述
$paragraphStyle String / Array 文本样式.

添加文本资源后,就可以添加具有独特样式的文本或链接了。

$textrun = $section->createTextRun();
$textrun->addText('I am bold', array('bold'=>true)); 
$textrun->addText('I am italic, array('italic'=>true));
$textrun->addText('I am colored, array('color'=>'AACC00'));

当然也可以继承使用段落或文字样式

文本样式

设置文本样式有两种方法:
1.内嵌样式:

$fontStyle = array('color'=>'006699', 'size'=>18, 'bold'=>true);
$section->addText('helloWorld', $fontStyle);

$text = $section->addText('helloWorld');
$style = $text->getStyle();
$style->setColor('006699');
$style->setSize(18);
$style->setBold();

2.或者定义一个样式定义设置文本样式,定义一种样式后,必须把第二个参数设置为样式名称,使用方法函数addFontStyle:

$PHPWord->addFontStyle( $styleName, $fontStyle);
addFontStyle()  
参数 类型 描述
$styleName String 样式名称
$fontStyle Array 样式风格.

示例:

$fontStyle = array('color'=>'006699', 'size'=>18, 'bold'=>true);
$PHPWord->addFontStyle('myOwnStyle', $fontStyle);
$text = $section->addText('helloWorld', 'myOwnStyle');

添加段落样式,使用方法函数addParagraphStyle:

addParagraphStyle()  
参数 类型 描述
$styleName String 段落样式名称.
$paragraphStyle Array 段落样式.

样式属性列表

属性大小写敏感 !

Font Style

名称描述
size 字号.
name 字体
bold 粗体
italic 斜体
superScript 上标
subScript 下标
underline 下划线,使用常量: PHPWord_Style_Font::UNDERLINE_...
Color 字体颜色
fgColor 前景色. 只能使用预定义常量:PHPWord_Style_Font::FGCOLOR_...

Paragraph Style

名称描述
align 水平对齐:leftrightcenterboth / justify
spaceBefore 段前间距,单位: twips.
spaceAfter 段后间距,单位:twips
spacing 行间距,单位: twips.

换行符

添加换行符,使用方法函数 addTextBreak:

$section->addTextBreak();

添加多个换行符:

$section->addTextBreak(15);

添加分页符

添加分页符,使用方法函数:addPageBreak:

$section->addPageBreak();

列表

添加列表

添加列表使用方法函数: addListItem:

$section->addListItem( $text, [$depth], [$styleText], [$styleList], [$styleParagraph] );
addListItem()  
参数 类型 描述
$text String 文本内容.
$depth Integer 编号
$styleText String / Array 文本样式.
$styleList Array 列表样式.
$styleParagraph String / Array 段落样式

列表样式

示例:

$listStyle = array('listType' => PHPWord_Style_ListItem::TYPE_NUMBER);
$section->addListItem('Listitem 1', 0, null, $listStyle);

列表样式属性列表

属性大小写敏感!

名称描述
listType 列表符号样式.使用常量 PHPWord_Style_ListItem::TYPE_...

超链接

添加超链接

添加超链接,使用方法函数: addLink:

$section->addLink( $linkSrc, [$linkName], [$styleFont], [$styleParagraph]);
addListItem()  
参数 类型 描述
$linkSrc String 链接地址
$linkName String 链接名称.
$styleFont String / Array 文本样式
$styleParagraph String / Array 段落样式

注意在添加链接地址时最好trim一下前后有空格很可有可能导致文档打不开

超链接样式

定义超链接风格的两种方法

  1. 内嵌样式:
$linkStyle = array('color'=>'0000FF',
    'underline'=>PHPWord_Style_Font::UNDERLINE_SINGLE);
$section->addLink('http://www.google.de', null, $linkStyle);
  1. 或者定义一个样式定义设置超链接样式,定义一种样式后,必须把第三个参数设置为样式名称
$linkStyle = array('color'=>'0000FF',
    'underline'=>PHPWord_Style_Font::UNDERLINE_SINGLE);
$PHPWord->addLinkStyle('myHyperlinkStyle', $linkStyle);
$section->addLink('http://www.google.de', null, 'myHyperlinkStyle');
addLinkStyle()  
参数 类型 描述
$styleName String 超链接样式名称.
$styles Array 链接样式.可以使用各种字体样式属性

图片

添加图片

添加图片的函数方法: addImage:

$section->addImage( $src, [$style] );
addImage()  
参数 类型 描述
$src String 图像的服务器路径,支持相对和绝对路径
$style Array 图片样式.

注意在添加图片路径时最好trim一下前后有空格很可有可能导致文档打不开

图片样式

添加图片样式只能使用数组方式 :

$imageStyle = array('width'=>350, 'height'=>350, 'align'=>'center');
$section->addImage('EARTH.jpg', $imageStyle);

图片样式属性

大小写敏感!

名称描述
width 图像宽度,单位像素
height 图像高度,单位像素
align 图像对齐方式leftrightcenter

如果没有指定图片高或宽的属性,系统将使用PHP原生函数”getimagesize”来获取相关属性。
PHPWord 支持的图片格式: gif, jpeg, png, bmp, tiff.

添加GD生成图片

你也可以添加由GD库生成的图片,使用函数方法:addMemoryImage:

$section->addMemoryImage( $link, [$style] );
addMemoryImage()  
参数 类型 描述
$link String 生成图片的php文件的路径. 注意: 应设置文件的绝对路径(就像你在浏览器中调用php文件),否则会发生错误。
$style Array 图像样式.

示例:

$section->addMemoryImage('http://localhost/image.php');

你GD图片样式的设置和本地图片一样.

PHPWord 支持的 GD 图片类型: png, jpeg, gif.

添加水印

添加水印的页面需要一个头部引用,添加水印方法函数:addWatermark

addWatermark()  
参数 类型 描述
$src String 水印图片的文件地址
$style Array 水印图片样式

水印图片是在页面是绝对定位的,所以水印图片至少需要两个样式属性

名称描述
marginLeft 左边距,单位像素
marginTop 上边距,单位像素

注:图片样式并没有提供图像并排,文字环绕等功能,可以通过与表格想结合进行解决。

添加对象

我们可以使用方法函数 addObject,添加对象和链接

$section->addObject( $src, [$style] );
addObject()  
参数 类型 描述
$src String 文件的服务器,支持相对和绝对路径.
$style Array 对象样式.

对象属性是有一个样式:
属性区分大小写!

名称描述
align 对齐方式leftrightcenter

PHPWord 支持的对象类型: XLS, DOC, PPT.

添加标题

我们可以使用标题来为结构化文档或为文档建立目录,添加标题使用方法函数addTitleStyle 和 addTitle:

$PHPWord->addTitleStyle( $titleCount, [$fontStyle] );
addTitleStyle()  
参数 类型 描述
$src Integer 标题级别,最多支持9级标题
$fontStyle Array 标题字体样式

需要添给标题添加一个样式,否则文档不会将其作为一个真正的标题来处理。

定义标题样式后,定义标题就很简单了,可以使用函数方法:addTitle;

$section->addTitle( $text, [$depth] );
addTitle()  
参数 类型 描述
$text String 标题文本内容
$depth Integer 标题级别编号,通过该参数调用addTtileStyle()设置的标题样式

添加目录

添加目录使用方法函数: addTOC:

$styleTOC = array('tabLeader'=>PHPWord_Style_TOC::TABLEADER_DOT);
$styleFont = array('spaceAfter'=>60, 'name'=>'Tahoma', 'size'=>12);
$section->addTOC($styleFont, $styleTOC);
addTOC()  
参数 类型 描述
$styleFont Array 目录字体样式
$styleTOC Array 目录样式

目录样式属性列表:

样式属性区分大小写 !

名称描述
tabLeader 标题的类型和对应页码.默认使用系统常量 PHPWord_Style_TOC::TABLEADER_...
tabPos 标题与页码的位置,单位: twips.
Indent 标题缩进,单位: twips.

表格

添加表格

添加表格使用函数方法:addTable:

$table = $section->addTable( [$tableStyle] );

参数 $tableStyle 是可选的. 表格样式这章有关于表格样式的详细说明。为addTable建立一个本地对象,我们需要使用这个对象来调用相关函数方法。

  • 添加行
$table->addRow( [$height] );

行的高度可以通过$height参数来设置,单位:twips.

  • 添加单元格

单元格添加前必须先添加行,添加单元格的函数方法为: addCell

$cell = $table->addCell(h, [$cellStyle] );
addCell()  
参数 类型 描述
$width Integer 单元格宽度: twips.
$cellStyle Array 单元格样式

为addcell创建一个本地对象,需要使用该对象来 调用以下函数

名称描述
addText 添加文本
addTextBreak 添加换行符
addLink 添加链接
addImage 添加图片
addMemoryImage 添加水印
addListItem 添加列表
addObject 添加对象
addPreserveText 添加页码,只对页眉和页脚有效

示例1:

$table = $section->addTable();
$table->addRow();
$cell = $table->addCell(2000);
$cell->addText('Cell 1');
$cell = $table->addCell(2000);
$cell->addText('Cell 2');
$cell = $table->addCell(2000);
$cell->addText('Cell 3');

示例2:

$table = $section->addTable();
$table->addRow(400);
$table->addCell(2000)->addText('Cell 1');
$table->addCell(2000)->addText('Cell 2');
$table->addCell(2000)->addText('Cell 3');

$table->addRow(1000);
$table->addCell(2000)->addText('Cell 4');
$table->addCell(2000)->addText('Cell 5');
$table->addCell(2000)->addText('Cell 6');

单元格样式

使用addCell的第二个参数来给单元格设置样式
示例:

$cellStyle = array('textDirection'=>PHPWord_Style_Cell::TEXT_DIR_BTLR,     'bgColor'=>'C0C0C0');

$table = $section->addTable();
$table->addRow(1000);
$table->addCell(2000, $cellStyle)->addText('Cell 1');
$table->addCell(2000, $cellStyle)->addText('Cell 2');
$table->addCell(2000, $cellStyle)->addText('Cell 3');
$table->addRow();
$table->addCell(2000)->addText('Cell 4');
$table->addCell(2000)->addText('Cell 5');
$table->addCell(2000)->addText('Cell 6');

单元格样式属性列表:

属性大小写敏感 !

名称描述
valign 单元格内容对齐方式: left, right, center
textDirection 文本方向. 使用预定常量 PHPWord_Style_Cell:: TEXT_DIR_...
bgColor 单元格背景色
borderTopSize 单元格上边框尺寸,单位 twips.
borderTopColor 单元格上边框 颜色
borderLeftSize 单元格左边框尺寸,单位twips
borderLeftColor 单元格左边框颜色
borderRightSize 单元格右边框尺寸,单位twips
borderRightColor 单元格右边框颜色
borderBottomSize 单元格下边框尺寸 ,单位twips
borderBottomColor 单元格下边框颜色

表格样式

我们可以设置整个表格的样式,通过创建表格函数addTable的参数$tableStyle,表格具有如下样式属性 
属性名称大小写敏感!

名称描述
cellMarginTop 单元格上边距,单位: twips.
cellMarginLeft 单元格左边距,单位: twips.
cellMarginRight 单元格右边距,单位: twips.
cellMarginBottom 单元格下边距,单位: twips.

示例:

$tableStyle = array('cellMarginTop'=>80,
     'cellMarginLeft'=>80,
     'cellMarginRight'=>80,
     'cellMarginBottom'=>80);
$table = $section->addTable($tableStyle);

我们可以使用函数方法: addTableStyle,为表格定义一个完整的样式。

$PHPWord->addTableStyle($styleName, $styleTable, [$styleFirstRow] );
addTableStyle()  
参数 类型 描述
$styleName String 表样式名称
$styleTable Array 这个表的样式
$styleFirstRow Array 表头样式(第一行)

示例:

$styleTable = array('borderColor'=>'006699',
     'borderSize'=>6,
     'cellMargin'=>50);
$styleFirstRow = array('bgColor'=>'66BBFF');
$PHPWord->addTableStyle('myTable', $styleTable, $styleFirstRow);

$table = $section->addTable('myTable');
$table->addRow(400);
$table->addCell(2000)->addText('Cell 1');
$table->addCell(2000)->addText('Cell 2');
$table->addCell(2000)->addText('Cell 3');
$table->addRow(1000);
$table->addCell(2000)->addText('Cell 4');
$table->addCell(2000)->addText('Cell 5');
$table->addCell(2000)->addText('Cell 6');

表格样式属性,注意属性名称大小写敏感!

名称描述
cellMarginTop 单元格上边距,单位:twips.
cellMarginLeft 单元格左边距,单位:twips.
cellMarginRight 单元格右边距,单位:twips.
cellMarginBottom 单元格下边距,单位:twips.
cellMargin 单元格间距,单位:twips.
bgColor 表格背景色
borderTopSize 表格上边框尺寸,单位:twips.
borderTopColor 表格上边框颜色
borderLeftSize 表格左边框尺寸,单位:twips.
borderLeftColor 表格左边框颜色
borderRightSize 表格右边框尺寸,单位:twips.
borderRightColor 表格右边框颜色
borderBottomSize 表格下边框尺寸,单位:twips..
borderBottomColor 表格下边框颜色
borderInsideHSize 表格内水平网格尺寸,单位: twips.
borderInsideHColor 表格内水平网格颜色
borderInsideVSize 表格内垂直网格尺寸,单位: twips.
borderInsideVColor 表格内垂直网格颜色
borderSize 表格边框尺寸,单位:twips.
borderColor 表格边框颜色

注意:表格在word布局中的功能可以进行体现,例如进行图片,对象等的布局可以考虑与表格结合进行处理

页脚

添加文档页脚使用函数方法: createFooter:

$footer = $section->createFooter();

确保在本地对象中保存页脚,并使用下列函数

名称描述
addText 添加文本
addTextBreak 添加换行符
addImage 添加图像
addMemoryImage 添加GD生成图像
addListItem 添加列表
addPreserveText 添加页码,只能在页眉或页脚使用
addTable 添加表格
createTextrun 添加文本资源

向页脚(页眉)添加页码使用函数方法:addPreserveText:

addPreserveText( $text, [$style] );
addPreserveText()  
参数 类型 描述
$text String 页脚(页眉)的文本内容
$style Array 文字样式.

示例:

$footer->addPreserveText('Page {PAGE} of {NUMPAGES}.');

页眉

添加页眉,使用函数方法: createHeader:

$header = $section->createHeader();

确保页眉是建立在一个文档中,页眉和页脚使用相同的属性和函数,详见页脚章节 。
注意:只用添加了页眉的页面,才能添加和使用图片水印

模版

我们可以利用搜索替换功能创建一个docx格式的模版文档,来替换文档中你想替换的文本. 但是要注意,只有文本或链接可以被替换。加载模版文档使用函数方法:loadTemplate function.

loadTemplate()  
参数 方法 描述
$strFilename String 模版文件路径和名称

加载完成模版文档后,你可以使用函数方法: setValue 来搜索替换相关内容

setValue()  
参数 Type Description
$search Mixed 搜索的值
$replace Mixed 替换的值
$template = $PHPWord->loadTemplate('Template.docx');
$template->setValue('Name', 'Somebody someone');
$template->setValue('Street', 'Coming-Undone-Street 32');

被搜索替换的标签格式为: ${YOUR_SEARCH_PATTERN}
不能添加新的PHPWORD元素到加载的模版文档中

$PHPWord = new PhpWord(); $tempPlete = $PHPWord->loadTemplate('/data/home/www/web/runtime/old.docx'); $tempPlete->setValue('${p3-9}', '跨国有限公司'); // $objWriter = IOFactory::createWriter($PHPWord, 'Word2007'); $aa = $tempPlete->saveAs('/data/home/www/web/runtime/new.docx');

模版使用的几个注意事项:

  • 从模板生成word文档,支持在word模板文档里写替换标签,标签格式为${xxx},不过一定要注意,不要直接在word里编辑这些标签,一定要在文本文档里先写好标签,直接拷贝粘贴上去,千万不要编辑,否则无法替换,原因也很简单,把word文档另存为xml,然后看xml里标签的位置,会发现标签中间被插入了很多没用的xml节点,还有中文字体的节点。。。
  • 模版文档要一次性完成在保存,否则会出现替换失败问题。
  • 中文乱码问题,这个一定是存在的,如果php环境已经是utf8了,要找到关键地方,转utf8的代码,去掉,否则就是转两次编码了,典型的是phpwordtemplate.php文件,把这行注释掉:$replace = utf8_encode($replace);
  • linux下报“Could not close zip file.”错误,这个你永远想不到,要将模板文件所在目录权限改为可写,因为要在那个目录下生成临时文件
  • 引用模板文件和另存文件路径什么的,最好用绝对路径

声明:本文内容来自网络文件,部分未测试,后面有机会整个实例出来,目前整理成一份适合在网页阅读的文档,供参考。
官方实例:https://github.com/PHPOffice/...
官网:https://phpword.readthedocs.i...

转载:https://segmentfault.com/a/1190000019479817?utm_source=tag-newest#articleHeader44

标签:继承,section,addText,Yii,twips,PHPWord,样式,添加,table
来源: https://www.cnblogs.com/xiaommvik/p/11308929.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有