PHPWord
1.3.0
分會長:
PHPWord 是一個用純 PHP 編寫的函式庫,它提供了一組用於寫入和讀取不同文件檔案格式的類別。目前版本的 PHPWord 支援 Microsoft Office Open XML(OOXML 或 OpenXML)、OASIS Office 應用程式開放文件格式(OpenDocument 或 ODF)、富文本格式 (RTF)、HTML 和 PDF。
PHPWord 是一個根據 LGPL 第 3 版條款授權的開源專案。您可以透過閱讀開發人員文件來了解有關 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 做出貢獻。以下是您可以做的一些貢獻。