Home  >  Article  >  Backend Development  >  How to convert php to word

How to convert php to word

PHPz
PHPzOriginal
2023-04-10 09:35:19785browse

With the development of the Internet, more and more people need to convert website content into Word documents for offline viewing or printing. As a commonly used web programming language, PHP is loved by the majority of developers. In this article, we will explore how to use PHP to convert website content into Word documents.

First of all, we need to make it clear that a Word document is a binary file and its format is not public. Therefore, in order to generate and edit Word files, we need to use the PHPWord library. PHPWord is a PHP class library that can generate Microsoft Word docx documents. The library can be installed through Composer. The installation command is as follows:

composer require phpoffice/phpword

After installation, we can create a basic Word document through the following code:

require_once 'vendor/autoload.php';

$phpWord = new \PhpOffice\PhpWord\PhpWord();

$section = $phpWord->addSection();

$text = 'Hello World!';
$section->addText($text);

$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('hello-world.docx');

The above code does the following:

  • Introduce the autoload.php file, which contains all the required classes.
  • Created a new PhpWord instance.
  • A new Section instance has been added to represent a paragraph in a Word document.
  • Added text content.
  • Created a Word2007 writer to create docx file format.
  • Save the document.

Now that we have successfully generated a simple Word document, we need to convert the website content into a Word document.

Taking a simple blog post as an example, we need to convert the title, content, author, publication time and other information of the article into Word. The code is as follows:

require_once 'vendor/autoload.php';

use PhpOffice\PhpWord\PhpWord;

// 模拟博客文章数据
$title = 'PHP 转 Word';
$content = '本文介绍了如何使用 PHPWord 库将网站内容转换成 Word 文档。';
$author = 'PHP高手';
$date = '2021年11月1日';

// 初始化一个 PhpWord 实例
$phpWord = new PhpWord();

// 添加一个空段落
$section = $phpWord->addSection();

// 标题
$section->addText($title, ['size' => 24, 'bold' => true]);

// 作者和发布时间
$section->addText($author . ' ' . $date, ['size' => 12, 'italic' => true]);

// 内容
$section->addText($content);

// 保存为 Word 文档
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('php-to-word.docx');

The execution result of this code is: a Word document named php-to-word.docx is generated, which contains the title, content, and author of the blog article and release time. As you can see, it is very easy to use the PHPWord library to generate Word documents. You only need to call the corresponding API.

In summary, we have realized the function of converting website content into Word documents by using the PHPWord library. This technology can be applied to various scenarios where website content needs to be exported to Word documents.

The above is the detailed content of How to convert php to word. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn