当前位置:网站首页>【PHPWord】Quick Start of PHPWord in PHPOffice Suite
【PHPWord】Quick Start of PHPWord in PHPOffice Suite
2022-07-30 18:25:00 【light rain youth】
一、简介
PHPWord 是一个用纯 PHP 编写的库,It provides a set of classes for writing and reading different document file formats. 当前版本的 PHPWord 支持 Microsoft Office Open XML(OOXML 或 OpenXML)、用于 Office 应用程序的 OASIS 开放文档格式(OpenDocument 或 ODF)、富文本格式 (RTF)、HTML 和 PDF.

项目地址:https://github.com/PHPOffice/PHPWord
二、开源协议
PHPWord 是根据 LGPL 第 3 Open source project licensed under the terms of the version.
那么,什么是 LGPL 第 3 版呢?
GNU宽通用公共许可证(英语:GNU Lesser General Public License,简称:LGPL)Free software license terms published by the Free Software Foundation.It allows businesses and software developers to use,或将LGPLLicensed software is integrated into their own software(This is allowed even if the software is proprietary),will not receiveCopyleftA feature's license enforces restrictions on open source software.This license is often used for some(但不是全部)GNU程序库.
一言蔽之,Can be used as a class library for commercial software,However, it cannot be re-modified and sold as a closed-source product.
LGPLThe official website address of the agreement: 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 The style sheet is applied to the template )
- 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');
输出结果如下图所示.

我们可以看到PHPWordA series of formatting operations can be performed,This is very important for actual business.
五、总结
本文为PHPWord的入门介绍,This column will address the needs of actual business laterPHPWord的完整解决方案,包括但不限于:
- 文档版本;
- 模板输出;
- Special formats and characters;
- 字体;
- 页眉页脚;
- 表单生成.
欢迎订阅本专栏.
边栏推荐
猜你喜欢

猎豹移动终于递交年报:年营收7.85亿 腾讯持股16.6%

NC | Tao Liang Group of West Lake University - TMPRSS2 "assists" virus infection and mediates the host invasion of Clostridium sothrix hemorrhagic toxin...

沃尔沃中国的年中总结,在“安全感”中寻找未来

好未来单季营收2.24亿美元:同比降84% 张邦鑫持股26.3%

SQL行列转换

AI Basics: Graphical Transformer

WeChat Mini Program Cloud Development | Urban Information Management

cocos creater 热更重启导致崩溃

智慧中控屏

高性能短链设计
随机推荐
What ARC does at compile time and runtime
Anaconda Navigator卡在loading applications
The use of terminal split screen tool Terminalx
LayaBox---TypeScript---类
Graphic LeetCode -- 11. Containers of most water (difficulty: medium)
arcpy获取要素类(属性表)包含的数目
强啊,点赞业务缓存设计优化探索之路。
载誉而归,重磅发布!润和软件亮相2022开放原子全球开源峰会
沃尔沃中国的年中总结,在“安全感”中寻找未来
时序数据库在船舶风险管理领域的应用
智慧中控屏
CMake library search function does not search LD_LIBRARY_PATH
core sound driver详解
中集世联达工业级成熟航运港口人工智能AI产品规模化应用,打造新一代高效能智慧港口和创新数字港口,全球港航人工智能能领军者中集飞瞳
MYSQL(基本篇)——一篇文章带你走进MYSQL的奇妙世界
中集世联达飞瞳全球工业人工智能AI领军者,全球顶尖AI核心技术高泛化性高鲁棒性稀疏样本持续学习,工业级高性能成熟AI产品规模应用
ROS 环境使用第三方动态链接库(.so)文件
国轩高科瑞交所上市:募资近7亿美元 为瑞士今年最大融资项目
【Swords Offer】Swords Offer 17. Print n digits from 1 to the largest
Mongo for infrastructure