Wednesday, September 07, 2011

Convert .doc to .pdf in command line using Libreoffice

The command would be 
 libreoffice --headless --convert-to pdf test.doc

test.doc is the document which we needed to be converted into pdf and this command will produce test.pdf in the same directory since we haven't specified a output directory.

Apparently this can be used to convert many more types of documents. For example converting a pdf file into odt would work too. 

libreoffice --headless --convert-to odt test.pdf

Its interesting to have a look at the command line options provided by the Libreoffice

LibreOffice 3.6

Usage: soffice [options] [documents...]

Options:
--minimized    keep startup bitmap minimized.
--invisible    no startup screen, no default document and no UI.
--norestore    suppress restart/restore after fatal errors.
--quickstart   starts the quickstart service
--nologo       don't show startup screen.
--nolockcheck  don't check for remote instances using the installation
--nodefault    don't start with an empty document
--headless     like invisible but no userinteraction at all.
--help/-h/-?   show this message and exit.
--version      display the version information.
--writer       create new text document.
--calc         create new spreadsheet document.
--draw         create new drawing.
--impress      create new presentation.
--base         create new database.
--math         create new formula.
--global       create new global document.
--web          create new HTML document.
-o            open documents regardless whether they are templates or not.
-n            always open documents as new files (use as template).

--display
      Specify X-Display to use in Unix/X11 versions.
-p
      print the specified documents on the default printer.
--pt
      print the specified documents on the specified printer.
--view
      open the specified documents in viewer-(readonly-)mode.
--show
      open the specified presentation and start it immediately
--accept=
      Specify an UNO connect-string to create an UNO acceptor through which
      other programs can connect to access the API
--unaccept=
      Close an acceptor that was created with -accept=
      Use -unnaccept=all to close all open acceptors
--infilter=
      Force an input filter type if possible
      Eg. -infilter="Calc Office Open XML"
--convert-to output_file_extension[:output_filter_name] [-outdir ouput_dir] files
      Batch convert files.
      If -outdir is not specified then current working dir is used as output_dir.
      Eg. -convert-to pdf *.doc
          -convert-to pdf:writer_pdf_Export -outdir /home/user *.doc
--print-to-file [-printer-name printer_name] [-outdir ouput_dir] files
      Batch print files to file.
      If -outdir is not specified then current working dir is used as output_dir.
      Eg. -print-to-file *.doc
          -print-to-file -printer-name nasty_lowres_printer -outdir /home/user *.doc

Remaining arguments will be treated as filenames or URLs of documents to open.

PS :-
As pointed out by Martin(read comments),  Looks like the above command will not work if the libreoffice application is already running. Thanks for point it out Martin. Does anybody know a work around?

14 comments:

  1. Anonymous2:34 AM

    Hi there

    I was working with OpenKM a DMS, and I was trying to use the preview feature for MS-Office documents.
    The server is an Ubuntu 11.04 with LibreOffice 3.4.3.
    I couln't see on-line, or via command any document, so, I tried: /path/soffice -writer and /path/soffice -writer a_file.doc.

    I no one I could open the file, or at least get the GUI of LibreOffice. I did a lot of steps but not worked!!
    Some idea?
    Thanks

    ReplyDelete
  2. Hi,

    I haven't used OpenKM. But reading the below thread, it seems to be that preview feature of OpenKM has some problems with Libreoffice. Didn't read the whole thread to see whether some one has a solution.
    http://forum.openkm.com/viewtopic.php?f=3&t=4691

    ReplyDelete
  3. Anonymous1:35 PM

    Hello!

    If I have already open LibreOffice, than the command /path/soffice -writer dont open new document. You must close all office windows, I think also quick luncher, then the command work

    Martin

    ReplyDelete
  4. Anonymous1:36 PM

    This comment has been removed by a blog administrator.

    ReplyDelete
  5. Hi,
    I am working on LInux fedora 15. Please suggest a way to convert doc and docx file to PDF file to integrate through PHP. (Othere than ABI word & unoconv, i have used this before but it can/t be successfully installed in server).
    So please help.

    Thanks in Advance

    ReplyDelete
  6. @Durga : Sorry, I don't know much about PHP unfortunately:( Hope someone would see your comment and help out :)

    ReplyDelete
  7. @Martin
    Thanks for the update Martin. I would change the post accordingly. Good observation and I don't know a work around too. If u come through any please share.

    ReplyDelete
  8. Anonymous12:58 PM

    for libreoffice 3.6 you need to use following command -->

    libreoffice3.6 --headless --convert-to pdf:writer_pdf_Export *.doc


    ---- As in terminal, libreoffice will be opened by command

    libreoffice3.6

    and not by command

    libreoffice

    ReplyDelete
  9. Anonymous2:55 AM

    bookmarked!!, I love your web site!

    Also visit my weblog - doc to pdf convertor

    ReplyDelete
  10. The workaround to convert documents even if some instance of LibreOffice is running is using the -env parameter to execute libreoffice with a new, temporary, profile:

    soffice -env:UserInstallation=file:///tmp/tempprofile --headless

    Details are here: http://ask.libreoffice.org/en/question/1686/how-to-not-connect-to-a-running-instance/

    Worked for me!

    ReplyDelete
    Replies
    1. Anonymous9:41 AM

      This worked well for me

      Thanks Jesus

      Delete
  11. bookmarked!!, I love your web site!
    Thank you for sharing this.

    ReplyDelete
  12. I am really inspired with your writing.

    ReplyDelete
  13. Anonymous3:29 PM

    for batch conversion, the number of files that can be converted is 248 . Any workaround ?

    ReplyDelete