Align two logos in same line in php word

2019-07-16 04:42发布

问题:

I added two logos using php word but both logos are not on the same line:

I want both logos to be on the same line like below:

Where's my mistake?

if (file_exists($logo)) {
    $table->addRow();
    // $table->addCell(20000, array('bgColor' => 'ffffff', 'spaceBefore' => 0, 'spaceAfter' => 0, 'spacing' => 0), $fontStyleIndexParaTitle)->addImage($logo, array('align' => 'center'));
    $table->addCell(20000, array('bgColor' => 'ffffff', 'spaceBefore' => 0, 'spaceAfter' => 0, 'spacing' => 0 ), $fontStyleIndexParaTitle)->addImage($logo, array('align' => 'left','width' => 70, 'height' => 70,));
}

if (file_exists($logo2)) {
    $table->addRow();
    // $table->addCell(20000, array('bgColor' => 'ffffff', 'spaceBefore' => 0, 'spaceAfter' => 0, 'spacing' => 0), $fontStyleIndexParaTitle)->addImage($logo, array('align' => 'center'));
    $table->addCell(20000, array('bgColor' => 'ffffff', 'spaceBefore' => 0, 'spaceAfter' => 0, 'spacing' => 0 ), $fontStyleIndexParaTitle)->addImage($logo2, array('align' => 'right', 'width' => 130));
}

回答1:

If one or both of the files exist, you only want to add a single row, so try this:

if (file_exists($logo) || file_exists($logo2)) {
    $table->addRow();
    if (file_exists($logo)) {
        $table->addCell(…);
    }
    if (file_exists($logo2)) {
        $table->addCell(…);
    }
}

Edit: This code (using phpoffice/phpword v0.16.0):

<?php
require_once 'vendor/autoload.php';

$phpWord = new \PhpOffice\PhpWord\PhpWord();

$logo = 'logo.bmp';
$logo2 = 'logo2.bmp';

$section = $phpWord->addSection();
$table = $section->addTable();

if (file_exists($logo) || file_exists($logo2)) {
    $table->addRow();
    if (file_exists($logo)) {
        $table->addCell(20000)->addImage($logo);        
    }
    if (file_exists($logo2)) {
        $table->addCell(20000)->addImage($logo2);        
    }
}

$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('aman121.docx');

Produces this Word document (my sample images are just solid colored rectangles):



标签: php phpword