Align two logos in same line in php word

只谈情不闲聊 提交于 2019-12-11 01:14:21

问题


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



来源:https://stackoverflow.com/questions/56951185/align-two-logos-in-same-line-in-php-word

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!