92

I have to wait for long after using this LibreOffice command which is intended to convert DOCX into PDF:

libreoffice --headless --convert-to pdf test.docx --outdir  /pdf

But I'm getting no response and no error. No file is converted. It looks like terminal is hanging.

So, is there is any way to track the error?

Braiam
  • 1
  • 11
  • 47
  • 78
vishal
  • 1,368
  • 2
  • 15
  • 34

6 Answers6

203
  1. I would first of all try it with giving absolute paths to the command.

  2. I suspect that the libreoffice binary does not work -- you have to locate the soffice binary and see if that works.

  3. Then, your --convert-to pdf is not sufficient. It needs to be:

    --convert-to pdf:writer_pdf_Export
    

    Be sure to follow exactly this capitalization!

  4. Next, the command will not work if there is already a LibreOffice GUI instance up and running on your system. It is caused by bug known since 2011. Add this additional parameter to your command:

     "-env:UserInstallation=file:///tmp/LibreOffice_Conversion_${USER}"
    

    This will create a new, separate environment which can be used by a second, headless LO instance without interfering with a possibly running first GUI LO instance started by the same user.

  5. Also, make sure that the --outdir /pdf you specify does exist, and that you have write permission to it. Or, rather use a different output dir. Even if it is just for the first testing and this debugging round:

    $ mkdir ${HOME}/lo_pdfs
    
  6. Hence:

    /path/to/soffice                                                     \
      --headless                                                         \
      "-env:UserInstallation=file:///tmp/LibreOffice_Conversion_${USER}" \
      --convert-to pdf:writer_pdf_Export                                 \
      --outdir ${HOME}/lo_pdfs                                           \
    /path/to/test.docx
    

    This works for me on Mac OS X Mavericks 10.9.5 with LibreOffice v4.4.3.2 (using my specific path for the binary soffice which will be different for you anyway...).

    If all this doesn't work:

  7. It may be a problem with the specific DOCX file you try the command with... So create a very simple DOCX document of your own first. Use LibreOffice itself for this. Write "Hello World!" on an otherwise empty page. Save it as DOCX.

  8. Try again. Does it work with the simple DOCX?

  9. If it again doesn't work, repeat step 7, but save as ODT this time.

  10. Repeat step 8, but make sure to reference the ODT this time.

  11. Last: Use full path to soffice, to soffice.bin and to libreoffice and run each with the -h parameter:

    $ /path/to/libreoffice -h
    $ /path/to/soffice -h
    $ /path/to/soffice.bin -h
    
    • Do you even get an output here?
    • For which one of the three binaries/symlinks?
    • Record the outputs.
    • Tell us your outputs!!!

    Compare them to the command line you used:

    Are there any changes in parameter names, capitalizations, number of dashes used, etc.??

    For comparison, my own output is here:

    $ /Applications/LibreOffice.app/Contents/MacOS/soffice -h
    
      LibreOffice 4.4.3.2 88805f81e9fe61362df02b9941de8e38a9b5fd16
    
      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 <display>
            Specify X-Display to use in Unix/X11 versions.
      -p <documents...>
            print the specified documents on the default printer.
      --pt <printer> <documents...>
            print the specified documents on the specified printer.
      --view <documents...>
            open the specified documents in viewer-(readonly-)mode.
      --show <presentation>
            open the specified presentation and start it immediately
      --accept=<accept-string>
            Specify an UNO connect-string to create an UNO acceptor through which
            other programs can connect to access the API
      --unaccept=<accept-string>
            Close an acceptor that was created with --accept=<accept-string>
            Use --unnaccept=all to close all open acceptors
      --infilter=<filter>[:filter_options]
            Force an input filter type if possible
            Eg. --infilter="Calc Office Open XML"
                --infilter="Text (encoded):UTF8,LF,,,"
      --convert-to output_file_extension[:output_filter_name[:output_filter_options]] [--outdir output_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
                --convert-to "html:XHTML Writer File:UTF8" *.doc
                --convert-to "txt:Text (encoded):UTF8" *.doc
      --print-to-file [-printer-name printer_name] [--outdir output_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
      --cat files
            Dump text content of the files to console
            Eg. --cat *.odt
      --pidfile file
            Store soffice.bin pid to file.
      -env:<VAR>[=<VALUE>]
            Set a bootstrap variable.
            Eg. -env:UserInstallation=file:///tmp/test to set a non-default user profile path.
    
      Remaining arguments will be treated as filenames or URLs of documents to open.
    
  12. Add one more argument to your command line to enforce the application of an input filter when soffice opens your DOCX file:

    --infilter="Microsoft Word 2007/2010/2013 XML"
    

    or

    --infilter="Microsoft Word 2007/2010/2013 XML"
    --infilter="Microsoft Word 2007-2013 XML"
    --infilter="Microsoft Word 2007-2013 XML Template"
    --infilter="Microsoft Word 95 Template"
    --infilter="MS Word 95 Vorlage"
    --infilter="Microsoft Word 97/2000/XP Template"
    --infilter="MS Word 97 Vorlage"
    --infilter="Microsoft Word 2003 XML"
    --infilter="MS Word 2003 XML"
    --infilter="Microsoft Word 2007 XML Template"
    --infilter="MS Word 2007 XML Template"
    --infilter="Microsoft Word 6.0"
    --infilter="MS WinWord 6.0"
    --infilter="Microsoft Word 95"
    --infilter="MS Word 95"
    --infilter="Microsoft Word 97/2000/XP"
    --infilter="MS Word 97"
    --infilter="Microsoft Word 2007 XML"
    --infilter="MS Word 2007 XML"
    --infilter="Microsoft WinWord 5"
    --infilter="MS WinWord 5"
    

Update

LibreOffice cannot only convert DOCX to PDF on the command line:

  • It can process any input format which it can open and "read": DOC, XLS, XLSX, PPT, PPTX, ...
  • It can convert to any output format which it can "write": DOC, XLS, XLSX, PPT, PPTX, ...

Of course, results will never be perfect, and sometimes they will even be un-acceptable to you.

Re. output as PDF:

To control, which LibreOffice component generates PDF output, you can use these variants:

--convert-to pdf:writer_pdf_Export
--convert-to pdf:calc_pdf_Export
--convert-to pdf:draw_pdf_Export
--convert-to pdf:impress_pdf_Export
--convert-to pdf:writer_web_pdf_Export

Re. input which is not DOCX:

To enforce infilters for non-DOCX input formats, you could use (list is not complete):

--infilter="HTML Document"                      # for HTML input
--infilter="MediaWiki"                          # for MediaWiki input
--infilter="Text CSV"                           # for CSV spreadsheet input
--infilter="Microsoft PowerPoint 2007/2010 XML" # for PPTX input
--infilter="Microsoft PowerPoint 97/2000/XP"    # for PPT input
--infilter="Windows Metafile"                   # for WMF input
--infilter="Enhanced Metafile"                  # for EMF input
--infilter="Scalable Vector Graphics"           # for SVG input
--infilter="Microsoft Excel 2007/2010 XML"      # for XLSX input
--infilter="Microsoft Excel 97/2000/XP"         # for XLS input
--infilter="Microsoft Excel 95"                 # for some XLS input
--infilter="Microsoft Excel 5.0"                # for some XLS input

Re. output which is not PDF:

To convert to specific output formats, you could use (list not complete):

--convert-to html:HTML
--convert-to html:draw_html_Export                 # force "Draw" to generate the HTML
--convert-to mediawiki:MediaWiki_Web               # generate MediaWiki output
--convert-to csv:"Text - txt - csv (StarCalc)"     # generate CSV spreadsheet output
--convert-to pptx:"Impress MS PowerPoint 2007 XML" # generate PPTX
--convert-to ppt:"MS PowerPoint 97"                # generate PPT
--convert-to wmf:impress_wmf_Export                # force "Impress" to generate the WMF
--convert-to wmf:draw_wmf_Export                   # force "Draw" to generate the WMF
--convert-to emf:impress_emf_Export                # force "Impress" to generate the EMF
--convert-to emf:draw_emf_Export                   # force "Draw" to generate the EMF
--convert-to svg:impress_svg_Export                # force "Impress" to generate the SVG
--convert-to svg:draw_svg_Export                   # force "Draw" to generate the SVG
--convert-to xlsx:"Calc MS Excel 2007 XML"         # generate XLSX
--convert-to xls:"MS Excel 97"                     # generate XLS like Excel 97
--convert-to xls:"MS Excel 95"                     # generate XLS like Excel 95
--convert-to xls:"MS Excel 5.0/95"                 # generate XLS like Excel 5.0/95
Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
  • 21
    @vishal: I've been very specific in my answer for you. You don't even bother to tell your version, despite having been asked for it explicitly. Nor did you tell, if your 'soffice' vs. 'libreoffice' binaries exist... Sorry, my support for your problem debugging ends here. – Kurt Pfeifle May 28 '15 at 23:53
  • 13
    Kurt, I found the detailed response very helpful. Thank you for investing the time to answer the question. – dat Sep 21 '15 at 17:12
  • 4
    @dat: Thanks for saying "thanks". Too many barbarians here aren't any longer familiar with this type of courtesy. – Kurt Pfeifle Nov 13 '15 at 20:52
  • 2
    Thanks for outlining this! #4 was what was wrong for me. – emilyk May 17 '16 at 21:07
  • How do you set the range of pages? – Dor May 21 '16 at 09:31
  • @Dor: I don't think you can. You'll have to convert all pages first, then post-process the generated PDF to extract only these which you want. *(But you are hijacking a thread with your **other** question, and you are asking it in a comment -- 2 violations of common StackExchange rules...)* – Kurt Pfeifle May 21 '16 at 17:34
  • 1
    @KurtPfeifle love for such details – Steeve Sep 12 '16 at 13:49
  • @KurtPfeifle i tried word to pdf and it worked but from pdf to word is not working in latest version of libreoffice.is it correct or i am doing some mistake, libreoffice --headless --convert-to docx:MS Word 2007 XML home/1/bara.pdf --outdir home//1 – Steeve Sep 12 '16 at 13:52
  • 1
    A giant THANK YOU for 4) I was almost crying why I had no output. – Kai Lehmann Sep 14 '20 at 08:28
  • 1
    Kurt, where are the specific output formats documented? – Massimo May 10 '21 at 13:55
  • 2
    @Massimo: Here, in my answers. I do not know any other more complete or more comprehensive document. I sourced my answer in parts from the LibreOffice source code, in parts from LibreOffice documentation. – Kurt Pfeifle May 10 '21 at 17:34
  • You can find all of the Libreoffice 5.x Import/Export filters here and with the links in the top reply here: https://ask.libreoffice.org/t/what-are-the-5-x-impress-import-export-filters/15181 – UrbanConor Feb 08 '22 at 17:29
  • @Massimo & others - Documentation for filter names for command line document conversion can be found here: https://help.libreoffice.org/latest/en-US/text/shared/guide/convertfilters.html – UrbanConor Feb 08 '22 at 17:44
8

Update

The current Version of libreoffice is working: v5.2.6.2 (mac)

I'm using the following command

/Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to pdf --outdir . the_file_to_convert

OLD

It seems that the conversion is not working in newer version (> 4.2.4.2) I'm using libreoffice 4.2.4.2 You can download older versions from http://downloadarchive.documentfoundation.org/libreoffice/old/4.2.4.2/

  • linux (using the deb package):

    libreoffice4.2 --headless --convert-to pdf  myfile.odt
    
  • windows:

    swriter.exe --headless --convert-to pdf myfile.odt
    
Linkid
  • 517
  • 5
  • 17
lumos0815
  • 3,908
  • 2
  • 25
  • 25
  • Hello 4.2 also same issue not responding at all! – Kiran Reddy Apr 17 '17 at 11:42
  • have you tried 4.2.4.2? later versions were not working for me. I've installed it from the packages out of the given link – lumos0815 Apr 18 '17 at 06:44
  • 4.2.4.2 only I have installed from the above link, And it is taking more than 5 min to convert a 10 page doc to PDF And I tried in VPS ( AWS ) - Ubuntu 14.04 – Kiran Reddy Apr 18 '17 at 07:21
  • seems that the current version has fixed the pdf creator. if checked it with 5.2.6.2 with the following command: /Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to pdf --outdir . theodtfile.odt – lumos0815 Apr 18 '17 at 17:52
  • This helped me. I had up to date Centos 6 libreoffice. On conversion request no output to shell, no converted file. Just silent exit. And I remember that before everything was working. Rolling back to 4.0.4.2 (Centos 6.6 repository) made everything working again. – Evgeniy Spinov Sep 06 '19 at 20:12
  • what is the difference between soffice.exe and swriter.exe? – Željko Krnjić Dec 06 '22 at 12:38
  • `libreoffice --headless --convert-to pdf filename.docx` works fine for me with LibreOffice 7.0.4.2 00(Build:2) on Debian 11.7. I don't see why it wouldn't. – Faheem Mitha Aug 01 '23 at 09:32
5

Might sound stupid but I suffered from just this: In Ubuntu 14.04 it is not enough to install only the package libreoffice-core or -common to convert to PDF, but you need to install libreoffice-writer instead/additionally. The important thing to note is that libreoffice-core|common seems to bring in some functional libreoffice base, hence the name ;-). That doesn't error out if executed to convert some file, but instead just seems to wait on some socket or such for input or whatever... After I installed libreoffice-writer, which has [...]-core|common as a dependency, converting some ODT file to PDF worked like a charm with the exact same command line like used before.

