Search This Blog

Saturday, October 6, 2007

Auto-Convert HTML to PDF in OpenOffice 2.3 on Windows

Figuring this out took me quite a while, but I finally managed to make OpenOffice 2.3 convert HTML pages to PDF from the command line. The big problem was that all the examples I found had slightly different requirements, so the code didn't work for me (especially when starting soffice with "-invisible"). Here's how to do it:

We need three macros, one for the document conversion, one as a helper to save further typing by easing the creation of OO PropertyValues and one to quit OpenOffice after that. So you need to open an OpenOffice app and select "Extras -> Macros -> Manage Macros -> OpenOffice.org Basic..." from the main menu. The window that pops up should already point to "My Macros / Standard / Module1", if not, navigate there. Click on the "Edit" button and insert the following in the end of the editor window:

Sub EndOO
StarDesktop.Terminate()
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

sub PDFExporting( iFile As String, targetFile As String )
rem ----------------------------------------------------------------------
rem define variables
dim document as object
dim oDoc as object
dim dispatcher as object
dim oDesk as object
rem ----------------------------------------------------------------------
rem load the document

inURL = ConvertToURL( iFile )

oDesk = createUNOService("com.sun.star.frame.Desktop")

oDoc = oDesk.loadComponentFromURL( inURL, "_blank", 0, Array(_
MakePropertyValue( "Hidden", True ) ) )

rem get access to the document
document = oDoc.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(3) as new com.sun.star.beans.PropertyValue
args1(0).Name = "URL"
args1(0).Value = ConvertToURL(targetFile)
args1(1).Name = "FilterName"
args1(1).Value = "writer_pdf_Export"
args1(2).Name = "FilterData"
args1(2).Value = Array(_
Array("UseLosslessCompression",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("Quality",0,90,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("ReduceImageResolution",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("MaxImageResolution",0,600,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("UseTaggedPDF",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("ExportNotes",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("UseTransitionEffects",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("FormsType",0,1,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("",0,,com.sun.star.beans.PropertyState.DIRECT_VALUE))
args1(3).Name = "SelectionOnly"
args1(3).Value = true

rem the actual conversion
dispatcher.executeDispatch(document, ".uno:ExportToPDF", "", 0, args1())

end sub


Now just save the changes and close everything. The OpenOffice part is finished, now let's just put a small batch file in our path that does the calling and knows the path to OpenOffice. Open your text editor and insert the following without any newlines (make sure that the path to soffice.exe is correct):

"C:\Program Files\OpenOffice.org 2.3\program\soffice.exe" -invisible macro:///Standard.Module1.PDFExporting("%1","%2") macro:///Standard.Module1.EndOO()


Save the text file in your windows directory (usually C:\WINDOWS) with the name oo2pdf.cmd.

Now you can call it from the command line with the complete paths to the input html and output pdf as parameters:

oo2pdf "C:\Documents and Settings\MyUserName\MyDocuments\input.html" "C:\Documents and Settings\MyUserName\Desktop\output.pdf"


Added: With thanks to John (see the comment), here's the bash command line for *nix-ish operating systems:
soffice -invisible macro:///Standard.Module1.PDFExporting\(infile.htm,outfile.pdf\) macro:///Standard.Module1.EndOO\(\)

1 comment:

Unknown said...

dude you rock!

one other thing... for us Unix people. you need to change the script to the following:

soffice -invisible macro:///Standard.Module1.PDFExporting\(infile.htm,outfile.pdf\) macro:///Standard.Module1.EndOO\(\)

you will need to escape the parenthesis