16

I have some documentation written in OpenOffice, and I would like to include some of it as PDF files in the final build deliveries. I would like to do this with the automated build script.

Is there a way to create a PDF file from OpenOffice with a command line command?

Tsundoku
  • 2,455
  • 1
  • 19
  • 31
Drejc
  • 14,196
  • 16
  • 71
  • 106

5 Answers5

31

As of September 2012, LibreOffice can convert a document to PDF from the command line:

lowriter --headless --convert-to pdf yourfile.odt

It also has bulk conversion support:

lowriter --headless --convert-to pdf yourfiles*.odt

will convert all the files that match the pattern to the corresponding PDF file.

There must be no LibreOffice windows open when you run this command.

Tsundoku
  • 2,455
  • 1
  • 19
  • 31
Eineki
  • 14,773
  • 6
  • 50
  • 59
  • That's very neat. Do you know if there's a way to update a table of Contents before doing the conversion? – Pieter Hintjens Dec 14 '12 at 20:18
  • I've managed to convert images, word documents, spreadsheets. Works like a charm. – Valentin Despa Jul 02 '13 at 08:04
  • 1
    This solution is very solid but a bit slow. Just a little bit of info for anyone considering it, as of version 4.1.0.4, a fairly basic file is converting in 1800 ms. – Ben Gripka Jul 26 '13 at 14:39
  • Strange, this is just opening a new document for me with Libreoffice in Ubuntu 13.10: both the --convert-to and the --headless args seem to be ignored. I'm trying to convert spreadsheets rather than docs, though. – andybuckley Dec 20 '13 at 14:32
  • @andybuckley Are you using _localc_? I've just used the command on the LTS ubuntu (using localc instead of lowriter) and it worked flawlessy. – Eineki Dec 23 '13 at 10:44
  • @Eineki Yep, I used localc. I suspect it's something wrong with my system and/or the latest LibreOffice. But unoconv (below) from the U13.10 repository worked perfectly. – andybuckley Dec 27 '13 at 10:40
  • 2
    Since this is not documented here yet: If this command line call fails with this message `Error: Please reverify input parameters...` try running it as root (e.g. via `sudo`). This helped me on Ubuntu 12.04 LTS with Libreoffice 3 installed. And if it ignores the `--headless` parameter try calling `loffice` or `libreoffice` instead of `lowriter`. – Michael Helwig May 19 '14 at 11:34
  • @PieterHintjens You can define a global marco to update the TOC before doing the conversion as shown in https://ask.libreoffice.org/en/question/46586/how-to-automatically-update-indices-in-headless-mode/. You might want to start in headless mode and add `starDesktop.terminate()` in the macro to close LibreOffice once the conversion is complete. – Augustus Kling Jan 24 '16 at 23:19
  • If you want to export with certain options (e.g. tagged PDF), do you first need to set them in OpenOffice or LibreOffice? Or is there a command line argument for options like this? – Tsundoku Aug 12 '16 at 15:20
  • `--convert-to` already implies `--headless`, at least in my case (version 6.0.7.3 00m0(Build:3)). – knia Dec 16 '19 at 10:08
16

There is a great tool called "unoconv", it was in my Ubuntu repository. It converts ODF, .ods, ... to PDF and I think to other formats too.

I also could convert PowerPoint files to PDF.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
12

Art of Solving has also a very good API to perform the conversion in Java. It is a little slow but it is simple enough. This is how I use it:

        File inputFile = new File("C:\\oreyes\\hola.doc"); 
        File outputFile = new File("C:\\oreyes\\hola.pdf"); 
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
        try { 
            connection.connect(); 
        } catch(Exception e) {}

        DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
        converter.convert(inputFile, outputFile); 
        connection.disconnect(); 

You can create a jar with that and process it from the command line.

Tsundoku
  • 2,455
  • 1
  • 19
  • 31
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • 1
    Did same way as you describe, but specified `OpenOfficeConnection officeConnection = new SocketOpenOfficeConnection( "my_local_vm_ip", 8100 );`, but I get an error all the time. `Exception in thread "main" com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException: conversion failed: could not load input document` – Eugene Jul 21 '11 at 10:12
  • If you want to export with certain options (e.g. tagged PDF), do you first need to set them in OpenOffice or LibreOffice? – Tsundoku Aug 12 '16 at 15:20
2

Though this question is a little old, here something for the purpose of documenting some common pitfalls with the LibreOffice solution:

  • If lowriter does not work for you because it ignores command line parameters and brings up the gui just try calling the libreoffice or loffice binaries:

    loffice --headless --convert-to pdf yourfile.odt

  • If you get this message

    Error: Please reverify input parameters...

    try running it as root (e.g. via sudo). This helped me on Ubuntu 12.04 LTS with LibreOffice 3 installed and may also be a reason why this conversion is not running on a webserver without proper configuration (Libreoffice --headless refuses to convert unless root, won't work from PHP script)

  • Also make sure that you do not have any other instances of LibreOffice running or it will just fail silently and do no conversion at all.

Michael Helwig
  • 530
  • 4
  • 12
1

There is anytopdf. Haven't tried it myself.

Quoting...

anytopdf is a perl script that converts OpenOffice.org, Microsoft Office (Word DOC, Excel XLS), RTF, HTML, and other openoffice.org readable file formats to the PDF format. It will automatically install the supporting 'AnyToPDF' OpenOffice.org Basic macro library in the current user's OpenOffice.org configuration if it's not already present.

Dedicated to peace, love, understanding and respect for all beings.

Archie
  • 4,959
  • 1
  • 30
  • 36