2012-07-13 15 views
6

का उपयोग कर फ़ाइल अनुलग्नक डाउनलोड करें मैं शब्द दस्तावेज़ उत्पन्न करने के लिए PHPWord का उपयोग करने का प्रयास कर रहा हूं। और दस्तावेज़ सफलतापूर्वक उत्पन्न किया जा सकता है। लेकिन एक समस्या है जहां मेरा जेनरेट किया गया शब्द दस्तावेज़ सर्वर पर सहेजा जाएगा। मैं इसे सीधे डाउनलोड करने के लिए कैसे उपलब्ध कर सकता हूं?स्वतः 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 क्षेत्र क्या पीएचपी ब्राउज़र फ़ाइल का नाम है बताता है, यह सर्वर पर किसी फ़ाइल के नाम होने की जरूरत नहीं है। यह सिर्फ वह नाम है जिसे ब्राउज़र इसे सहेज लेगा।

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

+0

@JLearner: आह, ठीक है। मैं देख रहा हूं कि क्या हो रहा है। '$ दस्तावेज़-> सेव 'स्ट्रीम रैपर का समर्थन नहीं करता है, इसलिए यह फ़ाइल को शाब्दिक रूप से' 'php: // output' नाम दिया गया है। इसे किसी अन्य फ़ाइल नाम में बदलें, फिर ब्राउज़र पर गूंजने के लिए 'echo file_get_contents (' fileName.docx ')' का उपयोग करें। –

+0

हेडर भाग पर file_ge_contents गूंजें? या? – JLearner

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

0

क्षमा करें यह बाद में आया था। मैं एक ही समस्या को हल करने की कोशिश करते समय इस पर ठोकर खाई। मैं इसे नीचे प्रयोग कर लार्वेल 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

हैलो। आपकी पहली पोस्ट पर बधाई। क्या आप कुछ अतिरिक्त टेक्स्ट स्पष्टीकरण दे सकते हैं कि आपका उत्तर क्या कर रहा है? –

+0

फ़ाइल अटैचमेंट को डॉक्क्स के रूप में डाउनलोड करें :) – burrotauro

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"); 
संबंधित मुद्दे