Error converting docx to pdf using Unoconv



PHP Snippet 1:

<?php
try {
    require 'Unoconv.php';
    use Unoconv\Unoconv;
    
    $map1 = $_SESSION['companyid'];
    $filename = $result1['filename'];
    
    $originFilePath = './uf/doc/'.$map1.'/word/'.$filename.'.docx';
    $outputDirPath  = './uf/doc/'.$map1.'/pdf/'.$filename.'.pdf';
    
    Unoconv::convertToPdf($originFilePath, $outputDirPath);
    
    header("Content-type:application/pdf");
    header("Content-Disposition:attachment;filename=".$filename.".pdf");
    readfile($outputDirPath);
} catch (\Exception $e) {
    die($e->getMessage());
}

PHP Snippet 2:

#!/usr/bin/env python

PHP Snippet 3:

#!/opt/libreoffice7.1/program/python

PHP Snippet 4:

    $dir    = dirname($docfile);
    // Libreoffice saves here
    $pdf    = $dir . DIRECTORY_SEPARATOR . basename($docfile, '.docx').'.pdf';
    $ret = shell_exec("export HOME={$dir} && /usr/bin/libreoffice --headless --convert-to pdf --outdir '{$dir}' '{$docfile}' 2>&1");
    if (file_exists($pdf)) {
        rename($pdf, $realPDFName);
    } else {
        return false;
    }
    return true;

PHP Snippet 5:

$originFilePath = './uf/invoice/17/word/202100021.docx';
$outputDirPath  = './uf/invoice/17/pdf/202100021.pdf';

$dir    = dirname($originFilePath);
$pdf    = $dir . DIRECTORY_SEPARATOR . basename($originFilePath, '.docx').'.pdf';
$ret = shell_exec("export HOME={$dir} && /usr/bin/libreoffice --headless --convert-to pdf --outdir '{$dir}' '{$originFilePath}' 2>&1");
// $ret will contain any errors
if (!file_exists($pdf)) {
    die("Conversion error: " . htmlentities($ret));
}
rename($pdf, $outputDirPath);

header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename=202100021.pdf");
readfile($outputDirPath);

PHP Snippet 6:

<?php
header('Content-Type: text/plain');
print "If this works:\n";
system('which libreoffice 2>&1');
print "\n-- otherwise a different attempt, returning too much information --\n";
system('locate libreoffice');