使用PHPWord自动下载文件附件
我正在尝试使用PHPWord生成word文档。并且可以成功生成文档。但是有一个问题,我生成的word文档将保存在服务器上。我如何使它可以直接下载 样本:使用PHPWord自动下载文件附件,php,phpword,Php,Phpword,我正在尝试使用PHPWord生成word文档。并且可以成功生成文档。但是有一个问题,我生成的word文档将保存在服务器上。我如何使它可以直接下载 样本: $PHPWord = new PHPWord(); //Searching for values to replace $document = $PHPWord->loadTemplate('doc/Temp1.docx'); $document->setValue('Name', $Name); $document->set
$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..
请告知。是一个只写的流,它会写入您的屏幕(如echo
)
所以,$document->save('php://output');
不会将文件保存在服务器上的任何位置,它只会将其回显
$temp_file = tempnam(sys_get_temp_dir(), 'PHPWord');
$document->save($temp_file);
似乎,$document->save
,不支持流包装器,所以它创建了一个名为的文件php://output“
。尝试使用另一个文件名(我建议使用一个临时文件,因为您只想回显它)
在标题
中,文件名
字段是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
很抱歉,这个迟来了。我在试图解决同样的问题时偶然发现了这个问题。我能够通过以下方式使其在Laravel 5上工作:
$file_dir = $template_upload_dir.DIRECTORY_SEPARATOR.'filename.docx';
$tags = array();
if (file_exists($file_dir)) {
$templateProcessor = new TemplateProcessor($file_dir);
$tags = $templateProcessor->getVariables();
$replace = array('');
$templateProcessor->setValue($tags, $replace);
$save_file_name = $fullname.'-'.$inv_code.'-'.date('YmdHis').'.docx';
$templateProcessor->saveAs($save_file_name);
return response()->download($save_file_name)->deleteFileAfterSend(true);
}
希望它能帮助别人 现在为0.13.0版
// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
header("Content-Disposition: attachment; filename='myFile.docx'");
$objWriter->save("php://output");
这是我的工作:
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007', $download = true);
header("Content-Disposition: attachment; filename='File.docx'");
$objWriter->save("php://output");
基于@user3214824 answer的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);
嗨,它确实把它保存到我的文档目录里了。当我为标题附件命名文件名时。它可以下载,但里面的内容是空的。@JLearner:啊,好的。我知道发生了什么事
$document->save
不支持流包装器,因此它正在生成一个名为的文件php://output“
。将其更改为另一个文件名,然后使用echo file\u get\u contents('fileName.docx')
将其回显到浏览器。是否在标题部分回显文件内容?或者?@JLearner:file\u get\u contents
输出文件,所以它需要在头调用之后。我必须使用$document->saveAs($temp\u file);为了使它能够与docx模板一起工作,save()方法只生成了一个空文档。我尝试了您的代码,但它对每一个标题行都显示一个“标题已发送”-警告。之后,它会打印一些奇怪的符号,比如���% �T��E�tw�T��(�T��T�在浏览器中访问我的网络控制台。虽然我没有使用您的objWriter,但效果很好-但是使用模板和saveAs
它可以像预期的那样工作您好。祝贺您发表第一篇文章。您能提供一些额外的文本来说明您的答案是什么吗?下载文件附件作为docx:)此方法比save和readfile方法快得多。这是因为此解决方案没有通过存储I/O。
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007', $download = true);
header("Content-Disposition: attachment; filename='File.docx'");
$objWriter->save("php://output");
// ...
$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);