PHP,需要使用PhpOffice\PhpPresentation库生成PPT文件,我的问题是,是否可以使用默认模板生成文件?

ztmd8pv5  于 10个月前  发布在  PHP
关注(0)|答案(1)|浏览(82)

需要在PHP中使用PhpOffice\PhpPresentation库生成PPT。
我可以使用文本生成文件。
下面提到了我的问题:
1.是否可以使用模板或图像作为背景生成?
1.如何生成HTML内容,如表格和设置背景颜色。
下面提到了我目前的代码供大家参考。

<?php
                require 'vendor/autoload.php';

                use PhpOffice\PhpPresentation\PhpPresentation;
                use PhpOffice\PhpPresentation\IOFactory;

                // Create a new presentation
                $presentation = new PhpPresentation();

                // Add the first slide
                $firstSlide = $presentation->getActiveSlide();

                // Set the slide background color (optional)
                $backgroundShape = $firstSlide->createRichTextShape();
                $backgroundShape->setHeight(720);
                $backgroundShape->setWidth(960);
                $backgroundShape->getActiveParagraph()->getAlignment()->setHorizontal(\PhpOffice\PhpPresentation\Style\Alignment::HORIZONTAL_CENTER);
                $backgroundShape->getActiveParagraph()->getAlignment()->setVertical(\PhpOffice\PhpPresentation\Style\Alignment::VERTICAL_CENTER);
                $backgroundShape->getFill()->setFillType(\PhpOffice\PhpPresentation\Style\Fill::FILL_SOLID);
                $backgroundShape->getFill()->getStartColor()->setRGB('FFFFFF');

                // Add dynamic content to the first slide
                $textShape = $firstSlide->createRichTextShape();
                $textShape->setHeight(300);
                $textShape->setWidth(600);
                $textShape->setOffsetX(220);
                $textShape->setOffsetY(60);

                // Add a text run to the shape
                $textRun = $textShape->createTextRun('WELCOME TO');
                $textRun->getFont()->setSize(48);
                $textRun->getFont()->setBold(true);
                $textRun->getFont()->setColor(new \PhpOffice\PhpPresentation\Style\Color('00000'));

                $imagePath = 'bnilogo.png';
                $shape = $firstSlide->createDrawingShape();
                $shape->setName('Image');
                $shape->setDescription('Image Description');
                $shape->setPath($imagePath);
                $shape->setWidth(300);
                $shape->setHeight(150);
                $shape->setOffsetX(300);
                $shape->setOffsetY(200);

                // Add the second slide
                $secondSlide = $presentation->createSlide();

                // Set the slide background color (optional)
                $backgroundShape = $secondSlide->createRichTextShape();
                $backgroundShape->setHeight(720);
                $backgroundShape->setWidth(960);
                $backgroundShape->getActiveParagraph()->getAlignment()->setHorizontal(\PhpOffice\PhpPresentation\Style\Alignment::HORIZONTAL_CENTER);
                $backgroundShape->getActiveParagraph()->getAlignment()->setVertical(\PhpOffice\PhpPresentation\Style\Alignment::VERTICAL_CENTER);
                $backgroundShape->getFill()->setFillType(\PhpOffice\PhpPresentation\Style\Fill::FILL_SOLID);
                $backgroundShape->getFill()->getStartColor()->setRGB('FF5733');

                // Add dynamic content to the second slide
                $textShape = $secondSlide->createRichTextShape();
                $textShape->setHeight(300);
                $textShape->setWidth(600);
                $textShape->setOffsetX(180);
                $textShape->setOffsetY(150);

                // Add a text run to the shape
                $htmlcontent = "WELCOME TO PHP WORLD";
                //$textRun = $textShape->createTextRun($htmlcontent);

                $textRun = $textShape->createTextRun('Dynamic Text with Styles');
                //$textRun->getFont()->setSize(28);
                $textRun->getFont()->setBold(true);
                //$textRun->getFont()->setColor(new \PhpOffice\PhpPresentation\Style\Color('FF5733'));
                $textRun->getFont()->setItalic(true);
                $textRun->getFont()->setUnderline(true);
                $textRun->getFont()->setStrikethrough(true);
                $textRun->getFont()->setName('Arial');

                 $textRun->getFont()->setSize(48);
                 $textRun->getFont()->setColor(new \PhpOffice\PhpPresentation\Style\Color('FFFFFF'));

                // Add the third slide
                $thirdSlide = $presentation->createSlide();

                // Set the slide background color (optional)
                $backgroundShape = $thirdSlide->createRichTextShape();
                $backgroundShape->setHeight(720);
                $backgroundShape->setWidth(960);
                $backgroundShape->getActiveParagraph()->getAlignment()->setHorizontal(\PhpOffice\PhpPresentation\Style\Alignment::HORIZONTAL_CENTER);
                $backgroundShape->getActiveParagraph()->getAlignment()->setVertical(\PhpOffice\PhpPresentation\Style\Alignment::VERTICAL_CENTER);
                $backgroundShape->getFill()->setFillType(\PhpOffice\PhpPresentation\Style\Fill::FILL_SOLID);
                $backgroundShape->getFill()->getStartColor()->setRGB('33FF57');

                // Add dynamic content to the third slide
                $textShape = $thirdSlide->createRichTextShape();
                $textShape->setHeight(300);
                $textShape->setWidth(600);
                $textShape->setOffsetX(180);
                $textShape->setOffsetY(150);

                // Add a text run to the shape
                $textRun = $textShape->createTextRun('Dynamic Text on the Third Slide');
                $textRun->getFont()->setSize(48);
                $textRun->getFont()->setColor(new \PhpOffice\PhpPresentation\Style\Color('FFFFFF'));

                // Add more slides with dynamic content (optional)
                $fourthSlide = $presentation->createSlide();

                // Set the slide background color (optional)
                $backgroundShape = $fourthSlide->createRichTextShape();
                $backgroundShape->setHeight(720);
                $backgroundShape->setWidth(960);
                $backgroundShape->getActiveParagraph()->getAlignment()->setHorizontal(\PhpOffice\PhpPresentation\Style\Alignment::HORIZONTAL_CENTER);
                $backgroundShape->getActiveParagraph()->getAlignment()->setVertical(\PhpOffice\PhpPresentation\Style\Alignment::VERTICAL_CENTER);
                $backgroundShape->getFill()->setFillType(\PhpOffice\PhpPresentation\Style\Fill::FILL_SOLID);
                $backgroundShape->getFill()->getStartColor()->setRGB('33FF57');

                // Add dynamic content to the third slide
                $textShape = $fourthSlide->createRichTextShape();
                $textShape->setHeight(300);
                $textShape->setWidth(600);
                $textShape->setOffsetX(180);
                $textShape->setOffsetY(150);

                // Add a text run to the shape
                $textRun = $textShape->createTextRun('Dynamic Text on the fourth Slide');
                $textRun->getFont()->setSize(48);
                $textRun->getFont()->setColor(new \PhpOffice\PhpPresentation\Style  \Color('FFFFFF'));

                // Add dynamic content to the fourth slide and continue the process for more slides...

                // Save the presentation
                $dynamicPresentationPath = 'dynamic_presentation.pptx';
                $objWriter = IOFactory::createWriter($presentation, 'PowerPoint2007');
                $objWriter->save($dynamicPresentationPath);

                echo "Dynamic PowerPoint presentation generated successfully!";

