使用PHPWord在doc中没有打印任何内容

时间:2014-11-06 06:33:17

标签: php phpword

我正在使用PHPWord在php中下载docx文件。但是,如果我尝试下载它,则不会在文件中打印任何内容。但是内容会显示在保存在服务器上的文件中。以下是我使用过的代码。谁能告诉我这是什么问题。

<?php
include "../includes/config.inc.php";
include '../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();

$section->addText('CANDIDATES DETAILS');

$filename='test';

$file=$filename.'.docx';
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('/form_doc/'.$filename.'.docx');


//download the file
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment; filename='.$file);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);

提前致谢。

此致

NEHA

1 个答案:

答案 0 :(得分:1)

您应该设置路径以归档readfile()filesize()个功能:

$file = $filename.'.docx';
$filepath = '/form_doc/' . $file;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save($filepath);

//download the file
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment; filename='.$file);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
readfile($filepath);