vdespa / PHPWord-Joomla-Library

PHPWord library for Joomla! 2.5. & 3.0
2 stars 4 forks source link

Update to newer version possible? #2

Open schnuti opened 8 years ago

schnuti commented 8 years ago

Do you know if it's possible to update the library to a later PHPWord version with reasonable amount of time investment? I have NO interest in using Composer even if it would be possible with Joomla. (I think it_s hard) I_m not a professional developer or a wanna be and have no plans to hack around on that level.

I think there are limits when it comes to the autolader and I'm not sure if namespaces can be used and mixed with Joomla legacy. I'm also not sure if namespaces and file structure has a correlation.

I beliieve what I need is included in a former version of PHPWord not yet using PSR and other things so I could perhaps try to update to that version.

I have tested this version of the library with Joomla 3.5 and have come so far, that I create a document and on the fly download it without saving it to disk.

I have 7 days with 3 (might become 5) columns of lists. I want to have them in 3 (5) text columns. I could try to use 7 tables but mostly it's much harder to edit tables then plain text and thats the purpose of creating those documents - directly edit with Libre Office (Open Office) or MS Office.

I have a technically interesting part left - transform HTML text areas from the db to insert them in the PHPWord object. ;)

schnuti commented 8 years ago

I found out how to use the latest version.

Copy the folder PhpWord from the downloaded zip file (found in the src folder) to joomlas libraries folder + PhpOffice i.e. libararies/PhpOffice/PhpWord

Start e.g. a controller method with

    require_once JPATH_LIBRARIES . '/PhpOffice/PhpWord/Autoloader.php';
    \PhpOffice\PhpWord\Autoloader::register();

    // New Word Document
    $phpWord = new \PhpOffice\PhpWord\PhpWord();

So far everything works except one problem. I had to fix a couple of calls to a String class as I'm on PHP 7.

PHPExcel would probably work the same way. I have no idea, if it's interesting to the Joomla community to have an installable library package for those two.

vdespa commented 8 years ago

Hi @schnuti and thanks for looking into this.

Since I have not released any updates since 2013, it might be worth investing some time into this. I will let you know.

PHPWord for Joomla seems not so popular, but PHPExcel has a decent number of downloads so far - enough to keep me motivated.

If you find any problems with PHP7, it might be worth reporting / sending a pull request to the PhpOffice guys.

Keep you updated.

schnuti commented 8 years ago

Hi @vdespa,

They have solved the PHP7 issue in the development branch. You probably do not want to use that version loose code for a library. So:

  1. Wait for the next version ? or
  2. Fix the issue in the current version. 0.12.1 ( Search/replace "String" takes 5 min. - avoiding other textstrings including the word "String")

I now use the class simple_html_dom to filter out the strings from ul - li lists stored in text fields in the db. Those filtered values are exported and directly downloaded to a .docx file. The user now can edit those "to do lists / shopping lists". Made a lot of fun to code this :)

schnuti commented 8 years ago

I just saw that the code in "PhpOffice/Common" is needed for the next version. i.e. 3 parts in the library

libraries/PhpOffice/Common libraries/PhpOffice/PhpPresentation libraries/PhpOffice/\PhpSpreadsheet libraries/PhpOffice/\PhpWord

vdespa commented 8 years ago

Hi @schnuti

I have just did some testing using the latest (master) version of PHPWord. You can play with the pre-release: https://github.com/vdespa/PHPWord-Joomla-Library/releases/tag/0.13.0-alpha

Just download and install phpword_dist.zip

schnuti commented 8 years ago

I had a look in the zip file. To my understanding are you testing with the wrong code. You have to use the develop branch as the "String" class was removed there AND the "libraries/PhpOffice/Common" path is used for "common" classes. i.e. the package must include that path.

The size is now already 3M ! so I had to increase the sizes allowed in php.ini

schnuti commented 8 years ago

The Zend/Validator package is used.

Edit: I added a level I now have

libraries/phpoffice/PhpOffice/Common libraries/phpoffice/PhpOffice/PhpWord libraries/phpoffice/Zend/Validator

with a modified Autoloader.php in each folder. Only needed files included.

Do not ask me why it works (without conflicts) but it seems to.

require_once JPATH_LIBRARIES . '/phpoffice/PhpOffice/PhpWord/Autoloader.php';

\PhpOffice\PhpWord\Autoloader::register();

