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

Create a sort of "mail merge" with a template #2368

Open
CPASimUSante opened this issue Jan 11, 2023 · 1 comment
Open

Create a sort of "mail merge" with a template #2368

CPASimUSante opened this issue Jan 11, 2023 · 1 comment

Comments

@CPASimUSante
Copy link

Description of the query

I have a docx (Word 2016), in which I set a template with placeholders, and with a header and footer. When I use the templateProcessor methods, I can't manage to respect the layout.
Is this achievable with some twisting the code, is it not implemented, or is it a bug or a feature missing ?

Steps to Reproduce

1 - Using the cloneBlock method, I manage to create ONE docx with every elements, but the layout is not respected

In the word

Page 1
${block_name}
some content ${placeholder1} with layout
...
some content ${placeholder2} with layout
...
Page 2
other content ${placeholderN}
${/block_name}

In the php

$template = 'path to template file';
$filename = '...';
$templateProcessor = new TemplateProcessor($template);
foreach ($elements as $element) {
	$replacements[] = $element;
}
$templateProcessor->cloneBlock('block_name', 0, true, false, $replacements);

2 - Using the setValue method, the layout stays perfect... but I have N documents generated, if there are N elements : this is useless !

$template = 'path to template file';
foreach ($elements as $element) {
	$filename = '...';
	$templateProcessor = new TemplateProcessor($template);

	$templateProcessor->setValue('placeholder1', $data1);
        ...
	$templateProcessor->setValue('placeholderN', $dataN);
	
	$templateProcessor->saveAs($filename);
}

Expected Behavior

With a php loop over the elements (Users, Session...), I want to generate a docx with as much as the loop number of element, WITH CONSERVATION OF THE TEMPLATE LAYOUT : if I expect an element to generate 2 pages, I want for instance the second element starting on page 3, even if "there is room left on page 2"

-----
Page 1
     content for element 1 first part
-----
Page 2
     content for element 1 second part
-----
Page 3
     content for element 2 first part
-----
Page 4
     content for element 2 second part

Current Behavior

The next element is not on the next page, but immediately after the end of the previous one (~ no equivalent to page break)

-----
Page 1
     content for element 1 first part
-----
Page 2
     content for element 1 second part
     content for element 2 first part (the beginning) <--- WRONG
-----
Page 3
     content for element 2 first part (the middle) 
-----
Page 4
     content for element 2 second part (the end) 

Context

  • PHP Version: 8.1
  • PHPWord Version: 1.0.0
@thomasb88
Copy link

I added a parameter $separator to the cloneBlock function,

and replaced

if ($replace) {
$this->tempDocumentMainPart = str_replace(
$matches[2] . $matches[3] . $matches[4],
implode('', $cloned),
$this->tempDocumentMainPart
);
}

by

if ($replace) {
$this->tempDocumentMainPart = str_replace(
$matches[2] . $matches[3] . $matches[4],
implode($xml_separator, $cloned),
$this->tempDocumentMainPart
);
}

Then for page_break,
$xml_separator = $this->xml_pagebreak('page');

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

No branches or pull requests

2 participants