Phpword: import multiple docx templates into the same document

Created on 16 Aug 2012  Â·  25Comments  Â·  Source: PHPOffice/PHPWord

described here http://phpword.codeplex.com/discussions/316482

Also add function to merge multiple docx files into one docx. description here http://www.phpdocx.com/documentation/api-documentation/merge-two-word-documents


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

Change Request Template Processor

Most helpful comment

I'm still waiting for this feature since 2014 😭

All 25 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?
Op 6 sep. 2012 22:21 schreef "SergeC" [email protected] het
volgende:

it sounds like project is dead.

—
Reply to this email directly or view it on GitHubhttps://github.com/PHPOffice/PHPWord/issues/8#issuecomment-8346006.

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 :wink:

Please, assign it to me.

Thank you! :+1:

@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 AbstractContainer.php. The goal is to integrate our new elements to private variable AbstractContainer::$elements.

For this, I needed to add a new public method, that would take an imported element and:

  • reassign current PhpWord as parent container,
  • walk on all sub-elements to reassign indexes and ids

In AbstractContainer.php:

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

+1

+1

+1

+1

+1

+1

+1

+1

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tasmer picture tasmer  Â·  3Comments

btry picture btry  Â·  4Comments

JFSebastianVillalba picture JFSebastianVillalba  Â·  3Comments

igorsantos07 picture igorsantos07  Â·  3Comments

Ryuzakix3 picture Ryuzakix3  Â·  6Comments