yesgooo / phpword
PHPWord - 用于读取和写入办公文档(OOXML、ODF、RTF、HTML、PDF)的纯PHP库
Requires
- php: ^5.3.3 || ^7.0
- ext-xml: *
- phpoffice/common: ^0.2.9
- zendframework/zend-escaper: ^2.2
Requires (Dev)
- ext-gd: *
- ext-zip: *
- dompdf/dompdf: 0.8.*
- friendsofphp/php-cs-fixer: ^2.2
- mpdf/mpdf: 5.7.4 || 6.* || 7.*
- php-coveralls/php-coveralls: 1.1.0 || ^2.0
- phploc/phploc: 2.* || 3.* || 4.*
- phpmd/phpmd: 2.*
- phpunit/phpunit: ^4.8.36 || ^7.0
- squizlabs/php_codesniffer: ^2.9
- tecnickcom/tcpdf: 6.*
Suggests
- ext-gd2: Allows adding images
- ext-xmlwriter: Allows writing OOXML and ODF
- ext-xsl: Allows applying XSL style sheet to headers, to main document part, and to footers of an OOXML template
- ext-zip: Allows writing OOXML and ODF
- dompdf/dompdf: Allows writing PDF
This package is auto-updated.
Last update: 2024-09-25 14:02:12 UTC
README
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 需要以下条件
- PHP 5.3.3+
- XML 解析器扩展
- Zend\Escaper 组件
- Zend\Stdlib 组件
- Zip 扩展(可选,用于编写 OOXML 和 ODF)
- GD 扩展(可选,用于添加图像)
- XMLWriter 扩展(可选,用于编写 OOXML 和 ODF)
- XSL 扩展(可选,用于将 XSL 样式表应用于模板)
- dompdf 库(可选,用于编写 PDF)
安装
PHPWord 通过 Composer 安装。要在您的项目中添加 PHPWord 依赖项,您可以
运行以下命令以使用最新稳定版本
composer require phpoffice/phpword
或者如果您想使用最新 master 版本
composer require phpoffice/phpword:dev-master
当然,您也可以手动编辑 composer.json 文件
{
"require": {
"phpoffice/phpword": "v0.16.*"
}
}
入门
以下是一个 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. */
更多示例在 samples 文件夹 中提供。要轻松访问这些示例,请在 samples 目录中运行 php -S localhost:8000
,然后浏览到 https://:8000 以查看示例。您还可以阅读 开发者文档 获取更多详细信息。
贡献
我们欢迎每个人都为 PHPWord 做出贡献。以下是一些您可以做的事情来做出贡献。
- 阅读 我们的贡献指南。
- Fork 我们 并 向 develop 分支请求合并。
- 向 GitHub 提交 错误报告或功能请求。
- 关注 @PHPWord 和 @PHPOffice 在 Twitter 上。