-
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
import multiple docx templates into the same document #8
Comments
I would also like to import a template, just for one section (for generating multiple invoices in 1 file) |
it sounds like project is dead. |
Do you know of an active fork or good alternative?
|
Well, looks like the project is alive again :) Once the reader is build (see #70), your need is easier to implement. @Progi1984, shouldn't we put this to 0.8? |
Or 0.7.7 :) |
Moved to the version 0.7.7 😉 |
Please, assign it to me. |
Thank you! 👍 |
@Progi1984 Can we start assigning issues? I see that you've assigned some issues to you. We also love to have assignments :) I think @RomanSyroeshko is interested in this one. |
Can't assign to people not in the team, maybe you could request @Progi1984 to be part of the team |
Ah, I see. Ok. Thanks. I'll submit the request. |
@RomanSyroeshko assigned |
Has there been any progress with copying a template to new section in the same document? |
I managed to import some Docx content by playing with For this, I needed to add a new public method, that would take an imported element and:
In public function addElementsFromAnotherPhpWord(array $elements) {
$count = count($elements);
for ($i = 0; $i < $count; ++$i) {
$element = $elements[$i];
$element->setParentContainer($this);
$this->reassignElementProperties($this, $element);
$this->elements[] = $element;
}
}
protected function reassignElementProperties($parent, $element) {
// Set parent container (code from addElement() )
$element->setParentContainer($parent);
$element->setElementIndex($parent->countElements() + 1);
$element->setElementId();
// Recursive on sub-elements
$count = count($element->elements);
for ($i = 0; $i < $count; ++$i) {
$this->reassignElementProperties($element, $element->elements[$i]);
}
} To call the method: $PHPWord = new \PhpOffice\PhpWord\PhpWord();
$section = $PHPWord->addSection();
$docImport = \PhpOffice\PhpWord\IOFactory::load('/absolute/path.docx');
$sectionsImport = $docImport->getSections();
$sectionsCount = count($sectionsImport);
for ($iSection = 0; $iSection < $sectionsCount; ++$iSection) {
$curSection = $sectionsImport[$iSection];
$elements = $curSection->getElements();
$section->addElementsFromAnotherPhpWord($elements);
} I share this as a first guess on how the import could be done. This is not perfect at all! This does not for example reassign global styles ids. You will only have 'paragraph' styles. |
I'm still waiting for this feature since 2014 😭 |
+1 |
1 similar comment
+1 |
+1 |
+1 |
7 similar comments
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
Thanks for sharing this! I thought I was about to get away with defining these two procedures locally: /**
* Attach elements of a different PhpWord object to a container
* object.
*
* @param \PhpOffice\PhpWord\Element\AbstractElement
* @param array \PhpOffice\PhpWord\Element\AbstractElement
*/
// Adapted from: https://github.com/PHPOffice/PHPWord/issues/8#issuecomment-243126241.
function addElementsFromAnotherPhpWord($parent, array $elements) {
foreach ($elements as $element) {
reassignElementProperties($parent, $element);
$parent->elements[] = $element;
}
}
/**
* Recursively adjust the parent containers of an element.
*
* @param \PhpOffice\PhpWord\Element\AbstractElement
* @param array(\PhpOffice\PhpWord\Element\AbstractElement)
*/
// Adapted from: https://github.com/PHPOffice/PHPWord/issues/8#issuecomment-243126241.
function reassignElementProperties($parent, $element) {
// Set parent container (similar to what
// AbstractElement::addElement, a protected method, does).
$element->setParentContainer($parent);
$element->setElementIndex($parent->countElements() + 1);
$element->setElementId();
// Recurse sub-elements.
foreach ($element->elements() as $child) {
reassignElementProperties($element, $child);
}
} But it turns out that this doesn't fly because the
Thinking I may be able to mutate the element object. That also didn't have the intended effect (the document would contain 'dummy' paragraphs rather than the $element I thought I had overridden it with). Perhaps that's still possible somehow? I'm no PHP programmer. |
+1 |
described here http://phpword.codeplex.com/discussions/316482
Also add function to merge multiple docx files into one docx. description here https://www.phpdocx.com/documentation/cookbook/merge-docx-documents
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
The text was updated successfully, but these errors were encountered: