user3319461
user3319461

Reputation:

PHPWord generating a blank page

Here is my code:

$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
$section->addText('Goodby WOrld!');

header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=byeWorld.docx");
header("Content-Type: application/docx");
header("Content-Transfer-Encoding: binary");

$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('byeWorld.docx');

It is downloading the file (byeWorld.docx) but the file is blank.

What is wrong here?

Upvotes: 1

Views: 2885

Answers (3)

Hsç
Hsç

Reputation: 1

After saving the Word file, you can give the full path of the file and download it. It is currently being saved as a word file.I am using in laravel like this:

$fileName= "word.docx";
$fileLink= base_path(). $file;
 
$headers = array(
            'Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        );

return Response::download($fileLink,$fileName, $headers);

Upvotes: 0

Mark Baker
Mark Baker

Reputation: 212522

You're saving to a file on the server ('byeWorld.docx'), not sending to the browser so there's no need to set headers.

If you want to send to the browser, then you should save to 'php://output', and then you need the headers

Upvotes: 1

Andrew
Andrew

Reputation: 20111

Looks like a problem with your headers. Based on the examples I've read, you don't need them. Or, another possibility, don't declare them in the middle of creating the document. Try at the top? And make sure you are requireing the PHPWord.php at the top.

from http://phpword.codeplex.com/documentation:

// Include the PHPWord.php, all other classes were loaded by an autoloader
require_once 'PHPWord.php';

// Create a new PHPWord Object
$PHPWord = new PHPWord();

// Every element you want to append to the word document is placed in a section. So you need a section:
$section = $PHPWord->createSection();

// After creating a section, you can append elements:
$section->addText('Hello world!');

// You can directly style your text by giving the addText function an array:
$section->addText('Hello world! I am formatted.', array('name'=>'Tahoma', 'size'=>16, 'bold'=>true));

// If you often need the same style again you can create a user defined style to the word document
// and give the addText function the name of the style:
$PHPWord->addFontStyle('myOwnStyle', array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232'));
$section->addText('Hello world! I am formatted by a user defined style', 'myOwnStyle');

// You can also putthe appended element to local object an call functions like this:
$myTextElement = $section->addText('Hello World!');
$myTextElement->setBold();
$myTextElement->setName('Verdana');
$myTextElement->setSize(22);

// At least write the document to webspace:
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('helloWorld.docx');

Upvotes: 0

Related Questions