phppdfdocxphpwordunoconv

The image showing in docx file is not visible in pdf when converted using unoconv


The image showing in docx file is not visible in pdf when converted using unoconv where the image is displayed using

$document = new \PhpOffice\PhpWord1\TemplateProcessor($docx_temp_file_path);
$imagePath = '/abc.png';
if (file_exists($imagePath)) {
    $document->setImageValue($key, $imagePath);
}

Here is the unoconv code

public function docxToPdf( $docx_file_path, $pdf_file_path )
{
    $pythonPath = '/usr/bin/python';
    $unoconvPath = '/usr/bin/unoconv';
    $command = sprintf(
        '"%s" "%s" -f pdf "%s" 2>&1', 
        $pythonPath,
        $unoconvPath,
        $docx_file_path
    );

    exec($command, $output, $return);
    //
    if (is_array($output) && !empty($output[0])) {
        throw new Exception(json_encode(implode('; ', $output), true));
    }
    return true;
}

Here am attaching sample link of docx filedocx file


Solution

  • Your generated Word document contains 3 images in the page footer. The footer1.xml file contains this:

    <w:pict><v:shape type="#_x0000_t75" style="width:;height:70px" stroked="f"><v:imagedata r:id="rId1" o:title=""/></v:shape></w:pict>
    <w:pict><v:shape type="#_x0000_t75" style="width:;height:70px" stroked="f"><v:imagedata r:id="rId3" o:title=""/></v:shape></w:pict>
    <w:pict><v:shape type="#_x0000_t75" style="width:;height:70px" stroked="f"><v:imagedata r:id="rId2" o:title=""/></v:shape></w:pict>
    

    All images have this style:

    width:;height:70px
    

    As you can see, the width is missing. In that case, Word uses a default value, but LibreOffice doesn't show the image. Since unoconv uses LibreOffice to convert to PDF, the images are missing in the resulting document.

    I could reproduce the issue by specifying this in the template:

    ${key::70:false}
    

    You can fix it either by specifying the width:

    ${key:70:70:false}
    

    or by removing the ratio parameter:

    ${key::70}