Jump to content

Generated file saving


rusjoan
 Share

Recommended Posts

Hello guys!

Above all sorry for my english :)

Yesterday I have found an exellent PHPWord class ( http://phpword.codeplex.com/ )

I'm going to create the module that based on phpWord that allows to work with .doc files and use doc templates.

My programming skills let to do it with no problem but I want to code this class support maximally using existing PW abilities.

One my big problem (exactly - my non-acquaintance) is on saving generated DOC-file.

This PHP class saves document in any directory that I can choose using $document->save('/dir/Solarsystem.docx'); instruction.

I'm going to show a little example for you:

require_once '../PHPWord.php';

$PHPWord = new PHPWord();

$document = $PHPWord->loadTemplate('Template.docx');

$document->setValue('Value1', 'Sun');
$document->setValue('Value2', 'Mercury');
$document->setValue('Value3', 'Venus');

$document->setValue('weekday', date('l'));
$document->setValue('time', date('H:i'));

$document->save('Solarsystem.docx');

This code replaces some values in template document and saves it.

And one main question - how to save it right using PW? The best variant is to save it to DB like other files saving through admin interface, but more perfect is to give file to user by setting headers in response, with no fixing it in DB or file system

Hope for your help :)

Thanks!

PHPWord_0.6.2_Beta.zip

Link to comment
Share on other sites

Hi rusjoan,

Welcome to Pw!

What exactly should your module do? How do you plan to integrate the class into ProcessWire?

It's not clear from your description.

ProcessWire does save the files/images of a page in the folder /site/assets/files/{page-id}/.

So if you wanted to save a word file in ProcessWire, then it could be somewhere in the /site/assets/ directory.

If you have one word per page, you'd save it like this:

$document->save($config->paths->assets . "files/{$page->id}/myName.docx");


This probably didn't answer your question :)

  • Like 2
Link to comment
Share on other sites

When saving files to a page's path, Wanze's got the right trick there, but do it like this instead (which ensures the directory is created if it's not there already):

$document->save($page->filesManager->path . 'myName.docx'); 

You might also want to add it to an existing files field on your page? 

$page->files->add($page->filesManager->path . 'myName.docx');

The above assumes your files field is named 'files'. 

For temporary, non-web accessible files, I usually save to /site/assets/cache/

$document->save($config->paths->cache . 'myName.docx'); 

If you need to store multiple files, it's best to have your module create it's own directory under /site/assets/cache/. 

  • Like 2
Link to comment
Share on other sites

Thank you, guys!

Ryan, is it right to replace this:

@mkdir(wire('config')->paths->assets . "files/" . wire('page')->id);
$doc->save(wire('config')->paths->assets . "files/" . wire('page')->id . "/document.docx");

to:

$doc->save($page->filesManager->path . 'document.docx'); 

?

Link to comment
Share on other sites

Yes, that's better. Beyond creating the directory for you if it doesn't already exist, it's also good to note that the directory might not literally be the $page->id. If you've got the $config->pagefileSecure=true; option in your /site/config.php, then unpublished and non-guest-accessible pages will have file directories that start with a period, in order to prevent web access, i.e. ".123" rather than "123". Using the $page->filesManager->path or $page->[your file field]->path, rather than constructing it yourself, ensures that all of this continues to work as it should. 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...