Detailed explanation of the method of generating word document with PHPWord
- 2021-12-12 03:57:53
- OfStack
This article illustrates the method of generating word documents using PHPWord. Share it for your reference, as follows:
Sometimes we need to save web content as an Word document format for other people to view and edit. PHPWord is a pure PHP written library, PHPWord can easily deal with word document content, you want to generate word document.
Download source code
Installation
We use Composer to install PHPWord.
composer require phpoffice/phpword
How to use
Automatic loading
After installing phpword, create a new php document and introduce autoload. php.
require 'vendor/autoload.php';
Instantiation
Instantiate and add 1 blank page.
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
Add text content
Add text content to blank pages. You can set the style of text, including font, color, font size, bold and so on.
$fontStyle = [
'name' => 'Microsoft Yahei UI',
'size' => 20,
'color' => '#ff6600',
'bold' => true
];
$textrun = $section->addTextRun();
$textrun->addText(' Hello, this is generated Word Documents. ', $fontStyle);
Link
You can add links for clicking and jumping to the text in Word documents.
$section->addLink('https://www.helloweba.net', ' Welcome to visit Helloweba', array('color' => '0000FF', 'underline' => \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE));
$section->addTextBreak();
Picture
You can add pictures to word, such as the picture address logo. png, size 64x64. The picture source can also be a remote picture.
$section->addImage('logo.png', array('width'=>64, 'height'=>64));
Header
Add a header to the Word document.
$header = $section->addHeader();
$header->addText('Subsequent pages in Section 1 will Have this!');
Footer
Add a footer to the word document. The footer content is the page number and the format is centered.
$footer = $section->addFooter();
$footer->addPreserveText('Page {PAGE} of {NUMPAGES}.', null, array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER));
Add 1 page
Continue to add 1 page and add content.
$section = $phpWord->addSection();
$section->addText(' New 1 Page .');
Forms
Add 1 basic table, and you can set the style of the table.
$header = array('size' => 16, 'bold' => true);
$rows = 10;
$cols = 5;
$section->addText('Basic table', $header);
$table = $section->addTable();
for ($r = 1; $r <= 8; $r++) {
$table->addRow();
for ($c = 1; $c <= 5; $c++) {
$table->addCell(1750)->addText("Row {$r}, Cell {$c}");
}
}
Generate an Word document
If you want to generate word documents and put them on the server, you can use:
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('hellwoeba.docx');
Download the Word documentation
If you want to download Word documents directly without saving them on the server, you can use:
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
0
The above code forces the browser to download the word document.
For more information on PHPWord, please refer to the PHPWord document: http://phpword.readthedocs.org/.
For more readers interested in PHP related content, please check the special topics of this site: "Summary of php Operating office Document Skills (including word, excel, access, ppt)", "Encyclopedia of PHP Array (Array) Operation Skills", "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary", "PHP Mathematical Operation Skills Summary", "php Regular Expression Usage Summary", "php String (string) Usage Summary" and "php Common Database Operation Skills Summary"
I hope this article is helpful to everyone's PHP programming.