Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Loading A .docx with PHPWord

  • This topic is empty.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #176695
    Harris Christiansen
    Participant

    Hi 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.

    #176696
    __
    Participant

    Obviously 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.

    #176697
    Harris Christiansen
    Participant

    The 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.

    #176698
    __
    Participant

    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.

    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 trycatch 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.

    #176699
    Harris Christiansen
    Participant

    I 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.

    #176700
    __
    Participant

    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.

    …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.

    #176701
    Harris Christiansen
    Participant

    Alright, 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");
    
    #176709
    Harris Christiansen
    Participant

    Resolved: 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);

    #176710
    __
    Participant

    Cool. 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!

Viewing 9 posts - 1 through 9 (of 9 total)
  • The forum ‘Back End’ is closed to new topics and replies.