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.


Introduction

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.

Installation

To install unoconv, open your terminal and execute the following command.

studytutorial.in@studytutorial:~$ sudo apt-get install unoconv

Option

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:

studytutorial.in@studytutorial:~$ 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

studytutorial.in@studytutorial:~$ sudo mkdir /var/www

studytutorial.in@studytutorial:~$ 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:

<?php

putenv('PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:
        /bin:/usr/games:/usr/local/games:/opt/node/bin');

?>

 

Exceute exec() method

Define a string:

<?php

$command = 'echo $HOME  & echo $PATH  & UNO_PATH=/usr/lib/libreoffice &  
           /usr/bin/unoconv -vvvv --format pdf --output 
           /yourdirectory/yourfilename.pdf  
          /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:

<?php

putenv('PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:
              /sbin:/bin:/usr/games:/usr/local/games:/opt/node/bin');

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

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

?>



The following two tabs change content below.
Abhay Anand has experience in the field of Software Development. He can program Android, PHP, JavaScript, Html, Css. He develops web and Android applications. You can reach him at anand.abhay1910@gmail.com