自动下载使用PHPWord文件附件(Auto download the file attachmen

2019-06-26 06:39发布

我试图用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..

好心提醒。

Answer 1:

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


Answer 2:

$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;


Answer 3:

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


Answer 4:

现在丝毫版本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');
//#####################################################
//#####################################################
?>


Answer 5:

这是我的工作:

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

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

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


Answer 6:

很抱歉,这排在其后。 我偶然在此试图解决同样的问题。 我能得到它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);
    }

希望它可以帮助别人!



Answer 7:

根据@ 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);


文章来源: Auto download the file attachment using PHPWord
标签: php phpword