How to convert Excel to PDF in Ubuntu using Unoconv Library PHP Tutorial


This tutorial explains how to convert Excel Sheet to PDF file in Linux server using PHP.

Download Code


Unoconv is used to convert any file format to any LibreOffice supported file. It is a command line program that reads file in one format and writes into another format.

We will use unoconv to convert Excel Sheet to PDF file in PHP. We will use Linux sever to convert file one to another. Firstly, we need to install unoconv and then need to make a directory named as libreoffice in the /var/www/ path. We will create a PHP file to make a Php script to convert Excel Sheet to PDF file.


To install unoconv, open your terminal and execute the following command.$ sudo apt-get install unoconv


To convert from one format to another, we’ll need to use the following flags:

The “-f” flag will indicate what you’re requesting as the final output. For example, “-f pdf” will convert whichever file(s) you include in the command to PDF format.

The “-o” flag allows you to specify a different path for the converted files (however, this is optional, as the default is the current directory).

Convert Excel sheet to PDF file by terminal

We can convert Excel Sheet to PDF by using terminal. hence open your terminal and run following command:$ unoconv -f pdf -o /tmp/example.pdf /tmp/example.xlsx

Create Directory

unoconv needs a directory in the /var/www/libreoffice because it make a copy of libreoffice so, we create a directory /var/www/libreoffice$ sudo mkdir /var/www$ sudo mkdir /var/www/libreoffice

Convert Excel Sheet to PDF file using PHP

To convert Excel Sheet to PDF file through PHP, we need to create a PHP file to write a script.

Add putenv() method

Now, add putenv() method in the PHP file.The putenv() function adds or changes the value of environment variables. The argument string is of the form name=value. The string pointed to by string becomes part of the environment, so altering the string changes the environment.

Use the following code:





Exceute exec() method

Define a string:


$command = 'echo $HOME  & echo $PATH  & UNO_PATH=/usr/lib/libreoffice &  
           /usr/bin/unoconv -vvvv --format pdf --output 
          /yourdirectory/yourfilename.xlsx 2>~/output.txt';



$HOME:- we can define own your PATH where we want to generate output.txt file.

$PATH:- $PATH is your environment PATH that define in putenev() method.

UNO_PATH:- UNO_PATH is your libreoffice path.

/usr/bin/unoconv:- /usr/bin/unoconv is your unoconv path.

-format pdf:- write your output pdf format.

–output /yourdirectory/yourfilename.pdf:- write pdf file into your defined directory.

/yourdirectory/yourfilename.xlsx :- read your excel file.

output.txt :- write result in output.txt file.

Here is the final code:



$command = 'echo $HOME  & echo $PATH  & UNO_PATH=/usr/lib/libreoffice &  
           /usr/bin/unoconv -vvvv --format pdf --output 
           /yourdirectory/yourfilename.xlsx 2>~/output.txt';

exec($command, $output, $result_var);