ephpoffice/phpword

PHPWord - 使用PHP读取、创建和写入Word文档

0.7.0 2014-01-28 07:03 UTC

This package is auto-updated.

Last update: 2024-09-21 21:01:39 UTC


README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

OpenXML - 使用PHP读取、写入和创建Word文档。

PHPWord是一个用PHP编写的库,用于创建Word文档。

因为生成的结果是docx文件(Office Open XML),可以被所有主流办公软件打开,所以无需Windows操作系统即可使用。

想要贡献? 分支我们吧!

要求

  • PHP版本5.3.0或更高

安装

建议您通过composer安装PHPWord库。为此,请将以下行添加到您的composer.json中。

{
    "require": {
       "phpoffice/phpword": "dev-master"
    }
}

文档

目录

  1. 基本用法
  2. 部分
  3. 文本
  4. 段落样式
  5. 表格
  6. 图片

基本用法

以下是一个PHPWord库的基本示例。

$PHPWord = new PHPWord();

// Every element you want to append to the word document is placed in a section.
// To create a basic section:
$section = $PHPWord->createSection();

// After creating a section, you can append elements:
$section->addText('Hello world!');

// You can directly style your text by giving the addText function an array:
$section->addText('Hello world! I am formatted.',
    array('name'=>'Tahoma', 'size'=>16, 'bold'=>true));

// If you often need the same style again you can create a user defined style
// to the word document and give the addText function the name of the style:
$PHPWord->addFontStyle('myOwnStyle',
    array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232'));
$section->addText('Hello world! I am formatted by a user defined style',
    'myOwnStyle');

// You can also put the appended element to local object like this:
$fontStyle = new PHPWord_Style_Font();
$fontStyle->setBold(true);
$fontStyle->setName('Verdana');
$fontStyle->setSize(22);
$myTextElement = $section->addText('Hello World!');
$myTextElement->setFontStyle($fontStyle);

// Finally, write the document:
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('helloWorld.docx');

度量单位

Open Office XML的基本长度单位是twip。twip表示“英寸点二十分之一”,即1 twip = 1/1440英寸。

您可以使用PHPWord辅助函数将英寸、厘米或点转换为twip。

// Paragraph with 6 points space after
$PHPWord->addParagraphStyle('My Style', array(
    'spaceAfter' => PHPWord_Shared_Font::pointSizeToTwips(6))
);

$section = $PHPWord->createSection();
$sectionStyle = $section->getSettings();
// half inch left margin
$sectionStyle->setMarginLeft(PHPWord_Shared_Font::inchSizeToTwips(.5));
// 2 cm right margin
$sectionStyle->setMarginRight(PHPWord_Shared_Font::centimeterSizeToTwips(2));

部分

Word中每个可见元素都放置在部分内部。要创建部分,请使用以下代码

$section = $PHPWord->createSection($sectionSettings);

$sectionSettings是一个可选的关联数组,用于设置部分。示例

$sectionSettings = array(
    'orientation' => 'landscape',
    'marginTop' => 600,
    'colsNum' => 2,
);

部分设置

以下是可以用于部分的可用设置

  • orientation 页面方向,即'portrait'(默认)或'landscape'
  • marginTop 页面顶部边距,以twip为单位
  • marginLeft 页面左侧边距,以twip为单位
  • marginRight 页面右侧边距,以twip为单位
  • marginBottom 页面底部边距,以twip为单位
  • borderTopSize 顶部边框大小,以twip为单位
  • borderTopColor 顶部边框颜色
  • borderLeftSize 左侧边框大小,以twip为单位
  • borderLeftColor 左侧边框颜色
  • borderRightSize 右侧边框大小,以twip为单位
  • borderRightColor 右侧边框颜色
  • borderBottomSize 底部边框大小,以twip为单位
  • borderBottomColor 底部边框颜色
  • headerHeight 页眉上方间距
  • footerHeight 页脚下方间距
  • colsNum 列数
  • colsSpace 列间距
  • breakType 部分断点类型(nextPage、nextColumn、continuous、evenPage、oddPage)

以下两个设置由orientation设置自动设置。您可以更改它们,但不太推荐。

  • pageSizeW 页面宽度,以twip为单位
  • pageSizeH 页面高度,以twip为单位

部分页码

您可以更改部分页码。

$section = $PHPWord->createSection();
$section->getSettings()->setPageNumberingStart(1);

文本

可以通过使用addTextcreateTextRun方法添加文本。 addText用于创建只包含文本且样式相同的简单段落。createTextRun用于创建包含不同样式(有些加粗,有些斜体等)或其它元素(如图片或链接)的复杂段落。

addText示例

$fontStyle = array('name' => 'Times New Roman', 'size' => 9);
$paragraphStyle = array('align' => 'both');
$section->addText('I am simple paragraph', $fontStyle, $paragraphStyle);

createTextRun示例

$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'));

属性
  • size 文本大小,例如2022
  • name 字体名称,例如Arial
  • bold 文本加粗,truefalse
  • italic 文本斜体,truefalse
  • superScript 文本是上标,truefalse
  • subScript 文本是下标,truefalse
  • underline 文本是下划线,truefalse
  • strikethrough 文本是删除线,truefalse
  • color 文本颜色,例如 FF0000
  • fgColor 前景色
  • line-height 文本行高,例如 1.01.5 等...

段落样式

属性
  • line-height 文本行高,例如 1.01.5 等...
  • align 段落对齐,leftrightcenter
  • spaceBefore 段落前的空格
  • spaceAfter 段落后的空格
  • tabs 自定义制表位集合
  • indent 缩进量

表格

以下说明如何创建表格。

$table = $section->addTable();
$table->addRow();
$table->addCell();

单元格样式
单元格跨行

您可以在多列上跨单元格。

$cell = $table->addCell(200);
$cell->getStyle()->setGridSpan(5);

图片

您可以使用以下示例轻松添加图片。

$section = $PHPWord->createSection();
$section->addImage('mars.jpg');

属性
  • width 像素宽度
  • height 像素高度
  • align 图片对齐,leftrightcenter
  • marginTop 顶部边距,以英寸为单位,可以是负值
  • marginLeft 左侧边距,以英寸为单位,可以是负值
  • wrappingStyle 可以是 inlinesquaretightbehindinfront

要添加具有属性的图片,请考虑以下示例。

$section->addImage(
    'mars.jpg',
    array(
        'width' => 100,
        'height' => 100,
        'marginTop' => -1,
        'marginLeft' => -1,
        'wrappingStyle' => 'behind'
    )
);