Tags:

data mining   fail save   gentoo   html2pdf   lazykid   mod_fcgid   nothing to hide   onetime password   open office   otp   privacy   ruby   ruby on rails   security   test driven development  

Last update
(Nov 25 2007)

Html2Pdf with OpenOffice

Thursday 01 November 2007 - 05:29 by sven

There is an easy way to make use of the OpenOffice features via commandline.

Recently I was in need for a robust and fast way to convert html files to pdf.
Main requirement was the ability to run under win32 and linux systems. Rendering of embedded images and usage of css.
First I used html2pdf from (www.tufat.com [http://www.tufat.com/s_html2ps_html2pdf.htm]), but ran into problems as the process used too much memory and processing power caused by many tables inside the html.

Here is how to do it with OpenOffice:

1. Start OpenOffice
2. Go to: 'Tools' -> 'Macros' -> 'Organize Dialogs..'
3. Create a new library called 'Converter' by selecting the 'Library' tab, pressing new and entering the name ('Convert').
4. Select the 'Modules' tab. There you can find your new library under the 'My Macros' menu item. Select it and press 'New' to     create a new module. Call it 'Convert'.
5. If you are on the right track, an editor window should be open now. In case there is any default text inside. Delete it.
6. Now enter the following code into the editor and press the save button when you are done.

Sub HtmlToPDF( cFile )
   cURL = ConvertToURL( cFile )
   oDoc = StarDesktop. loadComponentFromURL(cURL,"_blank",0,DimArray())
   cFile = Left( cFile, Len( cFile ) ) + ".pdf"
   cURL = ConvertToURL( cFile )
   oDoc.storeToURL( cURL, Array(_
            MakePropertyValue( "FilterName", "writer_pdf_Export" ),_
            )
   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

7. Ok. Close OpenOffice now. You are nearly done.
8. Download an example webpage. Lets call it 'test.html'
9. Test your macro by executing the following on your command prompt:
$ oowriter -invisible "macro:///Converter.Convert.HtmlToPDF(file:///tmp/test.html)"
10. Be happy with your new pdf at /tmp/test.html.pdf

If you run into problems, have questions or know a better or more easy way, please use the comment system.


open office html2pdf

Make a Comment!