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>
Related videos on Youtube
Author by
user260582
Updated on September 18, 2022Comments
-
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 about 10 yearsWhen I enter the first command libreoffice --headless... nothing happens
-
user260582 about 10 yearsno nothing, the command seems like it finishes but does not produce a file
-
Sylvain Pineau about 10 yearsWhen I start this first command I get
convert /home/sylvain/Documents/sample.xlsx -> /home/sylvain/Documents/sample.pdf using calc_pdf_Export
-
user260582 about 10 yearsI don't get anything, no response or errors
-
Sylvain Pineau about 10 yearsCould you try with another xlsx file? this is the right command to use on Ubuntu 13.10
-
user260582 about 10 yearsI just created a new xlsx file with data in one cell and still the same
-
Sylvain Pineau about 10 yearsI have
LibreOffice 4.1.3.2 410m0(Build:2)
(usinglibreoffice --version
) -
user260582 about 10 yearsI am using the version from the repo, LibreOffice 3.5, I am on 12.04.4 LTS
-
Sylvain Pineau about 10 yearsIt 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 about 10 yearsI 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 about 10 yearsI got this too (javaldx: Could not find a Java Runtime Environment!) but the conversion to pdf succeeded
-
user260582 about 10 yearsstrange, I get that error but still no PDF file
-
Sylvain Pineau about 10 yearsI'd recommend to upgrade to 13.10 as this command seems to not work for everyone on 12.04
-
user260582 about 10 yearsunfortunately it's not that simple as this is a production server
-
user260582 about 10 yearsdo you know how I can print to file using ghostscript as an alternative?
-
Sylvain Pineau about 10 yearsCould 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 about 10 yearsDid you try printing to a file? what's the outcome?
-
user260582 about 10 yearsHi Sylvain, I tried your command above, unfortunately I do not get a file created, the command runs but nothing
-
Sylvain Pineau about 10 yearsI only get this kind of behaviour when I have other libreoffice processes running. Did you check that on your server?
-
user260582 about 10 yearshow would I check if either older libreoffice or openoffice processes are running?
-
Sylvain Pineau about 10 yearsusing
pgrep -f libreoffice
and to kill thempkill -f libreoffice
-
user260582 about 10 yearsok 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 about 10 yearshumm, 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 about 10 yearsCould you check that
libreoffice-calc
is installed and if not install it?sudo apt-get install libreoffice-calc
-
user260582 about 10 yearsI 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 about 10 yearsBINGO! it worked, libreffice-calc wasn't installed
-
Sylvain Pineau about 10 yearsExcellent, I'll update my answer and add the tag 'server' to your question. Once done please vote and mark it as accepted please
-
user260582 about 10 yearsPerfect, 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 about 10 yearsUsually 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.