PowerPoint conversion

Filed under: Programming · Date: Sat Jul 28 09:21:00 2007

One project I'm involved in required reports in PowerPoint format. Unfortunately there is no Open Source PowerPoint library for Perl or PHP. PowerPoint doesn't seem to be as popular a format as e.g. Excel.

Since there were no implementations for producing PowerPoint files programmatically, I chose an alternate way to generate them. My goal was to avoid writing an exporter myself, which is a lengthy process at best. So I'm generating Impress documents and saving them as PowerPoint via OpenOffice.org.

The idea is simple, and would have been as easy to implement as any file format conversion using Unix tools. If only OpenOffice.org was as easy to use as the other Unix tools.

Generating PowerPoint

The first step is to generate the desired presentation with Impress. This is done with OpenOffice::OODoc.

In the second step we'll need to generate a macro to convert OpenOffice Impress documentation to PowerPoint.

Sub SaveAsPPT( cURL )
   On Error Resume Next
   oDoc = StarDesktop.loadComponentFromURL( cURL, "_blank", 0, _
      Array( MakePropertyValue( "Hidden", True ), ) )
   cURL = Left(cURL,Len(cURL)-4)+".ppt"
   oDoc.storeToURL( cURL, Array(_
   MakePropertyValue( "FilterName", "MS PowerPoint 97" ) )
   oDoc.close( True )
End Sub

Function MakePropertyValue( Optional cName As String, Optional uValue ) _
As com.sun.star.beans.PropertyValue
   Dim oPropertyValue As New com.sun.star.beans.PropertyValue
   If Not IsMissing( cName ) Then
      oPropertyValue.Name = cName
   EndIf
   If Not IsMissing( uValue ) Then
      oPropertyValue.Value = uValue
   EndIf
   MakePropertyValue() = oPropertyValue
End Function

The above script is based on code found in the OpenOffice.org forums. To install the macro, you'll need to open OpenOffice and use the macro organizer to create a new package and save the functions in it.

After we have our macros set, it's time to make OpenOffice.org callable via a script. This is the tricky part, since unlike other Unix utilities which provide a command line interface, quiet modes and all, OpenOffice.org requires X connection even if it's ordered not to open windows and work in the background.

My solution is to use Xvfb, which provides an display-less X server. To start Xvfb as a background process in display 0, use the following command.

$ Xvfb :0 &

After Xvfb is running, we can use OpenOffice from the shell.

$ DISPLAY=:0.0 soffice -invisible 'macro:///Standard.Conversion.SaveAsPPT(file:///tmp/doc.odp)'

Conclusion

Since we need to have Xvfb running in the background, the setup is more prone to problems. But we have the conversion working. I would have preferred a more independent implementation.

Comments

1
From Adarsh KP · Written on Nov 01, 2007 at 08:23

Thanks, it found to be very useful. I am looking for such a solution. Is it possible to work this command in php script. If not what can be possible issues.

2
From Juha-Mikko · Written on Nov 25, 2007 at 23:28

To run the conversion from a PHP script, you’ll need to first start Xvfb in the background, preferrably at system startup, from the same account PHP scripts are run as (usually that’s www-data or apache). These macros need to be installed for that user also. It’s easily done by installing them for your own account, and then copying the macro files to the apache user’s folder. For my system, the folder is /var/www/.openoffice.org2/user/basic/Standard/, but you should check your own account configuration for the proper place for them. After this initial setup, all you need to do is use system() method to make the conversion.

3
From Thirumaran · Written on Mar 28, 2008 at 12:23

hi ..
I want to convert ppt into image using openoffice… so please send the coding for openoffice..and how to command in linux+php


Comments are disabled for this post.