- This topic is empty.
-
AuthorPosts
-
July 28, 2014 at 9:11 am #176695
Harris Christiansen
ParticipantHi All,
Don’t know if any of you have any experience with PHPWord (actually a pretty cool library I stumbled upon), but worth a shot!
So either I’m missing something very obvious, or else I just have terrible luck. I am attempting to load a .docx file with PHPWord, set a value in it, and save the file. The issue is the saved file comes out as a blank word document, 7kb in size. The document I put in is large and has multiple pages. Obviously its not loading it right. My code is as follows:
$PHPWord = new \PhpOffice\PhpWord\PhpWord(); $document = $PHPWord->loadTemplate('Resources/documents/test.docx'); $document->setValue('theDate', '2014-07-25'); $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007'); $xmlWriter->save("php://output");
I have tried with multiple test.docx files of different sorts, but the output file is always blank.
July 28, 2014 at 9:38 am #176696__
ParticipantObviously its not loading it right.
Why is this obvious? Please note, I mean no offense here: you haven’t described what you tried to do in any detail, nor have you provided any links to code or documentation that might help point the way at how it should be done. Without detailed explanations, all we can do is offer wild guesses.
Wild guess: you’re creating a class instance and assigning it to
$PHPWord
, then you’re creating (what I assume is) a document and assigning it to$document
. You’re setting a value on$document
, but it would appear that you’re trying to save$PHPWord
.edit:
Is this the library you’re using? If so, it would seem that my wild guess above is probably not the cause of your problem. Have you asked your question on PhpWord’s discussion boards?
Another suggestion would be to make sure you have error reporting enabled.
July 28, 2014 at 9:44 am #176697Harris Christiansen
ParticipantThe library for phpWord can be found here: https://github.com/PHPOffice/PHPWord
An example for using phpWord Templates can be found here: http://phpword.readthedocs.org/en/latest/templates.html
To me it was obvious that it was not loading the template file because, following phpWords examples, if I add sections and text to the file, that section/text shows up in the output file. But nothing from the loaded template file shows up in the output file.
Error reporting is enabled to show all errors and warnings, but nothing is shown. While it is a bit annoying of syntax to me as well, you are actually suppose to save the $phpWord variable and not the created $document. Sections are added to the file in the same way, and saving them by referring to $phpWord does indeed work.
I have tended to stay away from the website you linked because it appears that the project has drifted from that website, as the version hosted there is very old compare to the current official version on github.
July 28, 2014 at 9:51 am #176698__
ParticipantTo me it was obvious that it was not loading the template file because, following phpWords examples, if I add sections and text to the file, that section/text shows up in the output file. But nothing from the loaded template file shows up in the output file.
Ah — the way I read your OP, the output file was “a blank word document, 7kb in size”, regardless.
Briefly looking over the source code, it would seem that the authors use exceptions for error handling. (Are you using
try
…catch
blocks?) Make sure you have enabled error reporting on your server and see if any error messages show up — if loading the template is failing, there are three or four points where you should get an exception thrown.July 28, 2014 at 9:56 am #176699Harris Christiansen
ParticipantI apologize for the continued confusion. The output file is a blank word document, 7kb in size, when all I do is load the template and try to set a value. Just through testing, such as adding additional sections and text onto the “template file” does anything (and only the added text) show up on the output file.
It is not failing to load the template, beyond that nothing shows up. Error reporting is fully enabled, and if I change the load file path to a nonexistent file, it throws the exception.
July 28, 2014 at 9:58 am #176700__
ParticipantI have tended to stay away from the website you linked because it appears that the project has drifted from that website, as the version hosted there is very old compare to the current official version on github.
…Which I noticed after following your link. The github repo seems to have an Issues page, though. You might start by looking through those reports to see if you can find a similar issue. (Note that these are bug reports, however; it is not an appropriate place to ask for help with a specific bit of code.*)
* although people seem to ask a lot of questions there, so maybe the authors don’t mind…
Error reporting is enabled to show all errors and warnings, but nothing is shown.
Okay.
While it is a bit annoying of syntax to me as well, you are actually suppose to save the $phpWord variable and not the created $document.
It has more to do with how the code actually works. The
PhpWord
instance represents the entire document, while documents, sections, etc. represent parts of the document (actually “nested” inside the PhpWord object). Knowing this, it makes sense that you would save the PhpWord object rather than one of the components.I’ll take some time to look over how this works, but I might not have time right away to actually download and test anything out. I’ll let you know if I find any clues.
July 28, 2014 at 10:16 am #176701Harris Christiansen
ParticipantAlright, thank you. For now so you can reproduce as much as possible I’ll put all up all code for what I have done and tell you what my file looks like.
I have tested the script with multiple .docx files, all of which I either download from online or type up something in Microsoft Word. I have tried things even as basic as Hello World. I am saving my files as I would save any Microsoft Word file, not in any special, template fashion, as I have not seen any indication that I should do so.
My code is:
require_once $_SERVER['DOCUMENT_ROOT'].'/Resources/plugins/PhpWord/Autoloader.php'; \PhpOffice\PhpWord\Autoloader::register(); $PHPWord = new \PhpOffice\PhpWord\PhpWord(); $document = $PHPWord->loadTemplate('Resources/documents/test.docx'); $document->setValue('theDate', '2014-07-25'); $file = 'testFile.docx'; header("Content-Description: File Transfer"); header('Content-Disposition: attachment; filename="' . $file . '"'); header("Content-Type: application/docx"); header('Content-Transfer-Encoding: binary'); header("Cache-Control: public"); header('Expires: 0'); $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007'); $xmlWriter->save("php://output");
July 28, 2014 at 12:26 pm #176709Harris Christiansen
ParticipantResolved: For some reason PHPWord does not like creating an output file for download from templates. I was able to save the document to the server ($document, not $PHPWord) and then read the document file to initiate the download.
$document->saveAs("Resources/documents/".$file);
July 28, 2014 at 12:29 pm #176710__
ParticipantCool. I had wondered about that, but I didn’t find any documentation on it and it seemed like it ought to work. Glad you solved it!
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.