使用PHPWord不会在文档中打印任何内容


Nothing gets printed in the doc using 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

您应该设置文件的路径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);