Created
July 30, 2013 18:49
-
-
Save motin/6115683 to your computer and use it in GitHub Desktop.
Example code using the Java library Apache POI (http://php-java-bridge.sourceforge.net/pjb/webapp.php) to manipulate ppt-files (PHPPowerPoint can only create, not manipulate files). Uses Java/PHP bridge (http://php-java-bridge.sourceforge.net/pjb/how_it_works.php)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once("http://localhost:8080/GapcmsJavaBridge/java/Java.inc"); | |
// fake paths for gist | |
$sourceDataPpt = APP_PATH . "/includes/source_data.ppt"; | |
$uploadedPpt = APP_PATH . "/upload/foo_data.ppt"; | |
try { | |
$sourceDataPpt = new java("org.apache.poi.hslf.usermodel.SlideShow", new java("org.apache.poi.hslf.HSLFSlideShow", $$sourceDataPpt)); | |
$ppt = new java("org.apache.poi.hslf.usermodel.SlideShow", new java("org.apache.poi.hslf.HSLFSlideShow", $uploadedPpt)); | |
$newSlide = $ppt->createSlide(); | |
$translate = array( | |
'${t:DATA DOCUMENTATION}' => 'DATA DOCUMENTATION', | |
'${t:Updates and translations to this file may be available here:}' => 'Updates and translations to this file may be available here:', | |
'${chapter_url}' => 'http://www.gapminder.org/foo', | |
'${datasource1_url}' => 'http://un.org/esa/population', | |
'${datasource2_url}' => 'www.gapminder.org/data/documentation/gd003/', | |
); | |
foreach ($sourceDataPpt->getSlides()[0]->getShapes() as $k => $shape) { | |
foreach ($shape->getTextRun()->getRichTextRuns() as $i => $rt) { | |
$text = $rt->getText(); | |
//var_dump($k, $i, java_values($rt->getText())); | |
foreach ($translate as $search => $replace) { | |
if (strpos($text, $search) !== false) { | |
$text = str_replace($search, $replace, $text); | |
$rt->setText(new java("java.lang.String", $text)); | |
} | |
} | |
} | |
$newSlide->addShape($shape); | |
} | |
//var_dump(java_values($ppt)); | |
// Output to browser for now | |
header("Content-type: application/vnd.ms-powerpoint"); | |
header("Content-Disposition: attachment; filename=presentation.ppt"); | |
$memoryStream = new java("java.io.ByteArrayOutputStream"); | |
$ppt->write($memoryStream); | |
$memoryStream->close(); | |
echo java_values($memoryStream->toByteArray()); | |
} catch (Exception $e) { | |
var_dump($e); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment