borjalive/phpword

PHPWord - 一个纯PHP库,用于读取和写入文档处理文档(OOXML,ODF,RTF,HTML,PDF)。修复了cloneBlock

0.18.4 2022-07-20 14:50 UTC

README

主分支: 最新稳定版本 构建状态 代码质量 覆盖率状态 总下载量 许可证 加入聊天 https://gitter.im/PHPOffice/PHPWord

开发: 最新开发版本 构建状态 代码质量 覆盖率状态

PHPWord是一个用纯PHP编写的库,提供了一组用于写入和读取不同文档文件格式的类。PHPWord当前版本支持MicrosoftOffice Open XML(OOXML或OpenXML),OASISOpen Document Format for Office Applications(OpenDocument或ODF),Rich Text Format(RTF),HTML和PDF。

PHPWord是一个开源项目,遵循LGPL版本3许可协议。PHPWord旨在通过整合持续集成单元测试来成为一款高质量的软件产品。您可以通过阅读开发者文档来了解更多关于PHPWord的信息。

如果您有任何问题,请在StackOverFlow上提问

了解更多关于PHPWord的信息

功能

使用PHPWord,您可以使用PHP 5.3.3+脚本来动态创建OOXML、ODF或RTF文档。以下是可以使用PHPWord库执行的一些操作:

  • 设置文档属性,例如标题、主题和创建者。
  • 创建具有不同设置(例如纵向/横向、页面大小和页码)的文档部分。
  • 为每个部分创建页眉和页脚。
  • 设置默认字体类型、字体大小和段落样式。
  • 使用UTF-8和东亚字体/字符。
  • 定义自定义字体样式(例如加粗、斜体、颜色)和段落样式(例如居中、多栏、间距),无论是命名样式还是文本中的内联样式。
  • 插入段落,无论是简单的文本还是包含其他元素(文本运行)的复杂段落。
  • 插入标题(页眉)和目录。
  • 插入文本断和页面断。
  • 插入并格式化图像,无论是本地、远程还是作为页面水印。
  • 插入二进制OLE对象,如Excel或Visio。
  • 插入并格式化具有自定义属性(例如重复作为标题行)的表格,以及单元格(例如背景颜色、跨行、跨列)。
  • 以项目符号、编号或多层次方式插入列表项。
  • 插入超链接。
  • 插入脚注和尾注。
  • 插入绘图形状(弧、曲线、线、折线、矩形、椭圆)。
  • 插入图表(饼图、环形图、条形图、折线图、面积图、散点图、雷达图)。
  • 插入表单字段(文本输入、复选框和下拉列表)。
  • 从模板创建文档。
  • 使用XSL 1.0样式表转换OOXML模板的页眉、主体部分和页脚。
  • ...还有更多正在开发中的功能

要求

PHPWord需要以下内容:

安装

PHPWord通过Composer安装。要在项目中添加PHPWord的依赖项,您可以:

运行以下命令以使用最新稳定版本:

    composer require phpoffice/phpword

或者如果您想要最新master版本:

    composer require phpoffice/phpword:dev-master

当然,您也可以手动编辑您的composer.json文件

{
    "require": {
       "phpoffice/phpword": "v0.18.*"
    }
}

入门

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

<?php
require_once 'bootstrap.php';

// Creating the new document...
$phpWord = new \PhpOffice\PhpWord\PhpWord();

/* Note: any element you append to a document must reside inside of a Section. */

// Adding an empty Section to the document...
$section = $phpWord->addSection();
// Adding Text element to the Section having font styled by default...
$section->addText(
    '"Learn from yesterday, live for today, hope for tomorrow. '
        . 'The important thing is not to stop questioning." '
        . '(Albert Einstein)'
);

/*
 * Note: it's possible to customize font style of the Text element you add in three ways:
 * - inline;
 * - using named font style (new font style object will be implicitly created);
 * - using explicitly created font style object.
 */

// Adding Text element with font customized inline...
$section->addText(
    '"Great achievement is usually born of great sacrifice, '
        . 'and is never the result of selfishness." '
        . '(Napoleon Hill)',
    array('name' => 'Tahoma', 'size' => 10)
);

// Adding Text element with font customized using named font style...
$fontStyleName = 'oneUserDefinedStyle';
$phpWord->addFontStyle(
    $fontStyleName,
    array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
);
$section->addText(
    '"The greatest accomplishment is not in never falling, '
        . 'but in rising again after you fall." '
        . '(Vince Lombardi)',
    $fontStyleName
);

// Adding Text element with font customized using explicitly created font style object...
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(true);
$fontStyle->setName('Tahoma');
$fontStyle->setSize(13);
$myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
$myTextElement->setFontStyle($fontStyle);

// Saving the document as OOXML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');

// Saving the document as ODF file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
$objWriter->save('helloWorld.odt');

// Saving the document as HTML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
$objWriter->save('helloWorld.html');

/* Note: we skip RTF, because it's not XML-based and requires a different example. */
/* Note: we skip PDF, because "HTML-to-PDF" approach is used to create PDF documents. */

更多示例请参阅示例文件夹。要轻松访问这些示例,请在示例目录下启动php -S localhost:8000,然后浏览到https://:8000以查看示例。您还可以阅读开发者文档以获取更多详细信息。

贡献

我们欢迎所有人为PHPWord做出贡献。以下是一些您可以做的事情来贡献。