PHPWord
1.3.0
分会长:
PHPWord 是一个用纯 PHP 编写的库,它提供了一组用于写入和读取不同文档文件格式的类。当前版本的 PHPWord 支持 Microsoft Office Open XML(OOXML 或 OpenXML)、OASIS Office 应用程序开放文档格式(OpenDocument 或 ODF)、富文本格式 (RTF)、HTML 和 PDF。
PHPWord 是一个根据 LGPL 第 3 版条款许可的开源项目。PHPWord 旨在通过结合持续集成和单元测试成为高质量的软件产品。您可以通过阅读开发人员文档了解有关 PHPWord 的更多信息。
如果您有任何疑问,请在 StackOverFlow 上提问
了解有关 PHPWord 的更多信息:
借助 PHPWord,您可以使用 PHP 脚本动态创建 OOXML、ODF 或 RTF 文档。以下是您可以使用 PHPWord 库执行的一些操作:
PHPWord 需要以下内容:
PHPWord 通过 Composer 安装。要在项目中添加对 PHPWord 的依赖项,可以
运行以下命令以使用最新的稳定版本
composer require phpoffice/phpword
或者如果您想要最新的未发布版本
composer require phpoffice/phpword:dev-master
以下是 PHPWord 库的基本使用示例。
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
,然后浏览到 http://localhost:8000 以查看示例。您还可以阅读开发人员文档以了解更多详细信息。
我们欢迎大家为 PHPWord 做出贡献。以下是您可以做的一些贡献。