2

I have a Yii project that allows importing files.

Within this project I call the following command to try and convert xls files to csv:

$file = fopen($model->importfile->tempname,'r');
$filetype = substr($model->importfile, strrpos($model->importfile, '.')+1);
if ($filetype === 'xls')
{
    $tempxls = $model->importfile->tempname;
    $outputArr = array();
    exec(Yii::app()->basePath."/commands/xlstocsv.sh " . $tempxls, $outputArr);
    PropertiesController::xlsToConsoleV7Format($tempxls, $log);
}

xlstocsv.sh:

#!/bin/bash
# Try to autodetect OOFFICE and OOOPYTHON.
OOFFICE=`ls /usr/bin/libreoffice /usr/lib/libreoffice/program/soffice /usr/bin/X11/libreoffice | head -n 1`
OOOPYTHON=`ls /usr/bin/python3 | head -n 1`
XLS='.xls'
CSV='.csv'
INPUT=$1$XLS
OUTPUT=$1$CSV

cp $1 $INPUT

if [ ! -x "$OOFFICE" ]
then
    echo "Could not auto-detect OpenOffice.org binary"
    exit
fi
if [ ! -x "$OOOPYTHON" ]
then
    echo "Could not auto-detect OpenOffice.org Python"
    exit
fi
echo "Detected OpenOffice.org binary: $OOFFICE"
echo "Detected OpenOffice.org python: $OOOPYTHON"
# Start OpenOffice.org in listening mode on TCP port 2002.
$OOFFICE "-accept=socket,host=localhost,port=2002;urp;StarOffice.ServiceManager" -norestore -nofirststartwizard -nologo -headless &
# Wait a few seconds to be sure it has started.
sleep 5s
# Convert as many documents as you want serially (but not concurrently).
# Substitute whichever documents you wish.
$OOOPYTHON /fullpath/DocumentConverter.py $INPUT $OUTPUT
# Close OpenOffice.org.

cp $OUTPUT $1

DocumentConverter.py: This can be found here: https://github.com/mirkonasato/pyodconverter. It has been slightly modified to have correct syntax for python3.

Ok, the issue is, when running the php code from the terminal, it correctly creates the csv file from the excel file.

However, when running it from within the browser, it still runs the script and creates the output file, but it has not correctly converted it into csv.

It all works perfectly for every file I have thrown at it so far when running from console, but for some reason when running it from within a browser, it fails to convert the file properly.

Any ideas for what could be going wrong?

2
  • See stackoverflow.com/questions/19735250/… Commented Mar 31, 2015 at 14:58
  • @james-sunderland In a comment you mention that DocumentConverter.py should not be used concurrently is this a problem with libreoffice in listening mode, as I'd like to use it concurrently and/or do you have any source or more information about this? Commented Aug 4, 2019 at 10:48

1 Answer 1

1

Thanks alejandro, permission errors seemed to be the issue. Also I needed to move the .config/librroffice folder into apaches home directory.

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent!! Well done!! Please detail all steps in your answer and accept it to close the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.