Search code examples
nginxphp-7libreofficeconvertersunoconv

Convert documents into pdf using unoconv through Symphony Process Component


I'm trying to convert word documents into PDF using unoconv in PHP. When I run the following command in CLI it works properly without any error:

export HOME=/tmp/converts && unoconv -f pdf -o ~/da063764384fb612971bfc92c52c40fc.pdf ~/da063764384fb612971bfc92c52c40fc.docx

But the problem is when I try to run it in PHP as follow:

$env  = ['PATH' => '/sbin:/bin:/usr/sbin:/usr/bin'];
$bash = "export HOME=${tmpDir} && /usr/bin/unoconv -f pdf -o ~/${fileName}.pdf $srcFile";$process = new Process($bash);
$process->setTimeout(120);
$process->run(null, $env);

$output = "\nOutput: " . $process->getOutput();
$error  = "\nError: " . $process->getErrorOutput();

This results:

Output:
Error: unoconv: RuntimeException during import phase: Office probably died. Binary URP bridge disposed during call

Any one has any idea how can I fix the issue!?


Solution

  • unoconv spawns a child libreOffice process, and then connects to that process via port 2002. strangely the port never changes. unoconv cannot therefore run in parallel, if you have one instance running all the others fail. That was the thing happened on my server.

    I ran bellow command on CLI to find all libreOffice processes:

    ps aux | grep libre
    

    Then killed those process by running:

    ps aux | grep -i libre | awk {'print $2'} | xargs kill -9
    

    Generally speaking, using another tool(s) which supports multi treading is a better solution for such cases.