OpenOffice command line PDF creation

2019-01-17 08:08发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

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.



回答4:

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.



回答5:

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.