Skip to content

XMLWriter incompatibility in the writeAttribute method in PHP 8.1.2 #2204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
KFRANCMM opened this issue Mar 10, 2022 · 4 comments · May be fixed by #2776
Open

XMLWriter incompatibility in the writeAttribute method in PHP 8.1.2 #2204

KFRANCMM opened this issue Mar 10, 2022 · 4 comments · May be fixed by #2776
Milestone

Comments

@KFRANCMM
Copy link

KFRANCMM commented Mar 10, 2022

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.
imagen
imagen

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
@KFRANCMM KFRANCMM changed the title XMLWriter exception incopatibility in writeAttribute method in PHP 8.1.2 XMLWriter incompatibility in the writeAttribute method in PHP 8.1.2 Mar 18, 2022
@goozak
Copy link

goozak commented Apr 28, 2022

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.

@goozak
Copy link

goozak commented Oct 24, 2022

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.

@tw-ict
Copy link

tw-ict commented Dec 20, 2022

When will this be fixed in future updates?

@kilvroch
Copy link

kilvroch commented May 8, 2023

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

@Progi1984 Progi1984 added this to the 1.3.0 milestone Aug 22, 2024
@Progi1984 Progi1984 modified the milestones: 1.3.0, 1.3.1 Sep 1, 2024
@radarhere radarhere linked a pull request Apr 25, 2025 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

5 participants