Seems a bit crazy that the application looks like it works somehow, but simply couldn't ever because of missing packages, and doesn't tell anything about it.

Thorsten Schöning
  • 3,501
  • 2
  • 25
  • 46
1

I'm using LibreOffice 4.2.8.2 too (on GNU/Linux) but I tried to convert a DOC file to a HTML file. I've installed libreoffice-headless, but the terminal was hanging too. I've tried each bullet of Kurt Pfeifle, but it was not working…

The solution was simple: installing libreoffice-writer (maybe libreoffice-filters is ok too, but it depends on libreoffice-writer plus the other big components of LibreOffice).

Hope this will help.

Linkid
  • 517
  • 5
  • 17
0

I had the same problem and got stuck for an entire day; silly me ended by figuring out that I was trying to convert from and to a network file system; even tho I have written right libreoffice seems to not support it, or at least not in my configuration.

I solved this by converting to /tmp then moving the file by hand. Hope it helps.

phuclv
  • 37,963
  • 15
  • 156
  • 475
0

I encountered problems converting HTML to xlsx using libreoffice. Other convert-to options worked fine. Turns out it needed the --calc flag additionally. In case anyone else runs into the same trouble. Tested on LibreOffice-6.4.3.2.

Credits to: Libreoffice converting html to xls or xlsx

kaikun
  • 98
  • 10
  • 1
    Hmm, in the original question the output folder is "/pdf" - that is directly in root. If you run LibreOffice as a regular user you will not be allowed to output there. Try "pdf/" instead that will create a folder in your current folder. – Paul Bergström Jun 08 '22 at 08:28