15

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

親切なアドバイス。

4

7 に答える 7

18

php://output画面に書き込む書き込み専用ストリームです ( などecho)。

その$document->save('php://output');ため、サーバー上のどこにもファイルを保存せず、エコーアウトするだけです。

はストリームラッパー$document->saveをサポートしていないようです。そのため、文字通り"php://output". 別のファイル名を使用してみてください (エコーアウトしたいだけなので、一時ファイルをお勧めします)。

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

のフィールドはheaderfilenameファイルの名前を 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
于 2012-07-13T18:25:29.137 に答える
10
$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;
于 2013-05-22T09:42:39.023 に答える
5

これは私にとってはうまくいきます:

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

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

$objWriter->save("php://output");
于 2017-10-18T18:32:35.343 に答える
1

すみません、これは後で来ました。同じ問題を解決しようとしているときに、これに出くわしました。以下を使用して、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);
    }

それが誰かを助けることを願っています!!!

于 2017-08-11T03:29:45.710 に答える