PHPOffice / PHPPresentation

A pure PHP library for reading and writing presentations documents
https://phpoffice.github.io/PHPPresentation
Other
1.31k stars 523 forks source link

Float passed to setSize on load() causing exception #740

Closed websuasive closed 1 year ago

websuasive commented 1 year ago

Exception thrown trying to load a presentation due to $oElementLvlDefRPR->getAttribute('sz') / 100) passing a float to $oRTParagraph->getFont()->setSize().

Exception:

PhpOffice\PhpPresentation\Style\Font::setSize(): Argument #1 ($pValue) must be of type int, float given, called in /var/www/html/vendor/phpoffice/phppresentation/src/PhpPresentation/Reader/PowerPoint2007.php on line 559 {"cx":3,"userId":3,"exception":"[object] (TypeError(code: 0): PhpOffice\PhpPresentation\Style\Font::setSize(): Argument #1 ($pValue) must be of type int, float given, called in /var/www/html/vendor/phpoffice/phppresentation/src/PhpPresentation/Reader/PowerPoint2007.php on line 560 at /var/www/html/vendor/phpoffice/phppresentation/src/PhpPresentation/Style/Font.php:206)

[stacktrace]

0 /var/www/html/vendor/phpoffice/phppresentation/src/PhpPresentation/Reader/PowerPoint2007.php(559): PhpOffice\PhpPresentation\Style\Font->setSize()

1 /var/www/html/vendor/phpoffice/phppresentation/src/PhpPresentation/Reader/PowerPoint2007.php(397): PhpOffice\PhpPresentation\Reader\PowerPoint2007->loadMasterSlide()

2 /var/www/html/vendor/phpoffice/phppresentation/src/PhpPresentation/Reader/PowerPoint2007.php(354): PhpOffice\PhpPresentation\Reader\PowerPoint2007->loadMasterSlides()

3 /var/www/html/vendor/phpoffice/phppresentation/src/PhpPresentation/Reader/PowerPoint2007.php(170): PhpOffice\PhpPresentation\Reader\PowerPoint2007->loadSlides()

4 /var/www/html/vendor/phpoffice/phppresentation/src/PhpPresentation/Reader/PowerPoint2007.php(137): PhpOffice\PhpPresentation\Reader\PowerPoint2007->loadFile()

5 /var/www/html/app/Helpers/ParsePptx.php(35): PhpOffice\PhpPresentation\Reader\PowerPoint2007->load()

The code with issue:

$oElementLvlDefRPR = $xmlReader->getElement('a:defRPr', $oElementLvl); if ($oElementLvlDefRPR instanceof DOMElement) { if ($oElementLvlDefRPR->hasAttribute('sz')) { $oRTParagraph->getFont()->setSize($oElementLvlDefRPR->getAttribute('sz') / 100); <=============== } if ($oElementLvlDefRPR->hasAttribute('b') && 1 == $oElementLvlDefRPR->getAttribute('b')) { $oRTParagraph->getFont()->setBold(true); } if ($oElementLvlDefRPR->hasAttribute('i') && 1 == $oElementLvlDefRPR->getAttribute('i')) { $oRTParagraph->getFont()->setItalic(true); } }

Is the fix as simple as:

oRTParagraph->getFont()->setSize((int) round($oElementLvlDefRPR->getAttribute('sz') / 100,0));

websuasive commented 1 year ago

Realised duplicate of issue #738