字符串
我已经附上了我的预期输出供您参考。


的数据
需要能够生成PPT使用背景图像或PPT文件与HTML内容,如表格和图表

l5tcr1uw

l5tcr1uw1#

您可以将PhpOffice\PhpPresentation库与PhpOffice\PhpPresentation\Style类沿着使用。
例如,在
添加模板或图像作为背景。

// your image file
$imagePath = 'path/to/background_image.jpg';

// Set the background image for the first slide
$backgroundImage = $firstSlide->createDrawingShape();
$backgroundImage->setPath($imagePath);
$backgroundImage->setWidth(960);
$backgroundImage->setHeight(720);
$backgroundImage->setOffsetX(0);
$backgroundImage->setOffsetY(0);

字符串
使用表格和背景颜色生成类似HTML的内容。

// Add a new slide
$newSlide = $presentation->createSlide();

// Set the slide background color (optional)
$backgroundShape = $newSlide->createRichTextShape();
$backgroundShape->setHeight(720);
$backgroundShape->setWidth(960);
$backgroundShape->getFill()->setFillType(\PhpOffice\PhpPresentation\Style\Fill::FILL_SOLID);
$backgroundShape->getFill()->getStartColor()->setRGB('FF5733');

// Create a table
$table = $newSlide->createTable();

// Set the column widths
$table->getColumnWidth(0)->setWidth(200);
$table->getColumnWidth(1)->setWidth(200);
$table->getColumnWidth(2)->setWidth(200);

// Add rows and cells with background colors
$row = $table->addRow();
$row->getFill()->setFillType(\PhpOffice\PhpPresentation\Style\Fill::FILL_SOLID);
$row->getFill()->getStartColor()->setRGB('FFFFFF');
$row->getCell(0)->setText('Cell 1');
$row->getCell(1)->setText('Cell 2');
$row->getCell(2)->setText('Cell 3');

$row = $table->addRow();
$row->getFill()->setFillType(\PhpOffice\PhpPresentation\Style\Fill::FILL_SOLID);
$row->getFill()->getStartColor()->setRGB('ECECEC');
$row->getCell(0)->setText('Cell 4');
$row->getCell(1)->setText('Cell 5');
$row->getCell(2)->setText('Cell 6');

// Continue adding more rows and cells as needed

// Style the table cells' text
foreach ($table->getRows() as $row) {
    foreach ($row->getCells() as $cell) {
        $cell->getTextRun()->getFont()->setSize(12);
        $cell->getTextRun()->getFont()->setColor(new \PhpOffice\PhpPresentation\Style\Color('000000'));
    }
}


请注意,要将图像设置为幻灯片的背景,可以使用createDrawingShape()方法并将图像路径设置为幻灯片的背景。请确保在向幻灯片添加任何其他内容之前添加此代码。要创建具有背景色的表格等内容,可以利用Table类以及文本和单元格的样式属性。

相关问题