2012-07-13 281 views
6

我正在嘗試使用PHPWord生成word文檔。並且文檔可以成功生成。但是,我的生成的Word文檔將保存在服務器上,這是一個問題。我怎樣才能讓它立即下載?使用PHPWord自動下載文件附件

樣品:

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

請告知。

回答

8

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 
+0

嗨它沒有真正把它保存到我的文檔目錄。當我爲頭附件命名文件名時。它設法下載,但內容是空的。 – JLearner 2012-07-13 18:28:19

+0

@JLearner:啊,好的。我明白髮生了什麼事。 '$ document-> save'不支持流封裝,所以它創建一個名爲'「php:// output」'的文件。將其更改爲另一個文件名,然後使用'echo file_get_contents('fileName.docx')'將其回顯給瀏覽器。 – 2012-07-13 18:30:27

+0

在頭部回顯file_ge_contents?要麼? – JLearner 2012-07-13 18:35:54

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; 
+0

我試過你的代碼,但是它爲每個標題行說明了一個「標題已經發送」 - 警告。之後,它會在瀏覽器中向我的網絡控制檯打印' % t E tw t ( t t Ew')等怪異符號。 – moody 2017-01-05 14:16:27

0

很抱歉,這排在其後。我試圖解決同樣的問題時偶然發現了這個問題。我能夠得到它使用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); 
    } 

希望它可以幫助別人!

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

您好。祝賀您的第一篇文章。你能否給出一些額外的文字說明你的答案在做什麼? – 2017-08-19 23:45:19

+0

將文件附件下載爲docx :) – burrotauro 2017-08-21 17:17:40

1

現在絲毫版本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'); 
//##################################################### 
//##################################################### 
?> 
1

這是我的工作:

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

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

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