-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Comments
I ran into a similar problem and found this workaround: Instead of adding the image directly to the cell (or any other container)... add a text defining a macros that identifies that image. Then, when all your images macros are defined, call setComplexBlock to write them into the document. Finally, use setmageValue on every image macros you defined before. For example: |
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:
|
I've run into something similar; I am trying to use a $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 |
Why can't I replace it correctly according to your code, |
I worked around it, by simply doing: In Word document:
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. |
`<?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.
The text was updated successfully, but these errors were encountered: