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

Issue #260 - A quick hack around #564

Closed
wants to merge 3 commits into from
Closed

Conversation

OAFCROB
Copy link

@OAFCROB OAFCROB commented Jun 29, 2015

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');

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');
@OAFCROB OAFCROB changed the title #260 - A quick hack around Issue #260 - A quick hack around Jun 29, 2015
OAFCROB and others added 2 commits July 6, 2015 11:24
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.
@juanagu
Copy link

juanagu commented Nov 20, 2015

Hi,
I have a problem when the index is composed by another index,
worked in #1#n but not work when first > 1 e.g:
#2#1, #2#2 , #3#1 Any solution? (sorry for my english)

@OAFCROB
Copy link
Author

OAFCROB commented Nov 24, 2015

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

@Salman456
Copy link

Hi,
@OAFCROB I have exactly the same requirements as you mentioned but I have two problems. Can you please post your TemplateProcessor.php code or tell me how to access it because I am new to GitHub. Secondly, if you can post your word template that would be awesome.

Cheers!

@OAFCROB
Copy link
Author

OAFCROB commented Jul 18, 2016

@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.

$tags['tags']['${idworkLineRemedyCode#'.$i.'#'.$ii.'}'] = 'value';

template

@Salman456
Copy link

Salman456 commented Jul 19, 2016

@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 = new \PhpOffice\PhpWord\Template('assets/documents/resources/sales_narrative.docx');

$templateProcessor->setImageValue('testPhoto#1', 'image1.jpeg','favicon1.png');

@OAFCROB OAFCROB closed this Feb 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants