phpoffice / phpword Goto Github PK
View Code? Open in Web Editor NEWA pure PHP library for reading and writing word processing documents
Home Page: https://phpoffice.github.io/PHPWord/
License: Other
A pure PHP library for reading and writing word processing documents
Home Page: https://phpoffice.github.io/PHPWord/
License: Other
There is some code here: http://phpword.codeplex.com/discussions/232684
And the phpWord workitem here: http://phpword.codeplex.com/workitem/16
Currently, Template class has the "save()" method with the followed signature.
/**
* Save Template
*
* @param string $strFilename
*/
public function save($strFilename)
{
...
}
In most cases that's fine, but in some situations this approach it's not optimal enough.
For example, we have a batch of templates and we need to perform the followed actions with them.
a) Substitute macroses in each template.
b) Pack all the result documents in ZIP-archive and move this archive somewhere.
In such situtation Template class of current implementation allows us to do this, but it will require to save processed templates somewhere which involves unnecessary file copying. Moreover, we will have to delete these garbage files after archiving.
So, suggestion is to give options.
/**
* Save Template
*
* @return string
*/
public function save() {
...
}
/**
* Save Template As...
*
* @param string $strFilename
*/
public function saveAs($strFilename) {
...
}
Link :
Format :
Validator :
Samples :
ToDo :
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
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.
Sometimes we need to perform more complex actions with document template than just simple replace of ${} parameters. XSLT really helps in such situations.
This issue is a proposal of the correspondent class method implementation with the followed signature.
/**
* Applies XSL style sheet to XML template.
*
* @param DOMDocument &$xslDOMDocument
* @param array $xslOptions = array()
* @param string $xslOptionsURI = ''
*/
public function applyXslStyleSheet(&$xslDOMDocument, $xslOptions = array(), $xslOptionsURI = '')
{
...
}
Assumption: we have XSL style sheet loaded as DOMDocument before the method call.
Requirement: "php_xml.dll" library.
Limitation: XSLT 1.0, XPath 1.0 (because of XSLTProcessor).
See details here: #58 (comment).
Task: review the source code and find out what can be reused to make PHPWord/PHPOffice better.
This is a scope of 0.7.2. version.
I'm using the template stuff in order to replace some values, but the following code breaks the template (anything in the template after the &
symbol is not shown):
$document = $PHPWord->loadTemplate($templatePath);
$document->setValue('type', "some value & foobar");
I guess the &
is a specific character in xml word files, like it can be in html
The workaround I'm currently using is to wrap the value into an htmlspecialchars
and all is ok afterwards:
$document = $PHPWord->loadTemplate($templatePath);
$document->setValue('type', htmlspecialchars("some value & foobar"));
=> why not add this htmlspecialchars
right into the setValue
function ?
Thanks for your feedback
I'm using the GIT branch "custom-numbered-lists" of your PHPWord code.
I'm generating several docx documents in a row inside my php module. The documents contain images. I realised that when generating the second document the PHPWord_Writer_Word2007 tried to access the images of the first document. I think this is due to the fact that the static information in PHPWord_Media is not reset.
I wrote a simple reset function and now everything works as expected.
public static function resetMedia() {
self::$_sectionMedia = array(
'images' => array(),
'embeddings' => array(),
'links' => array());
self::$_headerMedia = array();
self::$_footerMedia = array();
}
I suppose such a method should actually be called when calling
PHPWord_IOFactory::createWriter(..., 'Word2007');
I don't know if this also affects other branches of the code.
Does anyone have a doc that could be used with this example?
https://github.com/PHPOffice/PHPWord/blob/develop/src/Examples/Template.php
$document = $PHPWord->loadTemplate('Template.docx');
cc @Progi1984
The support has been done for the Word 2007 Writer but not for ODT and RTF.
Link : #22 (comment)
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
WPS Writer (#69), Daisy XML Writer (#54), and ePub2 Writer (#55) will be implemented later.
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
how do I create cells with rowspan more than 1?
Sometimes we need to replace only several string occurrences in text. Current implementation of the corresponded method of Template class looks like as followed
/**
* Set a Template value
*
* @param mixed $search
* @param mixed $replace
*/
public function setValue($search, $replace)
{
...
$this->_documentXML = str_replace($search, $replace, $this->_documentXML);
}
This gives us no chance to replace, for example, the first occcurrence only.
This issue is a proposal of setValue method inhancement by implementing the followed signature:
/**
* Set a Template value
*
* @param mixed $search
* @param mixed $replace
* @param integer $limit
*/
public function setValue($search, $replace, $limit = -1) {
...
}
The files PHPWord create seem to be malformed for OpenOffice because PHPWord formats the xml with spaces and newlines. Word removes this lines and shows the document how its supposed to be, but OO inserts a newline for every newline in the xml-doc in the document is shows. Changing these line in Shared/XMLWriter.php fix this.
$this->_xmlWriter->setIndent(false);
$this->_xmlWriter->setIndentString('')
There is a comment in the code saying this might need to be turned off for production.
As soon as minimal supported PHP version of PHPWord is 5.3 for now, it would be really cool to refactor source code and use new features (namespaces, for example).
Migration guide from PHP 5.2.x to PHP 5.3.x could be found here. Tasks to do:
@Progi1984, @gavroche, do we plan to do something like that?
The code to do this is here: http://phpword.codeplex.com/discussions/316726
Here is the related 'workitem' on the phpWord site: http://phpword.codeplex.com/workitem/35
Alignment of the PreserveText can only be done by entering an emty fontstyle array.
Leaving out the fontsyle array will result in left alignment of the text.
Temporary files naming logic in PHPWord_Template can lead to a collision
The problem is in the followed peace of code:
public function __construct($strFilename) {
$path = dirname($strFilename);
$this->_tempFileName = $path.DIRECTORY_SEPARATOR.time().'.docx';
copy($strFilename, $this->_tempFileName); // Copy the source File to the temp File
$this->_objZip = new ZipArchive();
$this->_objZip->open($this->_tempFileName);
$this->_documentXML = $this->_objZip->getFromName('word/document.xml');
}
As we may see, temporary files are named using "the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)". In this case, if we have several requests per one second, the code will generate us files with exactly the same names, and that may lead to some confusions. So, we need to generate really unique filenames. tempnam() function meets this requirement.
Moreover, according to ETL, it's not correct to transform data right in the source we extract this data from. But this is what the peace of code does while creating temporary file in the datasource directory. It's not correct to generate temporary files in the destination directory too. There may be tons of such destination directories and, in case of bugs in our code, they may be populated with garbage temporary files. So, we need separate temporary directory for our temporary files, which allows us easily maintain it's content (when deal with garbage). sys_get_temp_dir() function meets this requirement.
Finally, what we need to do is to correct couple lines of code. :)
First I want to say congratulations on the project too. Now one thing that was sorely lacking in the styles used in the table was colspan and rowspan properties because they are widely used in creating tables. Now if you go as a way to debug irrei also assist in this project because it is of much interest
The readme file refers this example:
$myTextElement->setBold();
$myTextElement->setName('Verdana');
$myTextElement->setSize(22);
However these functions are (no longer?) implemented. I'm not sure if they should be implemented or if the example should be changed.
After doing Sample_02_TabStops.php
i got:
luuk@opensuse:~/public_html/PHPWord-master/samples> ll Sample_02*
-rw-r--r-- 1 luuk users 8309 jan 4 18:31 Sample_02_TabStops.docx
-rw-r--r-- 1 luuk users 3052 jan 4 18:31 Sample_02_TabStops.odt
-rwxrwxr-x 1 wwwrun users 1807 dec 17 11:45 Sample_02_TabStops.php
-rw-rw-r-- 1 wwwrun users 307 jan 4 18:31 Sample_02_TabStops.rtf
Is it not odd that the docx and odt file are created with user-rights, and that the rtf includes the group rights?
Is there a way to restart the ListItem numbering (lettering)?
Using this code snippet:
$numberStyleList = array('listType' => \PHPWord_Style_ListItem::TYPE_NUMBER);
$alphStyleList = array('listType' => \PHPWord_Style_ListItem::TYPE_ALPHANUM);
$section->addListItem('One', 0, null, $numberStyleList);
$section->addListItem('Two', 0, null, $numberStyleList);
$section->addListItem('Alpha', 1, null, $alphStyleList);
$section->addListItem('Beta', 1, null, $alphStyleList);
$section->addListItem('Three', 0, null, $numberStyleList);
$section->addListItem('Charlie', 1, null, $alphStyleList);
Currently it would output this:
1. One
2. Two
A. Alpha
B. Beta
3. Three
C. Charlie
I want to output this:
1. One
2. Two
A. Alpha
B. Beta
3. Three
A. Charlie
Is there some other code call that needs to be made? Or is this a limitation of PHPWord?
Is there a way to save a PHPWord Template Object with Word2007 Writer, for the compatibility ?
Thanks
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
How do i convert between centimeters
and the $width used in addImage() and addCell() ?
it would be nice to be able to use PHPWord with out needing ZipArchive. PHPExcel does this by using the PCLZip library.
On the odd occasion you want to turn some HTML into a PHPWord
object, it would be nice to have a utility for doing so.
Should the namespacing be done like this:
namespace PHPWord;
class PHPWord
{}
which will result in using the lib like this: new PHPWord\PHPWord();
OR
namespace PHPOffice\PHPWord;
class PHPWord
{}
which will result in using the lib like this: new PHPOffice\PHPWord\PHPWord();
Both of which are PSR-0 and PSR-4 compliant.
I'm using the loadTemplate()
function to set values in a template.
It works great except for values that are in header / footer. They're not replaced
The reason is because PHPWord only replaces values in the body, and not in the header / footer.
It has been quoted here, with a patch to make it work:
http://phpword.codeplex.com/discussions/236472
with a more generic way here: https://phpword.codeplex.com/workitem/69
Would be nice to have this feature in the official PHPWord :)
textBreak's don't seem to follow any sort of style. I could be wrong. I'd like to think it'd follow a paragraphStyle or the last textStyle used. textBreaks for my document seem to large.
I cannot use UTF8 signs, like " ' " or โฌ, but get a ? instead.
IsUTF8 should support Cyrillic UTF-8 and return TRUE in case of such input.
We need new feature for text/image wrapping. Today is very difficult align images.
maybe I can fork this project and contribute to it.
someone already did it?
The problem is in the followed statement:
trigger_error('Template file '.$strFilename.' not found.', E_ERROR);
Due to http://php.net/manual/en/function.trigger-error.php:
"It only works with the E_USER family of constants, and will default to E_USER_NOTICE."
So, replacing E_ERROR with E_USER_ERROR helps.
Unstable version should not be the master branch but another branch named develop.
Duplicate PHPOffice/PHPExcel pages and adapt them for PHPWord
$this->_writeTextRun($objWriter, $element);
is not yet implemented, in both (basic) writers.
Unfortunately the HTML to docx Converter (http://htmltodocx.codeplex.com/) makes heavy use of textruns, so if you want to export userinput, made by a WYSIWYG Editor, to .rtf or .odt, there is currently no possibility for that.
So implementing at least this function would make the 2 writers become more usefull...
thx!
sahib
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.