print xlsx file from command line using ghostscript and libreoffice

5,439

You can run libreoffice in headless mode, but before executing the following command, first quit/close all running Libreoffice instances.

In order to print xlsx files you'll have to install libreoffice-calc on your server:

sudo apt-get install libreoffice-calc

Then the following command will convert your filename.xlsx into filename.pdf in the same folder:

libreoffice --headless --invisible --convert-to pdf <filename.xlsx> 

and print the document with lpr:

lpr -U username -P the_printer_name <filename.pdf>

Note: alternatively you could also print-to-file using this command:

libreoffice --headless --print-to-file --outdir /tmp <filename.xlsx>
Share:
5,439

Related videos on Youtube

user260582
Author by

user260582

Updated on September 18, 2022

Comments

  • user260582
    user260582 over 1 year

    I have been searching on how to get libreoffice in headless mode to print to a ghostscript PDF file on Ubuntu Server 12.04.

    Can somebody tell me how to do this or if there is an easier way?

    I tried with unoconv but the PDF result didn't look right, I am hoping the ghostscript way prints it directly as it looks in Excel.

    Thanks

  • user260582
    user260582 about 10 years
    When I enter the first command libreoffice --headless... nothing happens
  • user260582
    user260582 about 10 years
    no nothing, the command seems like it finishes but does not produce a file
  • Sylvain Pineau
    Sylvain Pineau about 10 years
    When I start this first command I get convert /home/sylvain/Documents/sample.xlsx -> /home/sylvain/Documents/sample.pdf using calc_pdf_Export
  • user260582
    user260582 about 10 years
    I don't get anything, no response or errors
  • Sylvain Pineau
    Sylvain Pineau about 10 years
    Could you try with another xlsx file? this is the right command to use on Ubuntu 13.10
  • user260582
    user260582 about 10 years
    I just created a new xlsx file with data in one cell and still the same
  • Sylvain Pineau
    Sylvain Pineau about 10 years
    I have LibreOffice 4.1.3.2 410m0(Build:2) (using libreoffice --version)
  • user260582
    user260582 about 10 years
    I am using the version from the repo, LibreOffice 3.5, I am on 12.04.4 LTS
  • Sylvain Pineau
    Sylvain Pineau about 10 years
    It works for 13.10, some users reported the need of starting the command from the folder where the file is and using sudo. Time to upgrade?
  • user260582
    user260582 about 10 years
    I just added the libreoffice repo and am now trying with LibreOffice 4.2.2.1. Now I get Could not find Java Runtime Environment
  • Sylvain Pineau
    Sylvain Pineau about 10 years
    I got this too (javaldx: Could not find a Java Runtime Environment!) but the conversion to pdf succeeded
  • user260582
    user260582 about 10 years
    strange, I get that error but still no PDF file
  • Sylvain Pineau
    Sylvain Pineau about 10 years
    I'd recommend to upgrade to 13.10 as this command seems to not work for everyone on 12.04
  • user260582
    user260582 about 10 years
    unfortunately it's not that simple as this is a production server
  • user260582
    user260582 about 10 years
    do you know how I can print to file using ghostscript as an alternative?
  • Sylvain Pineau
    Sylvain Pineau about 10 years
    Could you try this before thinking to an alternative: libreoffice --headless --print-to-file --outdir /tmp <filename.xlsx>, it will give you a filename.ps in /tmp (in theory). If that works I'll update my answer. again be sure to kill all libreoffice processes running on your server.
  • Sylvain Pineau
    Sylvain Pineau about 10 years
    Did you try printing to a file? what's the outcome?
  • user260582
    user260582 about 10 years
    Hi Sylvain, I tried your command above, unfortunately I do not get a file created, the command runs but nothing
  • Sylvain Pineau
    Sylvain Pineau about 10 years
    I only get this kind of behaviour when I have other libreoffice processes running. Did you check that on your server?
  • user260582
    user260582 about 10 years
    how would I check if either older libreoffice or openoffice processes are running?
  • Sylvain Pineau
    Sylvain Pineau about 10 years
    using pgrep -f libreoffice and to kill them pkill -f libreoffice
  • user260582
    user260582 about 10 years
    ok I have made sure no libreoffice processes are running but I still get the same, Java Runtime error, command finishes but no output in /tmp
  • Sylvain Pineau
    Sylvain Pineau about 10 years
    humm, retested even on a 12.04 system, both commands work, print-to-file and convert-to. The only difference is that in my case I'm running desktop systems.
  • Sylvain Pineau
    Sylvain Pineau about 10 years
    Could you check that libreoffice-calc is installed and if not install it? sudo apt-get install libreoffice-calc
  • user260582
    user260582 about 10 years
    I might install a 12.04 system with desktop and see if it works for me, not sure how that will help me on my production server though.
  • user260582
    user260582 about 10 years
    BINGO! it worked, libreffice-calc wasn't installed
  • Sylvain Pineau
    Sylvain Pineau about 10 years
    Excellent, I'll update my answer and add the tag 'server' to your question. Once done please vote and mark it as accepted please
  • user260582
    user260582 about 10 years
    Perfect, one more thing, do you know why when converting using libreoffice it prints out different to excel, over more pages, can I set default page settings anywhere?
  • Sylvain Pineau
    Sylvain Pineau about 10 years
    Usually print settings/print area are defined in the workbook itself. Regarding the export settings to PDF, check this page. Please mark my answer as accepted please. You can ask a new question about print settings and the Ask Ubuntu community will do its best to help/assist you.