在php word的同一行中对齐两个徽标

时间:2019-07-09 11:06:13

标签: php phpword

我使用php word添加了两个徽标,但是两个徽标不在同一行:

not-same-line

我希望两个徽标位于同一行,如下所示:

same-line-logo

我的错误在哪里?

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 个答案:

答案 0 :(得分:1)

如果存在一个或两个文件,则只想添加一行,因此请尝试以下操作:

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

编辑:此代码(使用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');

生成此Word文档(我的示例图像只是纯色矩形):

word document output