PHP实现PPT/PPTX转PDF遇加载错误,求PPT问题解决办法
PHP实现PPT/PPTX转PDF的问题与求助
背景与实现思路
我有大量PPT和PPTX文件,需要用PHP转换为PDF。基于PHPOffice已经实现了XLS/XLSX转PDF,但PHPPresentation的Writer功能远不如PhpSpreadsheet完善,因此尝试结合MPDF/Dompdf和Imagick编写了转换代码:
$objReader = PresentationIOFactory::createReader('PowerPoint2007'); $objPHPPresentation = $objReader->load($pf); // Create a new Imagick object for image manipulation $imagick = new \Imagick(); // Loop through each slide and convert it to an image $slideCount = $objPHPPresentation->getSlideCount(); for ($i = 0; $i < $slideCount; $i++) { $oSlide = $objPHPPresentation->getSlide($i); $oDrawing = new Drawing\Gd(); $oDrawing->setRenderingFunction(Drawing\Gd::RENDERING_PNG); $oDrawing->setImageResource($oSlide->render()); $oDrawing->setSize($oSlide->getExtent()->getWidth(), $oSlide->getExtent()->getHeight()); $oDrawing->setMimeType(Drawing\Gd::MIMETYPE_DEFAULT); $imageString = $oDrawing->getContents(); // Add the image to the Imagick object $imagick->readImageBlob($imageString); } // Create a new mPDF object $mpdf = new MpdfLib([ 'mode' => 'utf-8', 'format' => [$objPHPPresentation->getLayout()->getDocument()->getDocumentPart()->getExtent()->getWidth(), $objPHPPresentation->getLayout()->getDocument()->getDocumentPart()->getExtent()->getHeight()] ]); // Add the slide images to the mPDF object $imagick->resetIterator(); foreach ($imagick as $index => $slideImage) { // Output slide image as a base64-encoded data URI $dataUri = 'data:image/png;base64,' . base64_encode($slideImage->getImageBlob()); // Add the slide image to the mPDF object $mpdf->AddPage(); $mpdf->WriteHTML('<img src="' . $dataUri . '" />'); } // Save the PDF $mpdf->Output($outputFilePath, 'F'); echo "PowerPoint file has been converted to PDF.";
遇到的错误问题
PPTX文件加载错误
Fatal error: Uncaught TypeError:PhpOffice\PhpPresentation\Reader\PowerPoint2007::loadShapeRichText(): Argument #3 ($oSlide) must be of type PhpOffice\PhpPresentation\Slide\AbstractSlide, PhpOffice\PhpPresentation\Slide\Note given, called in C:\xampp\htdocs\vendor\phpoffice\phppresentation\src\PhpPresentation\Reader\PowerPoint2007.php on line 1375 and defined in C:\xampp\htdocs\vendor\phpoffice\phppresentation\src\PhpPresentation\Reader\PowerPoint2007.php:888 Stack trace: #0 C:\xampp\htdocs\vendor\phpoffice\phppresentation\src\PhpPresentation\Reader\PowerPoint2007.php(1375): PhpOffice\PhpPresentation\Reader\PowerPoint2007->loadShapeRichText(Object(PhpOffice\Common\XMLReader), Object(DOMElement), Object(PhpOffice\PhpPresentation\Slide\Note)) #1 C:\xampp\htdocs\vendor\phpoffice\phppresentation\src\PhpPresentation\Reader\PowerPoint2007.php(755): PhpOffice\PhpPresentation\Reader\PowerPoint2007->loadSlideShapes(Object(PhpOffice\PhpPresentation\Slide\Note), Object(DOMNodeList), Object(PhpOffice\Common\XMLReader)) #2 C:\xampp\htdocs\vendor\phpoffice\phppresentation\src\PhpPresentation\Reader\PowerPoint2007.php(369): PhpOffice\PhpPresentation\Reader\PowerPoint2007->loadSlideNote('notesSlide1.xml', Object(PhpOffice\PhpPresentation\Slide)) #3 C:\xampp\htdocs\vendor\phpoffice\phppresentation\src\PhpPresentation\Reader\PowerPoint2007.php(169): PhpOffice\PhpPresentation\Reader\PowerPoint2007->loadSlides('<?xml version="...') #4 C:\xampp\htdocs\vendor\phpoffice\phppresentation\src\PhpPresentation\Reader\PowerPoint2007.php(136): PhpOffice\PhpPresentation\Reader\PowerPoint2007->loadFile('C:/xampp/htdocs...') #5 C:\xampp\htdocs\a\create_thumbnail.php(70): PhpOffice\PhpPresentation\Reader\PowerPoint2007->load('C:/xampp/htdocs...') #6 C:\xampp\htdocs\a\create_thumbnail.php(142): getThumbnail('../uploads/pptx...', 'Results Discuss...', 'C:/xampp/htdocs...', '../uploads/tmp/', 'pptx', '126', Object(mysqli)) #7 {main} thrown in C:\xampp\htdocs\vendor\phpoffice\phppresentation\src\PhpPresentation\Reader\PowerPoint2007.php on line 888
PPT文件加载错误
Fatal error: Uncaught TypeError: PhpOffice\Common\Microsoft\OLERead::getStream(): Argument #1 ($stream) must be of type int, null given, called in C:\xampp\htdocs\vendor\phpoffice\phppresentation\src\PhpPresentation\Reader\PowerPoint97.php on line 487 and defined in C:\xampp\htdocs\vendor\phpoffice\common\src\Common\Microsoft\OLERead.php:203 Stack trace: #0 C:\xampp\htdocs\vendor\phpoffice\phppresentation\src\PhpPresentation\Reader\PowerPoint97.php(487): PhpOffice\Common\Microsoft\OLERead->getStream(NULL) #1 C:\xampp\htdocs\vendor\phpoffice\phppresentation\src\PhpPresentation\Reader\PowerPoint97.php(454): PhpOffice\PhpPresentation\Reader\PowerPoint97->loadOLE() #2 C:\xampp\htdocs\vendor\phpoffice\phppresentation\src\PhpPresentation\Reader\PowerPoint97.php(442): PhpOffice\PhpPresentation\Reader\PowerPoint97->loadFile() #3 C:\xampp\htdocs\vendor\phpoffice\phppresentation\src\PhpPresentation\IOFactory.php(73): PhpOffice\PhpPresentation\Reader\PowerPoint97->load('C:/xampp/htdocs...') #4 C:\xampp\htdocs\a\create_thumbnail.php(112): PhpOffice\PhpPresentation\IOFactory::load('C:/xampp/htdocs...') #5 C:\xampp\htdocs\a\create_thumbnail.php(177): getThumbnail('../uploads/ppt/...', 'Architecture_xx...', 'C:/xampp/htdocs...', '../uploads/tmp/', 'ppt', '139', Object(mysqli)) #6 {main} thrown in C:\xampp\htdocs\vendor\phpoffice\common\src\Common\Microsoft\OLERead.php on line 203
已修复的问题(PPTX)
通过修改PHPPresentation源码修复了PPTX加载问题,修改内容如下:
文件:vendor/phpoffice/phppresentation/src/PhpPresentation/Reader/PowerPoint2007.php
原代码:
protected function loadShapeRichText(XMLReader $document, DOMElement $node, AbstractSlide $oSlide): void { if (!$document->elementExists('p:txBody/a:p/a:r', $node)) { return; }
替换为:
protected function loadShapeRichText(XMLReader $document, DOMElement $node, $oSlide): void { if (!$document->elementExists('p:txBody/a:p/a:r', $node) || !$oSlide instanceof AbstractSlide) { return; }
文件:vendor/phpoffice/phppresentation/src/PhpPresentation/Style/Border.php
原代码:
public function setLineWidth(int $pValue = 1): self { $this->lineWidth = $pValue; }
替换为:
public function setLineWidth($pValue = 1): self { $this->lineWidth = (int) $pValue; }
求助需求
目前仍未解决PPT文件的加载错误,恳请提供共享主机环境下的有效解决办法。
内容的提问来源于stack exchange,提问作者Osama Hussein
相关产品推荐
相关产品推荐

