当前位置:网站首页>【PHPWord】PHPOffice 套件之PHPWord快速入门
【PHPWord】PHPOffice 套件之PHPWord快速入门
2022-07-30 18:16:00 【小雨青年】
一、简介
PHPWord 是一个用纯 PHP 编写的库,它提供了一组用于写入和读取不同文档文件格式的类。 当前版本的 PHPWord 支持 Microsoft Office Open XML(OOXML 或 OpenXML)、用于 Office 应用程序的 OASIS 开放文档格式(OpenDocument 或 ODF)、富文本格式 (RTF)、HTML 和 PDF。

项目地址:https://github.com/PHPOffice/PHPWord
二、开源协议
PHPWord 是根据 LGPL 第 3 版条款获得许可的开源项目。
那么,什么是 LGPL 第 3 版呢?
GNU宽通用公共许可证(英语:GNU Lesser General Public License,简称:LGPL)是由自由软件基金会公布的自由软件授权条款。它允许企业与软件开发者使用,或将LGPL授权的软件整合至他们自己的软件内(即使该软件是私有软件也被允许),同时不会受到Copyleft特性的许可证强制对软件开源的限制。该许可证常被用于一些(但不是全部)GNU程序库。
一言蔽之,可以作为商业软件的类库,但是不能二次修改当做闭源商品来卖。
LGPL协议官网地址: https://www.gnu.org/licenses/lgpl-3.0.html
三、安装要求
- PHP 5.3.3+
- XML Parser extension
- Laminas Escaper component
- Zip extension (可选,用于编写 OOXML 和 ODF)
- GD extension (可选,用于添加图片)
- XMLWriter extension (可选,用于编写 OOXML 和 ODF)
- XSL extension (可选,用于将 XSL 样式表应用到模板 )
- dompdf library (可选,用于编写PDF)
四、快速入门
1. 安装
composer require phpoffice/phpword
2. demo
// 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');
输出结果如下图所示。

我们可以看到PHPWord是可以做一系列格式化的操作的,这对实际的业务非常重要。
五、总结
本文为PHPWord的入门介绍,本专栏后续会解决实际业务中需要用到PHPWord的完整解决方案,包括但不限于:
- 文档版本;
- 模板输出;
- 特殊格式和字符;
- 字体;
- 页眉页脚;
- 表单生成。
欢迎订阅本专栏。
边栏推荐
猜你喜欢

银行适用:此文能够突破你的运维流程管理问题

CCNA-ACL(访问控制列表)标准ACL 扩展ACL 命名ACL

高性能短链设计

EMC VPLEX VS2 SPS电池更换详细探讨

LeetCode Exercise - Two Questions About Finding Sum of Array Elements

ESP8266-Arduino编程实例-BMP180气压温度传感器驱动

网络基础(二)-Web服务器-简介——WampServer集成服务器软件之Apache+MySQL软件安装流程 & netstat -an之检测计算机的端口是否占用

【HMS core】【FAQ】Account Kit、MDM能力、push Kit典型问题合集6

Quickly build an e-commerce platform based on Amazon cloud technology serverless service - performance

js中的基础知识点 —— BOM
随机推荐
【HMS core】【FAQ】Account Kit、MDM能力、push Kit典型问题合集6
怎么样的框架对于开发者是友好的?
LayaBox---TypeScript---枚举
千亿级、大规模:腾讯超大 Apache Pulsar 集群性能调优实践
Anaconda Navigator卡在loading applications
高性能短链设计
微博广告分布式配置中心的构建与实践(有彩蛋)
MySQL中的存储过程(详细篇)
core sound driver详解
Kettle--MySQL生产数据库千万、亿级数据量迁移方案及性能优化
LayaBox---TypeScript---基础数据类型
【HMS core】【Analytics Kit】【FAQ】如何解决华为分析付费分析中付款金额显示为0的问题?
LayaBox---TypeScript---类型推论
ESP8266-Arduino编程实例-HC-SR04超声波传感器驱动
图解LeetCode——11. 盛最多水的容器(难度:中等)
线性筛求积性函数
积性函数
Presto 中 lookUp Join的实现
LayaBox---TypeScript---类型兼容性
leetcode-684:冗余连接