-
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
Issue #260 - A quick hack around #564
Conversation
A quick hack solution I came up with today. This isn't pretty in the slightest and is my first pull request so apologise if I've done it wrong. I normally work on BitBucket. Example Usage ------------------ $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('C:\Users\Robert\Desktop\sample.docx'); $templateProcessor->cloneRow('rowValue', 10); $templateProcessor->setValue('test#1', htmlspecialchars('Text goes here')); $templateProcessor->setImageValue('testPhoto#1', 'image3.png', 'my-other-image.jpg'); $templateProcessor->setValue('test#2', htmlspecialchars('Text2 goes here')); $templateProcessor->setImageValue('testPhoto#2', 'image4.png', my-image.jpg');
Image sizes now come from the actual image instead of being hard coded.
$xml may not be created if there isn't any images causing an error.
I've not tired the scenario of images within a multi-dimensional array of data to be honest with you, but have you managed to get the sub array of data to populate with non-image related data? For example have you got test#2#1 and test#2#2 to replace with simple text data? If not we would need to start there and get your data to populate, this is something I have managed to achieve. I hope the example below help with how I've parsed the tags. $tags = ['row' => [['clone' => '${lineRow}', 'rows' => 0]],
'tags' => []];
$i = 0;
foreach ($rowData as $key => $value) {
$i++;
$tags['tags']['${LineRow#'.$i.'}'] = '';
$tags['tags']['${LineText#'.$i.'}'] = 'Text data '.$i;
$tags['row'][] = ['clone' => '${subLineCode#'.$i.'}',
'rows' => count($value['subData'])];
$ii = 0;
foreach ($value['subData'] as $subKey => $subData) {
$ii++;
$tags['tags']['${subLineCode#'.$i.'#'.$ii.'}'] = 'sub text '.$ii;
}
$tags['row'][0]['rows'] = $i;
} Once the tag data is parsed I call into a method that looks for the "clone" tags clones the row by the row tag to give me the correct number of rows. I then go about replacing the tags in the template with my tags data array. I've done quite a bit of work on this and I have refactored my forked repo to best suit my needs. Put on some extra info on the wiki page of my forked rep https://github.com/OAFCROB/PHPWord |
Hi, Cheers! |
@Salman456 sorry but GitHub won't let me upload a docx file for some reason but here is a screenshot of the implemented template. With remedies bit on the image being the repeating section. |
@OAFCROB Can you please have look at my template at https://dl.dropboxusercontent.com/u/43068636/sales_narrative.docx and output file at https://dl.dropboxusercontent.com/u/43068636/out.docx. I am able clone rows with variables testPhoto#1 and testPhoto#2 but inserting images make the out corrupt. I used the code as $templateProcessor->setImageValue('testPhoto#1', 'image1.jpeg','favicon1.png'); |
A quick hack solution I came up with today. This isn't pretty in the
slightest and is my first pull request so apologise if I've done it
wrong. I normally work on BitBucket.
Example Usage
$templateProcessor = new
\PhpOffice\PhpWord\TemplateProcessor('C:\Users\Robert\Desktop\sample.docx');
$templateProcessor->cloneRow('rowValue', 10);
$templateProcessor->setValue('test#1', htmlspecialchars('Text goes
here'));
$templateProcessor->setImageValue('testPhoto#1', 'image3.png',
'my-other-image.jpg');
$templateProcessor->setValue('test#2', htmlspecialchars('Text2 goes
here'));
$templateProcessor->setImageValue('testPhoto#2', 'image4.png',
my-image.jpg');