5

I would like to automate the exportation of .odf file to .pdf.

I tried something called unoconv.

When typed in the Windows command prompt, everything is right.

>"C:\Program Files (x86)\OpenOffice 4\program\python.exe" "C:\Program Files (x86)\OpenOffice 4\program\unoconv-master\unoconv" -f pdf path/to/myfile.odt

Once used in a PHP script, nothing works anymore. No error appears.

exec('"C:\Program Files (x86)\OpenOffice 4\program\python.exe" "C:\Program Files (x86)\OpenOffice 4\program\unoconv-master\unoconv" -f pdf path/to/myfile.odt');

//or

shell_exec('"C:\Program Files (x86)\OpenOffice 4\program\python.exe" "C:\Program Files (x86)\OpenOffice 4\program\unoconv-master\unoconv" -f pdf path/to/myfile.odt')

I also tried to escape \ or using ' and " differently but none of my tests were conclusive.

$test = exec($cmd, $output, $return);
echo var_dump($test);
echo var_dump($output);
echo var_dump($return);

Give me

//echo var_dump($test)give nothing

//echo var_dump($ouput);
array (size=0)
  empty

//echo var_dump($return);
int 1

This sounds like a permissions issue but I also checked this and all the folders are accessible for PHP.

Atnaize
  • 1,766
  • 5
  • 25
  • 54
  • Try with a very simple command first, you have to sort out quote chars and escaping to get the syntax right. – arkascha Dec 14 '15 at 11:43

2 Answers2

0

I would add this as a comment instead but not enough rep...

I had this issue when changing from a WAMP server to IIS, and as you said it was a permissions issue. It turns out the I had to give the "IUSR" user and the "IIS_IUSRS" group full permissions for it to execute.

Later on I encountered something similar and I believe I decided to simply change the user IIS runs as to Administrator, but this is probably not recommended.

Of course this will be irrelevant if you're not using IIS, but I'll leave this here just in case anyone else is.

Sworrub Wehttam
  • 588
  • 4
  • 14
0

By testing exotic code, the python.exe was overwrited and unusable. The code in the initial post was nearly correct. My bad.

For curious people, this is my final code with an easy-to-use function

createPDF.bat

@echo off
set arg1=%1
set arg2=%2

cd "C:\Program Files (x86)\OpenOffice 4\program\"

python.exe "C:\Program Files (x86)\OpenOffice 4\program\unoconv-master\unoconv" -f pdf -o %arg2% %arg1%

createPDF.php

function createPDF($from, $to)
{
    //Launch the .bat, do not forget the double backslash
    $handle = popen("start /B path\\to\\createPDF.bat ".$from." ".$to, "r");

    //Debug if needed
    //echo "'$handle'; " . gettype($handle) . "\n";
    //$read = fread($handle, 2096);
    //echo $read;

    //Close the socket
    pclose($handle);
}

And now when I have to convert an odt file into pdf I just have to do the following

test.php

include_once($_SERVER['DOCUMENT_ROOT']."path\to\createPDF.php"); 

//createPDF(source, destination), do not forget the double backslash (\\)
createPDF("E:\\A\\strange\\path\\to\\Report.odt" , "D:\\Final_report");
Atnaize
  • 1,766
  • 5
  • 25
  • 54