mr-yuksekdam/php-word-fixed

更新 PhpWord,修复了自动编号目录问题

v1.0.0 2023-09-08 14:51 UTC

This package is auto-updated.

Last update: 2024-09-08 17:20:21 UTC


README

Latest Stable Version Coverage Status Total Downloads License CI Join the chat at https://gitter.im/PHPOffice/PHPWord

PHPWord 是一个纯 PHP 编写的库,提供了一套用于写入和读取不同文档文件格式的类。当前版本的 PHPWord 支持 Microsoft Office Open XML (OOXML 或 OpenXML)、OASIS Open Document Format for Office Applications (OpenDocument 或 ODF)、Rich Text Format (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 需要以下条件

安装

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 做出贡献。以下是一些您可以做的事情来做出贡献。