Comments (28)
I'm still waiting for this feature since 2014 😭
from phpword.
Has there been any progress with copying a template to new section in the same document?
from phpword.
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.
from phpword.
I would also like to import a template, just for one section (for generating multiple invoices in 1 file)
from phpword.
it sounds like project is dead.
from phpword.
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//issues/8#issuecomment-8346006.
from phpword.
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?
from phpword.
Or 0.7.7 :)
from phpword.
Moved to the version 0.7.7 😉
from phpword.
Please, assign it to me.
from phpword.
Thank you! 👍
from phpword.
@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.
from phpword.
Can't assign to people not in the team, maybe you could request @Progi1984 to be part of the team
from phpword.
Ah, I see. Ok. Thanks. I'll submit the request.
from phpword.
@RomanSyroeshko assigned
from phpword.
+1
from phpword.
+1
from phpword.
+1
from phpword.
+1
from phpword.
+1
from phpword.
+1
from phpword.
+1
from phpword.
+1
from phpword.
+1
from phpword.
+1
from phpword.
+1
from phpword.
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 elements
property of the AbstractElements is private, so elements cannot be added with $parent->elements[] = $element
. Another failed experiment I conducted was this:
function addElementsToSection($section, array $elements) {
foreach ($elements as $element) {
reassignElementProperties($section, $element);
// The elements array is protected... so workaround this by
// inserting a new paragraph element and overriding that fresh
// element with ours.
$new_element = $section->addText('dummy');
$new_element = $element;
}
}
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.
from phpword.
+1
from phpword.
Related Issues (20)
- Why not use a better HTML parser?
- Cant find how change list numbering font-size and font-family?
- ZipArchive::close(): Renaming temporary file failed: Permission denied{ "message": "Invalid or uninitialized Zip object" HOT 4
- IOFactory::load failed and the funtion setCommentReference not working HOT 2
- Special char (&) is breaking word document HOT 1
- IOFactory::Load fails with "Cannot add Title in Header." HOT 1
- Not able to add styles to some tags and listitems in addhtml
- empty li on odf (docx ok)
- dompdf version bump to include 3.0 when?
- Trouble Interacting with Dropdown FormField
- Error reporting of cloneRow
- Multicolumn layout is not working. HOT 3
- Text aligment JUSTIFY not wokring in Microsoft Word
- Add the ability to add alt text to images HOT 1
- Issue with Footnotes Display in PhpWord
- How to show hidden columns of a table in "phpoffice/phpword"? HOT 5
- First footnote displays as a black line with no number
- Changing Macro chars to {{ VAR }} doesn't work
- TemplateProcessor cloneBlock Missing template Styles
- Update pre-defined table of content with template processor HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from phpword.