Java ist auch eine Insel

Freitag, November 23, 2007

jodconverter für OpenOffice Dateiformatkonvertierungen

Der zweite Schritt meiner Rechnungserstellung ist die Konvertierung in PDF. Auch dazu gibt es eine prima Bibliothek: http://www.artofsolving.com/opensource/jodconverter. (Einziger Nachteil: viele Jars.) Damit kann man Word, PowerPoint, RTF und alles andere in ein bliebiges Zielformat bringen.

private static void ensureStartedOpenOfficeService()
{
  try
  {
    new Socket( "127.0.0.1", 8100 );
  }
  catch ( Exception e )
  {
    String path = "C:/Programme/OpenOffice.org 2.3/program/";
    ProcessBuilder processBuilder = new ProcessBuilder( path+"soffice", "-headless", "-accept=\"socket,host=127.0.0.1,port=8100;urp;\"", "-nofirststartwizard" );

    try
    {
      processBuilder.start();
    }
    catch ( IOException ioe )
    {
      throw new RuntimeException( ioe );
    }
  }
}

public static void convert( String source, String destination )
{
  ensureStartedOpenOfficeService();

  OpenOfficeConnection connection = null;

  try
  {
    connection = new SocketOpenOfficeConnection( 8100 );     
    connection.connect();
    DocumentConverter converter = new OpenOfficeDocumentConverter( connection );
    File inputFile = new File( source );
    File outputFile = new File( destination );
    converter.convert( inputFile, outputFile );
  }
  catch ( ConnectException e )
  {
    throw new RuntimeException( e );
  }
  finally
  {
    connection.disconnect();
  }
}

Für meine Rechnungen also:

String destination    = "S:/Private/Traida/Bills/bill1";
String destinationOds = destination + ".ods";
String destinationPdf = destination + ".pdf";
convert( destinationOds, destinationPdf );

Labels: ,

AddThis Social Bookmark Button