PHPWord
PHPWord copied to clipboard
XMLWriter incompatibility in the writeAttribute method in PHP 8.1.2
Describe the Bug
I am trying to use the library in Code Igniter 4, with PHP 8.1.2 but when I try to use XMLWritter an exception appears. I don't know if PHPWord is compatible with this php version or a downgrade is required. I tried using the 3 ways to save documents, only the HTML option works.
Exception has occurred.
ErrorException: Return type of PhpOffice\PhpWord\Shared\XMLWriter::writeAttribute($name, $value) should either be compatible with XMLWriter::writeAttribute(string $name, string $value): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice
Steps to Reproduce
I have only tested executing the example code, the IOFactory::createWriter() object does its function but when using the save()
method, the exception appears.
Code
namespace App\Controllers\API;
use CodeIgniter\RESTful\ResourceController;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style\Font;
class OfficeController extends ResourceController
{
//protected $office_word;
//protected $office_font;
public function __construct()
{
//$this->office_word = new PhpWord();
//$this->office_font = new Font();
}
public function index()
{
// 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('php://output');
//$objWriter->save('helloWorld.docx');
// Saving the document as ODF file...
//$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
//$objWriter->save('helloWorld.odt');
// Saving the document as HTML file...
//$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
//$objWriter->save('php://output');
}
}
Expected Behavior
Save document on server or expose via web browser.
Current Behavior
Function stopped by XMLWritter exception.
Context
Environment information:
- PHP Version: 8.1.2
- PHPWord Version: 0.18.3
------------------------------------------------------------------------
PHP 8.1.2 (cli) (built: Jan 19 2022 10:18:23) (ZTS Visual C++ 2019 x64)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
with Xdebug v3.1.3, Copyright (c) 2002-2022, by Derick Rethans
------------------------------------------------------------------------
[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
dom
exif
fileinfo
filter
ftp
gettext
hash
iconv
intl
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcre
PDO
pdo_mysql
pdo_sqlite
pdo_sqlsrv
Phar
readline
Reflection
session
SimpleXML
SPL
sqlsrv
standard
tokenizer
xdebug
xml
xmlreader
xmlwriter
zip
zlib
[Zend Modules]
Xdebug
For me, I get a "Deprecated" notice with the same text (when saving a Word document), which can be ignored (until PHP 9) by adding #[\ReturnTypeWillChange]
before the public function writeAttribute($name, $value)
declaration in ...\phpoffice\phpword\src\PhpWord\Shared\XMLWriter.php
.
So coming back to this, I now realize that the proper fix for this is actually to add the proper return type to the writeAttribute
function:
public function writeAttribute($name, $value): bool
Since it's actually calling parent::writeAttribute
, the return type will not change. The deprecation notice is only shown because the overloaded function doesn't tell PHP it will return a bool
.
When will this be fixed in future updates?
Is there any update on a fix for this? I must upgrade my web apps to php 8.1 before the end of May 2023