Convert .xls to .pdf using LibreOffice via Command Line

Rhenan Bartels picture Rhenan Bartels · Sep 16, 2014 · Viewed 10.7k times · Source

I'm trying to convert a .xls file to .pdf using LibreOffice via command line on Ubuntu. I have a kind of report on the .xls file with some colors in the background of the cells and etc.

The problem is when I convert the .xls file, the .pdf loses the original format. Each page is broken almost in the half and the content of one page is displayed in two different pages.

  • Does anybody know how to convert the .xls file to .pdf via command line with keeping the original format?
  • Or some trick to set the size of the .pdf page to not break pages? (Also via command line)

The code I used to make the conversion was:

soffice --headless --convert-to pdf:"impress_pdf_Export" filename.xls

Answer

Kurt Pfeifle picture Kurt Pfeifle · Apr 16, 2015

If you use LibreOffice to convert Microsoft Excel (XLS) files to PDF documents, this is a two-step process (even if your command does look like it is a one-step process):

  1. Import the XLS into LibreOffice (even if started with --headless).
  2. Export the PDF from LibreOffice.

If the result does not look like you expect (not similar enough to Excel's native PDF export), then start with debugging the first step from above:

  • Open the XLS file with LibreOffice in a GUI. Does it look like you expect it to look? Or are some formatting options looking weird?

    Export the PDF from there (with the GUI). Are the page dimensions as you expect? Did you set them up how you prefer? The margins like you want them? etc.pp. ...


If you are working on Windows, you may also want to consider OfficeToPDF.exe. It is hosted on CodePlex, licensed with the Apache 2.0 License and available in binary and in source code.

It requires a working Office 2013, Office 2010 or Office 2007 installation. But then it can commandline- and batch-convert to PDF various MS Office-based file formats, including XLS(X), PPT(X), DOC(X), VSD(X) and PUB as well as Libre/OpenOffice-based ODT, ODS and ODC files.