How to output the docx file after using the template

Sep 3, 2010 at 3:47 AM

Hello when i out put the file ,It is a empty file,Some body help me?

see the code here :


require_once 'function/phpword/PHPWord.php';

$PHPWord = new PHPWord();

$document = $PHPWord->loadTemplate('c://test.docx');

$document->setValue('Value1', 'Sun');
$document->setValue('Value2', 'Mercury');
$document->setValue('Value3', 'Venus');
$document->setValue('Value4', 'Earth');
$document->setValue('Value5', 'Mars');
$document->setValue('Value6', 'Jupiter');
$document->setValue('Value7', 'Saturn');
$document->setValue('Value8', 'Uranus');
$document->setValue('Value9', 'Neptun');
$document->setValue('Value10', 'Pluto');
$document->setValue('Value11','goodluck');
$document->setValue('weekday', date('l'));
$document->setValue('time', date('H:i'));
//$aa=time()."docx";
//$document->save($aa);
 
header('Content-Type: application/vnd.ms-word');
header('Content-Disposition: attachment;filename="01simple.docx"');
header('Cache-Control: max-age=0');
 $writer=PHPWord_IOFactory::createWriter($PHPWord,"Word2007");
 $writer->save("php://output");

Sep 3, 2010 at 3:50 AM

I want to out put the chaged file, if I using $document->save($aa); the file content is correct.   .   hope some body can help me ,thanks

Oct 27, 2010 at 2:35 PM

  header('Content-Type: application/vnd.ms-word');
  header('Content-Disposition: attachment;filename="myfile.docx"');
  header('Cache-Control: max-age=0');

// output the file to the browser
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('php://output');
exit; //you must have the exit!

I found I must have the exit; line or it did not work. I hope this helps you.

Feb 13, 2011 at 3:16 PM

Hello jill... i try your suggest... but the file still empty...

Feb 21, 2011 at 9:26 AM

I'm doing it like this and it's working:

header('Content-Description: File Transfer');
header('Content-type: application/force-download');
header('Content-Disposition: attachment; filename='.basename($filename));
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($filename));
readfile($filename);

 

$filename must be the full path to the file.

Which should match the path provided as argument for $document->save($filename).

Apr 25, 2011 at 9:26 AM

Anyone found the solution?

Apr 25, 2011 at 9:51 AM

Sorry for the previous comment. I didn't read well the whole discussion. Thank you!

May 2, 2011 at 12:59 PM

Hi all,

I have the same problem, the file is saved right but when I try to output directly to the browser it is empty.

I observed that despite PHPExcel that open "report.xls" if you use "header('Content-Disposition: attachment;filename="report.xls");", PHPWord open "report-1.docx".

The workaround is to save the file with a temporary name like suggests ADMelo, but these involve to manage the deletion of older file.

Someone has a new suggestion?

Thnaks in advance.



        
    
May 2, 2011 at 1:36 PM

I just got a cron job running daily to remove the files that are no longer needed. Since I know that during the night my application is very rarely used it is programmed for that time, for me it's a fine solution to deal with those files.

Jul 13, 2011 at 7:06 AM

Just add unlink($filename);

 

header('Content-Description: File Transfer');
header('Content-type: application/force-download');
header('Content-Disposition: attachment; filename='.basename($filename));
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($filename));
readfile($filename);

unlink($filename);

 

It will delete the saved file.

Jan 24, 2012 at 4:05 PM
Edited Jan 24, 2012 at 4:08 PM

I've tried what's written here and none worked.

I've solved with the following code snippet:

$document->save($file);
    
    if(!$file) {     
        // File doesn't exist, output error     
        die('file not found'); 
    } 
    else {     
        header("Cache-Control: public");     
        header("Content-Description: File Transfer");     
        header("Content-Disposition: attachment; filename=$file");     
        header("Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");     
        header("Content-Transfer-Encoding: binary");         

        readfile($file); 
    }
    
    unlink($file);
    
    exit;
 


Where $file is the name of the file. No path, just the name.

Jul 14, 2013 at 9:57 AM
I was facing same issue - Please check your image type and test with new image.