Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Discussion on converting .DOCX file to .PDF file using LIBREOFFICE in cakephp

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 278
    Comment on it

    Hi Reader's,
    Welcome to FindNerd, today we are going to discuss converting ".DOCX" file to ".PDF" file using LIBREOFFICE in cakephp.

    If we are developing any web application in CakePHP then sometimes we need a file to convert ".DOCX" to ".PDF". So for converting a file from .DOCX file to .PDF we have to install LIBRE OFFICE in our system.

    So let me explain something about LibreOffice.

    LIBREOFFICE:- It is very powerful and successor office suite which allows command line conversion using the LibreOffice conversion engine. LibreOffice is a very clean interface and it's features are very rich which help to us for making creativity and enhance to our productivity. It provides a preserves for the formatting our data  as we want.

    We will create a view which will look like below code:

    <?php
          echo $this->Form->create('User', array('url'=>array('controller'=>'users', 'action'=>'convertDocToPdf'),'enctype'=>'multipart/form-data', 'inputDefaults' => array('label' => false,'div' => false))) ;
          echo $this->Form->input('user_file',array('type'=>'file'));
          echo $this->Form->submit('Submit',array('class'=>'btnNew'));
    ?>

    Now we will write a function in our controller then our code will like below code:

    <?php
        public function convertDocToPdf(){
          if ($this->request->is('post')){
            if ($this->request->data['User']['user_file']!='' && is_array($this->request->data['User']['user_file'])){
                $imgData = array();
                $imgData['file'] = $this->request->data['User']['user_file'];
                $imgData['name']=$this->data['User']['user_file']['name'];
                $filepath = WWW_ROOT . 'file/temp/';//it is a folder name where we will move folder
                move_uploaded_file($imgData['file']['tmp_name'],$filepath.$imgData['name']);
                $result = shell_exec('export HOME='.$filepath.' && soffice --headless --convert-to pdf --outdir '.$filepath.' '.$filepath.$imgData['name']);//executes the command for converting DOCX file to pdf
                print_r($result);
                unlink($filepath.$imgData['name']);
                die;
            }
          }
        }
    ?>

    In above code when a user selects a file and goes for submit the request then firstly we will move the file in a folder and after that we will use shell_exec() function which will execute the command and converts the DOCX file to pdf.

    The command line that converts file is this:

    <?php 'export HOME='.$filepath.' && soffice --headless --convert-to pdf --outdir '.$filepath.' '.$filepath.$imgData['name'] ?>

    After executing this command we can find a pdf file in temp folder.

    So i hope this blog will help you for implementing to converting .DOCX file to .PDF file using LIBREOFFICE in your CakePHP web application.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: