Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addImage result blank in $templateProcessor->setComplexBlock #1964

Open
sabardotnet opened this issue Nov 5, 2020 · 5 comments
Open

addImage result blank in $templateProcessor->setComplexBlock #1964

sabardotnet opened this issue Nov 5, 2020 · 5 comments

Comments

@sabardotnet
Copy link

sabardotnet commented Nov 5, 2020

`<?php
use PhpOffice\PhpWord\Element\Field;
use PhpOffice\PhpWord\Element\Table;
use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\SimpleType\TblWidth;

include_once 'Sample_Header.php';

// Template processor instance creation
echo date('H:i:s'), ' Creating new TemplateProcessor instance...', EOL;
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('resources/Sample_40_TemplateSetComplexValue.docx');

$title = new TextRun();
$title->addText('This title has been set ', array('bold' => true, 'italic' => true, 'color' => 'blue'));
$title->addText('dynamically', array('bold' => true, 'italic' => true, 'color' => 'red', 'underline' => 'single'));
$templateProcessor->setComplexBlock('title', $title);

$inline = new TextRun();
$inline->addText('by a red italic text', array('italic' => true, 'color' => 'red'));
$templateProcessor->setComplexValue('inline', $inline);

$table = new Table(array('borderSize' => 12, 'borderColor' => 'green', 'width' => 6000, 'unit' => TblWidth::TWIP));
$table->addRow();
$table->addCell(150)->addText('Cell A1');
$table->addCell(150)->addText('Cell A2');
$table->addCell(150)->addText('Cell A3');
$table->addRow();
$table->addCell(150)->addText('Cell B1');
$table->addCell(150)->addText('Cell B2');
$table->addCell(null)->addImage('./resources/_earth.jpg', array('width' => 250, 'height' => 250, 'alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER));

$templateProcessor->setComplexBlock('table', $table); // this create black photo

$field = new Field('DATE', array('dateformat' => 'dddd d MMMM yyyy H:mm:ss'), array('PreserveFormat'));
$templateProcessor->setComplexValue('field', $field);

// $link = new Link('https://github.com/PHPOffice/PHPWord');
// $templateProcessor->setComplexValue('link', $link);

echo date('H:i:s'), ' Saving the result document...', EOL;
$templateProcessor->saveAs('results/Sample_40_TemplateSetComplexValue.docx');

echo getEndingNotes(array('Word2007' => 'docx'), 'results/Sample_40_TemplateSetComplexValue.docx');
if (!CLI) {
include_once 'Sample_Footer.php';
}
`
according to sample i put some code to add image to my table but the result blank image.

@ChrisF23
Copy link

ChrisF23 commented Jan 12, 2021

I ran into a similar problem and found this workaround:

Instead of adding the image directly to the cell (or any other container)...
$table->addCell(...)->addImage(...);

add a text defining a macros that identifies that image.
$table->addCell(...)->addText('${image_identifier}');

Then, when all your images macros are defined, call setComplexBlock to write them into the document.
$templateProcessor->setComplexBlock('table', $table);

Finally, use setmageValue on every image macros you defined before. For example:
$templateProcessor->setImageValue('image_identifier', ...);

@xenofx
Copy link

xenofx commented Jan 18, 2021

Same problem, if my template have photos then addimage copy the one photos and show it, but template have not photos then addimage not working and generated file is damaged, recovered file is not shown images.

same code:

$ceklist_tablosu->addCell(400, array('vMerge' => 'restart'))->addImage('C:/xampp/htdocs/images/check.gif',array('width'  => 20,'height' => 20,'align'  => 'center'));

@SteJaySulli
Copy link

SteJaySulli commented Mar 19, 2021

I've run into something similar; I am trying to use a TextRun to contain text and a list of images after it - the space the images would have taken up is visible in the document, but no image is visible or seemingly present:

$block = new TextRun();
$block->addText("Some text here");
$block->addTextBreak();
if(count($images) > 0) {
  $block->addText("Attached images:");
  $block->addTextBreak();
  foreach($images as $image) {
    $block->addImage($image, ["width"=>300, "height"=>300]);
  }
}
$template->setComplexValue("replaceme",$block);
// I have also tried this with the same result, although I'm not sure it's correct:
// $template->setComplexBlock("replaceme",$block);

Additional information

This appears to be caused because the image is not added to the relationships and hence is not included in the document's assets.

I have managed to work around this using code like this:

// $image is the PHPWord\Element\Image object I am trying to insert
// $url is the file path for the image I am trying to insert
function relateImage($image, $url) {
        $partFileName = $this->getMainPartName();
        $rid="rId".$this->getNextRelationsIndex($partFileName);
        
        $this->addImageToRelations(
            $partFileName,
            $rid,
            $url,
            $image->getImageType()
        );
    }

This is a pretty dirty hack and probably won't work for most people, but it might help PHPWord developers debug the issue further - In particular I can see that as I am creating the TextRun outside the context of a document, it is not going to know which word document to add the file to and as such won't do it when I susequently do ->setComplexValue().

@hipig
Copy link

hipig commented Sep 6, 2022

I ran into a similar problem and found this workaround:

Instead of adding the image directly to the cell (or any other container)... $table->addCell(...)->addImage(...);

add a text defining a macros that identifies that image. $table->addCell(...)->addText('${image_identifier}');

Then, when all your images macros are defined, call setComplexBlock to write them into the document. $templateProcessor->setComplexBlock('table', $table);

Finally, use setmageValue on every image macros you defined before. For example: $templateProcessor->setImageValue('image_identifier', ...);

Why can't I replace it correctly according to your code, image_identifier still exists

@geekdenz
Copy link

I worked around it, by simply doing:

In Word document:

${image1}${image2}...{$image100}

100 can be any number sufficient for your use case and then writing 2 loops:

$i = 0;
foreach ($imagePaths as $imagePath) {
  $i++;
  $imageVar = 'image' . $i;
  $templateProcessor->setImageValue($imageVar, ['path' => $imagePath, 'width' => 400, 'height' => 400]);
}

for ($j = $i; $j <= 100; $j++) {
  $imageVar = 'image' . $i;
  $templateProcessor->setValue($imageVar, '');
}

In the whole project I learnt a lot about PHPWord and the Word docx format. If I get enough motivation i.e. votes here and/or money I will be happy to fix the root problem of this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

6 participants