nandodutra / phpword
PHPWord - 一个用于读取和写入文档处理文件(DOCX, ODT, RTF, HTML, PDF)的纯PHP库
Requires
- php: >=5.3.3
- ext-xml: *
Requires (Dev)
- dompdf/dompdf: 0.6.*
- mpdf/mpdf: 5.*
- phpdocumentor/phpdocumentor: 2.*
- phploc/phploc: 2.*
- phpmd/phpmd: 2.*
- phpunit/phpunit: 3.7.*
- sebastian/phpcpd: 2.*
- squizlabs/php_codesniffer: 1.*
- tecnick.com/tcpdf: 6.*
Suggests
- ext-gd2: Used to add images
- ext-xmlwriter: Used to write DOCX and ODT
- ext-xsl: Used to apply XSL style sheet to main document part of OOXML template
- ext-zip: Used to write DOCX and ODT
- dompdf/dompdf: Used to write PDF
README
PHPWord 是一个用纯PHP编写的库,提供了一组用于读写不同文档文件格式的类。PHPWord 的当前版本支持微软的 Office Open XML(OOXML 或 OpenXML)、OASIS Open Document Format for Office Applications(OpenDocument 或 ODF)、Rich Text Format(RTF)、HTML 和 PDF。
PHPWord 是一个开源项目,许可协议为 LGPL version 3。PHPWord 通过整合 持续集成 和 单元测试,旨在成为一款高质量软件产品。您可以阅读 开发者文档 和 API 文档 了解更多关于 PHPWord 的信息。
了解更多关于 PHPWord 的信息
特性
使用 PHPWord,您可以使用 PHP 5.3+ 脚本动态创建 DOCX、ODT 或 RTF 文档。以下是一些您可以使用 PHPWord 库完成的事情
- 设置文档属性,例如标题、主题和创建者。
- 创建具有不同设置的文档部分,例如纵向/横向、页面大小和页码
- 为每个部分创建页眉和页脚
- 设置默认字体类型、字体大小和段落样式
- 使用 UTF-8 和东亚字体/字符
- 定义自定义字体样式(例如加粗、斜体、颜色)和段落样式(例如居中、多列、间距),可以是命名样式或文本中的内联样式
- 插入段落,可以是简单文本或复杂文本(文本运行),其中包含其他元素
- 插入标题(页眉)和目录
- 插入文本分页符和页码分页符
- 插入和格式化图像,可以是本地、远程或作为页面水印
- 插入二进制 OLE 对象,如 Excel 或 Visio
- 插入和格式化表格,为每行(例如重复作为页眉行)和单元格(例如背景颜色、跨行、跨列)自定义属性
- 插入列表项,可以是项目符号、编号或多层次
- 插入超链接
- 插入脚注和尾注
- 插入绘图形状(弧线、曲线、线、折线、矩形、椭圆形)
- 插入图表(饼图、环形图、柱状图、折线图、面积图、散点图、雷达图)
- 插入表单字段(文本输入、复选框和下拉菜单)
- 从模板创建文档
- 使用 XSL 1.0 样式表转换 OOXML 模板的主要文档部分
- ... 以及更多正在开发中的特性
需求
PHPWord 需要以下内容
- PHP 5.3+
- Zip 扩展
- XML 解析器扩展
- GD 扩展(可选,用于添加图像)
- XMLWriter 扩展(可选,用于写入 DOCX 和 ODT)
- XSL 扩展(可选,用于将 XSL 样式表应用于模板)
- dompdf(可选,用于生成PDF文件)
安装
建议您通过composer安装PHPWord库。这样做,您需要在您的composer.json
文件中添加以下行。
{ "require": { "phpoffice/phpword": "dev-master" } }
或者,您可以从发布页面下载最新版本。在这种情况下,您需要注册自动加载器。
require_once 'path/to/PhpWord/src/PhpWord/Autoloader.php'; \PhpOffice\PhpWord\Autoloader::register();
入门
以下为PHPWord库的基本使用示例。
<?php require_once 'src/PhpWord/Autoloader.php'; \PhpOffice\PhpWord\Autoloader::register(); // 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( htmlspecialchars( '"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( htmlspecialchars( '"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( htmlspecialchars( '"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( htmlspecialchars('"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. */
⚠️ 在传递给OOXML/ODF/HTML文档的任何字符串之前,请先对其进行转义,否则可能会损坏。
更多示例请参考示例文件夹。您还可以阅读开发者文档和API文档以获取更多详细信息。
贡献
我们欢迎 everyone 为 PHPWord 做出贡献。以下是一些您可以做的事情来做出贡献。