phpword1.1 / phpword
PHPWord - 一个纯PHP库,用于读取和写入文档处理文件(OOXML、ODF、RTF、HTML、PDF)
v1.1.0
2023-09-08 08:13 UTC
Requires
- php: ^7.1|^8.0
- ext-dom: *
- ext-json: *
- ext-xml: *
- laminas/laminas-escaper: >=2.6
Requires (Dev)
- ext-gd: *
- ext-libxml: *
- ext-zip: *
- dompdf/dompdf: ^2.0
- friendsofphp/php-cs-fixer: ^3.3
- mpdf/mpdf: ^8.1
- phpmd/phpmd: ^2.13
- phpunit/phpunit: >=7.0
- symfony/process: ^4.4
- tecnickcom/tcpdf: ^6.5
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 not auto-updated.
Last update: 2024-09-21 11:39:31 UTC
README
PHPWord是一个纯PHP编写的库,提供了一套类来读写不同的文档文件格式。当前版本的PHPWord支持微软的Office Open XML(OOXML或OpenXML)、OASIS的办公应用程序的开放文档格式(OpenDocument或ODF)、富文本格式(RTF)、HTML和PDF。
PHPWord是一个开源项目,遵循LGPL版本3许可条款。PHPWord通过集成持续集成和单元测试,旨在成为一个高质量的软件产品。您可以通过阅读开发者文档了解更多关于PHPWord的信息。
如果您有任何问题,请在StackOverFlow上提问
了解PHPWord的更多信息
功能
使用PHPWord,您可以使用PHP脚本动态地创建OOXML、ODF或RTF文档。以下是一些您可以使用PHPWord库执行的操作:
- 设置文档属性,例如标题、主题和创建者。
- 创建具有不同设置的文档部分,例如纵向/横向、页面大小和页码。
- 为每个部分创建页眉和页脚。
- 设置默认字体类型、字体大小和段落样式。
- 使用UTF-8和东亚字体/字符。
- 定义自定义字体样式(例如粗体、斜体、颜色)和段落样式(例如居中、多栏、间距),可以是命名样式或文本内联。
- 插入段落,可以是简单文本或复杂文本(文本运行),其中包含其他元素。
- 插入标题(页眉)和目录。
- 插入文本分页符和页码分页符。
- 插入并格式化图片,可以是本地、远程或作为页面水印。
- 插入二进制OLE对象,例如Excel或Visio。
- 插入并格式化表格,为每行(例如重复为标题行)和单元格(例如背景颜色、跨行、跨列)提供自定义属性。
- 插入项目符号、编号或多级列表项。
- 插入超链接。
- 插入脚注和尾注。
- 插入绘图形状(弧线、曲线、线条、多段线、矩形、椭圆形)。
- 插入图表(饼图、环形图、条形图、折线图、面积图、散点图、雷达图)。
- 插入表单字段(文本输入、复选框和下拉列表)。
- 从模板创建文档。
- 使用XSL 1.0样式表转换OOXML模板的页眉、主文档部分和页脚。
- ... 以及更多正在开发的功能
要求
PHPWord需要以下内容
- PHP 7.1+
- XML解析器扩展
- Laminas Escaper组件
- Zip扩展(可选,用于编写OOXML和ODF)
- GD扩展(可选,用于添加图片)
- XMLWriter扩展(可选,用于编写OOXML和ODF)
- XSL扩展(可选,用于将XSL样式表应用于模板)
- dompdf 库(可选,用于生成PDF文件)
安装
PHPWord通过Composer进行安装。要在项目中添加PHPWord的依赖项,可以:
运行以下命令以使用最新稳定版本:
composer require phpoffice/phpword
或者如果您需要最新未发布版本:
composer require phpoffice/phpword:dev-master
入门
以下是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做出贡献。以下是一些您可以做的贡献方式。