Converting from ppt or pdf to images is very useful in webhosting industries especially in streaming area where we can use ppt presentations along with the video. As far as I know it is not possible to use ppt format directly in flash. So converting it to image is necessary. We can use this steps to convert ppt to pdf, ppt to images. First of all we need openoffice, imagemagick and jodconverter to achieve the same.
To convert ppt to images, install imagemagick
{codecitation}yum install imagemagick{/codecitation}
Now you can convert pdf to images as simple as follows.
{codecitation}convert sample.pdf sample.jpg{/codecitation}
If the pdf has many pages, then image files will be like sample00.jpg sample01.jpg ...
Now we can convert ppt to pdf. For that we need to download JODconverter. JODConverter homepage lists the following possible conversions:
{codecitation}* DOC to PDF, DOC to ODT, DOC to RTF
* XLS to PDF, XLS to ODS, XLS to CSV
* PPT to PDF, PPT to ODP, PPT to SWF
* ODT to PDF, ODT to DOC, ODT to RTF
* ODS to PDF, ODS to XLS, ODS to CSV
* ODP to PDF, ODP to PPT, ODP to SWF{/codecitation}
You can get it downloaded from the link given below.
{codecitation} http://sourceforge.net/projects/jodconverter/files/{/codecitation}
Make sure that you have installed java in the server. You can download and install it from the link given below.
{codecitation}http://java.sun.com/javase/downloads/index.jsp{/codecitation}
Also install openoffice. Follow the steps given below to install openoffice.
Download the tar file from the site given below.
{codecitation}http://www.openoffice.org/
tar zxf OOo*
cd OOH680_m1*
./setup{/codecitation}
Now we need to run openoffice as a service. So we can create a init script.
{codecitation}#!/bin/bash
OOo_HOME=/opt/openoffice.org2.4
SOFFICE_PATH=$OOo_HOME/program/soffice
PIDFILE=$OOo_HOME/openoffice-server.pid
case "$1" in
start)
if [ -f $PIDFILE ]; then
echo "OpenOffice headless server has already started."
exit
fi
echo "Starting OpenOffice headless server"
$SOFFICE_PATH -headless -accept="socket,host=127.0.0.1,port=81000;urp;" -nofirststartwizard & > /dev/null 2>&1
touch $PIDFILE
;;
stop)
if [ -f $PIDFILE ]; then
echo "Stopping OpenOffice headless server."
killall -9 soffice && killall -9 soffice.bin
rm -f $PIDFILE
exit
fi
echo "Openoffice headless server is not running, foo."
exit
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0{/codecitation}
Now add it in chkconfig so that it will be started automatically in the boot preocedure.
{codecitation}sudo chkconfig --add openoffice
sudo chkconfig --list openoffice
service openoffice start{/codecitation}
Now if you need to run it as a web application, you can follow the steps in the link given below.
{codecitation}http://www.artofsolving.com/node/14{/codecitation}
Here I am going to use the command line method. You can use this as exec function from PHP. Go the the location where you have downloaded JOD converter and execute the following commnad.
{codecitation}java -jar lib/jodconverter-cli-2.2.2.jar document.ppt document.pdf{/codecitation}
Make sure you have copied the ppt in to the folder :)
Now you will get it converted to pdf. You can change that pdf to image using the procedure given above.
If you need to use it as a tomcat application, you can follow the steps given below.
{codecitation}#!/bin/bash
OOo_HOME=/opt/openoffice.org2.4
SOFFICE_PATH=$OOo_HOME/program/soffice
PIDFILE=$OOo_HOME/openoffice-server.pid
JOD_HOME=/usr/local/jodconverter
case "$1" in
start)
if [ -f $PIDFILE ]; then
echo "OpenOffice headless server has already started."
exit
fi
echo "Starting OpenOffice headless server"
$SOFFICE_PATH -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard & > /dev/null 2>&1
$JOD_HOME/bin/startup.sh & > /dev/null 2>&1
touch $PIDFILE
;;
stop)
if [ -f $PIDFILE ]; then
echo "Stopping OpenOffice headless server."
killall -9 soffice && killall -9 soffice.bin
$JOD_HOME/bin/catalina.sh stop > /dev/null 2>&1
rm -f $PIDFILE
exit
fi
echo "Openoffice headless server is not running, foo."
exit
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0{/codecitation}
This is the php code that can be used as a alternative to the command line method.
{codecitation} <?php
// prepare the file download (in my case, these actually come from a database record)
$file = '/path/to/input/file';
// instantiate the document converter class.
require_once 'HTTP/Request.php';
class Converter{
var $url = 'http://localhost:8080/converter/service';
function convert($input, $input_file_type, $output_file_type){
$request = new HTTP_Request($this->url);
$request->setMethod("POST");
$request->addHeader("Content-Type", $input_file_type);
$request->addHeader("Accept", $output_file_type);
$request->setBody($input);
$request->sendRequest();
return $request->getResponseBody();
}
}
// do whatever else we need to do to make the magic happen
$converter = new Converter();
$input_file_type = 'application/vnd.oasis.opendocument.text';
$output_file_type = 'application/pdf';
$output_file = '/path/to/' . basename($file, get_extension($file)) . 'pdf';
$output = $converter->convert(file_get_contents($file), $input_file_type, $output_file_type);
file_put_contents($output_file, $output);
// and now replace the file variable with what we just created
$file = $output_file;
$download_filename = basename($file);
// required for IE, otherwise Content-Disposition is ignored
if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off');
// and now pipe the file out to the customer
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // some day in the past
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header('Content-Description: File Transfer');
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-Length: " . filesize($file));
header('Content-Disposition: attachment; filename="' . $download_filename . '"');
set_time_limit(0);
readfile($file);
exit;
?>{/codecitation}
Please do comment if you like this article or have any doubts :)