当前位置:网站首页>【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的完整解决方案,包括但不限于:
- 文档版本;
- 模板输出;
- 特殊格式和字符;
- 字体;
- 页眉页脚;
- 表单生成。
欢迎订阅本专栏。
边栏推荐
- Presto 中 lookUp Join的实现
- Mysql brush dirty several scenarios and related parameters
- CMake库搜索函数居然不搜索LD_LIBRARY_PATH
- Meta元宇宙部门第二季度亏损28亿!仍要继续押注?元宇宙发展尚未看到出路!
- What are the applications of X-rays?
- ESP8266-Arduino编程实例-DS18B20温度传感器驱动
- mysql刷脏的几种场景以及相关参数
- 【HarmonyOS】【ARK UI】HarmonyOS ets语言怎么实现双击返回键退出
- 好未来单季营收2.24亿美元:同比降84% 张邦鑫持股26.3%
- 载誉而归,重磅发布!润和软件亮相2022开放原子全球开源峰会
猜你喜欢
DTSE Tech Talk丨第2期:1小时深度解读SaaS应用系统设计
ESP8266-Arduino编程实例-BMP180气压温度传感器驱动
MySQL数据类型
5分钟搞懂MySQL - 行转列
EMC VPLEX VS2 SPS电池更换详细探讨
固定资产可视化智能管理系统
cocos creater 热更重启导致崩溃
Network Basics (3) 01-Basic Concepts of Networks - Protocols, Host Addresses, Paths and Parameters of URL Addresses & 127.0.0.1 Local Loopback Address & View URL IP Address and Access Ping Space + URL
网络基础(三)01-网络的基础概念——URL地址组成之协议、主机地址、路径和参数&127.0.0.1本地回环地址& 查看网址IP地址并访问之ping空格+网址&netstat -anb查看本机占用端口
载誉而归,重磅发布!润和软件亮相2022开放原子全球开源峰会
随机推荐
好未来单季营收2.24亿美元:同比降84% 张邦鑫持股26.3%
Linux-安装MySQL(详细教程)
CMake库搜索函数居然不搜索LD_LIBRARY_PATH
The sixteenth issue of eight-part article Balabala said (MQ)
Anaconda Navigator卡在loading applications
[Solved] The problem that Unity Hub fails to obtain a license or does not respond and cannot develop
LayaBox---TypeScript---基础数据类型
毕业1年从事软件测试拿下11.5k,没有给98后丢脸吧...
ESP8266-Arduino programming example-BMP180 air pressure temperature sensor driver
Immersive experience iFLYTEK 2022 Consumer Expo "Official Designated Product"
mysql的多实例
【开发者必看】【push kit】推送服务典型问题合集3
博纳影通过IPO注册:阿里腾讯是股东 受疫情冲击明显
宽带射频放大器OA4SMM4(1)
记者卧底
【AGC】增长服务2-应用内消息示例
ctf.show_web5
X射线的应用是什么?
Fixed asset visualization intelligent management system
What kind of framework is friendly to developers?