当前位置:网站首页>【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;
- 字体;
- 页眉页脚;
- 表单生成.
欢迎订阅本专栏.
边栏推荐
- 原生js系列
- [Summary] 1396- 60+ VSCode plugins to create a useful editor
- Multiple instances of mysql
- 【Pointing to Offer】Pointing to Offer 22. The kth node from the bottom in the linked list
- Mysql执行原理剖析
- WeChat Mini Program Cloud Development | Urban Information Management
- AWS console
- Codeblocks + Widgets create window code analysis
- Chapter 14 Type Information
- Critical Reviews | 南农邹建文组综述全球农田土壤抗生素与耐药基因分布
猜你喜欢

NC | 西湖大学陶亮组-TMPRSS2“助攻”病毒感染并介导索氏梭菌出血毒素的宿主入侵...

强啊,点赞业务缓存设计优化探索之路。

Graphic LeetCode -- 11. Containers of most water (difficulty: medium)

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

SQL行列转换

Pagoda builds PHP adaptive lazy website navigation source code measurement

Deepen school-enterprise cooperation and build an "overpass" for the growth of technical and skilled talents

Immersive experience iFLYTEK 2022 Consumer Expo "Official Designated Product"

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

Linux-安装MySQL(详细教程)
随机推荐
Two-point answer naked question (plus a little pigeonhole principle)
core sound driver详解
ctf.show_web5
SQL行列转换
SwiftUI iOS 精品开源项目之 完整烘焙食品菜谱App基于SQLite(教程含源码)
Fixed asset visualization intelligent management system
荐书 | 推荐好评如潮的3本数据库书籍
Vulkan与OpenGL对比——Vulkan的全新渲染架构
中集世联达工业级成熟航运港口人工智能AI产品规模化应用,打造新一代高效能智慧港口和创新数字港口,全球港航人工智能能领军者中集飞瞳
Scrapy framework is introduced
while,do while,for循环语句
基于b/s架构搭建一个支持多路摄像头的实时处理系统 ---- 使用yolo v5 系列模型
OneFlow源码解析:Op、Kernel与解释器
NC | 西湖大学陶亮组-TMPRSS2“助攻”病毒感染并介导索氏梭菌出血毒素的宿主入侵...
ESP8266-Arduino编程实例-BMP180气压温度传感器驱动
【HMS core】【Analytics Kit】【FAQ】如何解决华为分析付费分析中付款金额显示为0的问题?
ESP8266-Arduino编程实例-HC-SR04超声波传感器驱动
AWS 控制台
【HMS Core】【FAQ】运动健康、音频编辑、华为帐号服务 典型问题合集7
终端分屏工具Terminalx的使用