使用PHPWord自动下载文件附件

时间:2012-07-13 18:15:36

标签: php phpword

我正在尝试使用PHPWord生成word文档。并且可以成功生成文档。但是有一个问题是我生成的word文档将保存在服务器上。我如何才能立即下载?

样品:

$PHPWord = new PHPWord();
//Searching for values to replace
$document = $PHPWord->loadTemplate('doc/Temp1.docx');
$document->setValue('Name', $Name);
$document->setValue('No', $No);
$document->save('php://output'); //it auto save into my 'doc' directory.

如何链接到标题以便按如下方式下载:

header("Content-Disposition: attachment; filename='php://output'"); //not sure how to link this filename to the php://output..

请告知。

6 个答案:

答案 0 :(得分:12)

php://output是一个只写流,可写入您的屏幕(如echo)。

因此,$document->save('php://output');不会将文件保存在服务器上的任何位置,只会将其回显。

似乎$document->save不支持流包装器,因此它实际上创建了一个名为"php://output"的文件。尝试使用另一个文件名(我建议使用临时文件,因为你只想回应它)。

$temp_file = tempnam(sys_get_temp_dir(), 'PHPWord');
$document->save($temp_file);

header中,filename字段是PHP告诉浏览器文件命名的字段,它不必是服务器上文件的名称。它只是浏览器将其保存为的名称。

header("Content-Disposition: attachment; filename='myFile.docx'");

所以,把它们放在一起:

$PHPWord = new PHPWord();
//Searching for values to replace
$document = $PHPWord->loadTemplate('doc/Temp1.docx');
$document->setValue('Name', $Name);
$document->setValue('No', $No);
// // save as a random file in temp file
$temp_file = tempnam(sys_get_temp_dir(), 'PHPWord');
$document->save($temp_file);

// Your browser will name the file "myFile.docx"
// regardless of what it's named on the server 
header("Content-Disposition: attachment; filename='myFile.docx'");
readfile($temp_file); // or echo file_get_contents($temp_file);
unlink($temp_file);  // remove temp file

答案 1 :(得分:8)

$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');

$filename = 'MyFile.docx';

$objWriter->save($filename);

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
flush();
readfile($filename);
unlink($filename); // deletes the temporary file
exit;

答案 2 :(得分:1)

// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
header("Content-Disposition: attachment; filename='myFile.docx'");
$objWriter->save("php://output");

答案 3 :(得分:1)

现在为Ver Ver 0.13.0 https://github.com/PHPOffice/PHPWord

<?
require_once "../include/PHPWord-develop/bootstrap.php";

$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('template.docx');

$templateProcessor->setValue('var01', 'Sun');
$templateProcessor->setValue('var02', 'Mercury');

//#####################################################
// Save File
//#####################################################
//#####################################################
header("Content-Disposition: attachment; filename='output01.docx'");
  $templateProcessor->saveAs('php://output');
//#####################################################
//#####################################################
?>

答案 4 :(得分:1)

这对我有用:

$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007', $download = true);

header("Content-Disposition: attachment; filename='File.docx'");

$objWriter->save("php://output");

答案 5 :(得分:0)

基于@ user3214824答案的Laravel简单解决方案。

// ...    
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007');
$doc_name = 'fileName.docx';
$objWriter->save($doc_name); // saving in the public path just for testing

return response()->download(public_path($doc_name))->deleteFileAfterSend(true);