Align two logos in same line in php word

2019-07-16 04:03发布

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

not-same-line

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

same-line-logo

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));
}

标签: php phpword
1条回答
beautiful°
2楼-- · 2019-07-16 04:51

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):

word document output

查看更多
登录 后发表回答