require_once JPATH_LIBRARIES . '/phpoffice/PhpOffice/Common/Autoloader.php';

\PhpOffice\Common\Autoloader::register();

require_once JPATH_LIBRARIES . '/phpoffice/Zend/Validator/Autoloader.php';

\Zend\Validator\Autoloader::register();
schnuti commented 8 years ago

phpoffice_word_excel.zip

This is an installation file. PhpExcel 1.8 is included but not at all tested. I guess that the autoloaders can be handled in another way.

vdespa commented 8 years ago

I have added another pre-release based on the develop branch. Download and install phpword_dist_develop.zip.

It should work only with:

jimport('phpword.library.autoload');

Unfortunately in the first run I cannot do anything about the size. I will try to remove some of the files after the thing is working.

Let me know.

schnuti commented 8 years ago

I have now included 3 PhpOffice "parts" in one installation zip. PhpExcel is a very special case, the path is PhpSpreadsheet but the namespace is PhpEdit!?

I've tested simple examples with all 3. "Powerpont", "Excel" and "Word". Not quite the Joomla way: I have for each example all the code in an "old" controller. It happend to be "extends JControllerForm" as i need that for my real issue.

libraries/phpoffice/PhpOffice/Common libraries/phpoffice/PhpOffice/PhpPresentation libraries/phpoffice/PhpOffice/\PhpSpreadsheet libraries/phpoffice/PhpOffice/\PhpWord libraries/phpoffice/Zend/Validator

The size is around 8MB unpacked, 3MB packed.

I'm not sure that this is the right way, but I guess they all have to be in the same folder and a deinstallation would remove everything, if it's separated into 3 different installation files.

FWIW: I do not really want to hack around with composer files when I want to add something to Joomla. That's for the ones that want to build there own code base.

Let me know if you want the zip file and I'll send it or upload it.

PS. I can't realy find a use for Powerpont creation with PHP but it makes fun to test it. DS.

teoyh commented 4 years ago

@schnuti schnuti I am trying to use phpword in Joomla! 3.9.13 i am using Php 7.3.11 Can you give me some clue how can i go about getting it setup. I tried downloading from here https://github.com/PHPOffice/PHPWord/releases and download the source code (zip) thereafter i unzip into libraries\PhpOffice\PhpWord but did not seem to be able to get it to work. I do not seem to be able to find those folder you mentioned like common, validator and the file autoloader.php Thank you in advance :)

schnuti commented 4 years ago

@teoyh It was a long time since I touched that code. It still works in my Joomla 4.0 that is updated to the latest alpha releases. As described above you also need: https://github.com/PHPOffice/Common and probably Zend/Validator

Initialize in a controller

    require_once JPATH_LIBRARIES . '/phpoffice/PhpOffice/PhpWord/Autoloader.php';
    \PhpOffice\PhpWord\Autoloader::register();
    require_once JPATH_LIBRARIES . '/phpoffice/PhpOffice/Common/Autoloader.php';
    \PhpOffice\Common\Autoloader::register();
    require_once JPATH_LIBRARIES . '/phpoffice/Zend/Validator/Autoloader.php';
    \Zend\Validator\Autoloader::register(); 

I do not remember, if I had to edit the Autoloaders. Probably I added one to the Zend path.

Hope it helps.

teoyh commented 4 years ago

I manage to get it working here is what i do for those who wish to get it to work in Joomla;

Download from https://github.com/PHPOffice/PHPWord/releases Unzip download zip file PHPWord-0.17.0.zip to a folder In Joomla libraries create a folder call PhpOffice Go back to the unzip folder and inside src folder , copy the entire PhpWord to PhpOffice Go to https://github.com/PHPOffice/Common and download the Common-develop.zip and unzip to a folder In the unzip folder of Common-develop look for the src folder, inside copy the entire Common folder to PhpOffice You will also need an autoloader.php inside the PhpWord which is not available from the zip (Attach here for you) Autoloader.zip

Once the above is complete to create a document ; I use the following code ; <?php defined('_JEXEC') or die(); require_once 'libraries/PhpOffice/PhpWord/Autoloader.php'; \PhpOffice\PhpWord\Autoloader::register(); require_once 'libraries/PhpOffice/Common/Autoloader.php'; \PhpOffice\Common\Autoloader::register(); $phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->createSection(); $section->addText('Hello World'); $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); $objWriter->save('documents\test.docx'); ?> When i execute it i can see a the test.docx created in the folder documents