Correction
Chinese support issues, prior to use if you find garbage, require some amendments:
- Solve coding problems, PHPword text input will be utf8_encode coding conversion, if you use the GBK, GB2312 or utf8 encoding, then there will be garbled, if you use utf8 encoding, transcoding library to find utf8_encode all methods to delete, if you are using GBK or GB2312 encoding, use iconv transcoding.
- Solve the Chinese font support, add $ objWriter-> writeAttribute 312 lines in writer / word2007 / base.php in ( 'w: eastAsia', $ font)
- Under start php zip support, windows environment php configuration file php.ini, the extension = php_zip.dll front of the semicolon ";" removal; (If not, please add extension = php_zip.dll this line and make sure php_zip.dll appropriate directory of file), and then also in the php.ini file, to the zlib.output_compression = Off zlib.output_compression = on;
Unit: Ti (twips)
PHPWord basic unit of measurement: "Ti" (twips), we often see in the document or use units of measurement, "Ti", which is open-source office software in the basic unit of measurement, "Ti" is "TWentieth of an Inch Point "shorthand meaning 1/20 pounds, and other commonly used dosage unit is a conversion Ti = 1 / 1,440 inch
1 Ti = 1/567 cm
1 Ti = 1/15 pixel
Font settings
Document default font is Arial, size No. 10, we can set the default font and size in the following ways:
Note that the inventory in Chinese font support problems, solutions: see beginning of the document
$PHPWord->setDefaultFontName('Tahoma');
$PHPWord->setDefaultFontSize(12);
Set document properties
We can set the following document properties
name | Types of | description |
---|---|---|
Creator | String | creator |
Company | String | the company |
Title | String | title |
Description | String | description |
Category | String | classification |
Last modified by | String | Last Modified By |
Created | Datetime | Created |
Modified | Datetime | Change the time |
Subject | String | theme |
Keywords | String | Key words |
We can set the document properties in the following ways
$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');
New Document
Add Page
Add a default page (the default page orientation and margins):
$section = $PHPWord->createSection();
Page Style
Adjust the page layout and style in two ways:
1. Create a style array:
$sectionStyle = array('orientation' => null,
'marginLeft' => 900, 'marginRight' => 900, 'marginTop' => 900, 'marginBottom' => 900); $section = $PHPWord->createSection($sectionStyle);
2. Call direct style property set method to set:
$section = $PHPWord->createSection();
$sectionStyle = $section->getSettings();
$sectionStyle->setLandscape();
$sectionStyle->setPortrait();
$sectionStyle->setMarginLeft(900);
$sectionStyle->setMarginRight(900);
$sectionStyle->setMarginTop(900);
$sectionStyle->setMarginBottom(900);
Page Style property
Note: All properties are case-sensitive!
Attributes | description |
---|---|
orientation | Page orientation: Default vertical: lateral null: Landscape |
marginTop | On the margins, unit: twips. |
marginLeft | Left margin, unit: twips. |
marginRight | Right margin, unit: twips. |
marginBottom | Lower margins, unit: twips .. |
borderTopSize | The frame size, unit: twips. |
borderTopColor | On border color |
borderLeftSize | Left frame size, unit: twips. |
borderLeftColor | Left border color |
borderRightSize | The right frame size, unit: twips. |
borderRightColor | Right border color |
borderBottomSize | Bottom frame size, unit: twips. |
borderBottomColor | The color of the bottom border |
Page height and width are set automatically, you can modify the following two properties, but is not recommended for modification.
Attributes | description |
---|---|
pageSizeW | Page width, unit: twips. |
pageSizeH | Page height, unit: twips. |
text
Adding text
Use function added to the document text:. AddText (Note PHPword will be utf8_encode input text encoding conversion, if you use the GBK, GB2312 or utf8 encoding, then there will be garbled, if you use utf8 encoding, you find all the library the method of transcoding utf8_encode delete, or if you use GBK GB2312 encoding using encoding conversion iconv.)
$section->addText( $text, [$fontStyle], [$paragraphStyle] );
addText() | ||
---|---|---|
parameter | Types of | description |
$text | String | Text content. |
$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一下前后有空格很可有可能导致文档打不开
超链接样式
定义超链接风格的两种方法
- 内嵌样式:
$linkStyle = array('color'=>'0000FF',
'underline'=>PHPWord_Style_Font::UNDERLINE_SINGLE);
$section->addLink('http://www.google.de', null, $linkStyle);
- 或者定义一个样式定义设置超链接样式,定义一种样式后,必须把第三个参数设置为样式名称
$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元素到加载的模版文档中
模版使用的几个注意事项:
- 从模板生成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