我尝试将HTML页面转换为word文档并下载内容,但是下载后却给了我空白文件。
下面是我的代码:
public function downloadWord(Request $request) {
$tasks = Task::findOrFail($request->id);
$schedule = Schedule::all();
view()->share('tasks', $tasks);
view()->share('schedule', $schedule);
if($request->has('downloadWord')) {
$word = new \PhpOffice\PhpWord\PhpWord();
$newSection = $word->addSection();
$html = view('pdf.pdf', ['pdf' => false, 'tasks' => $tasks, 'schedule' => $schedule])->render();
$dom = new \DOMDocument();
@$dom->loadHTML($html);
\PhpOffice\PhpWord\Shared\Html::addHtml($html, false, false);
$objectWriter = \PhpOffice\PhpWord\IOFactory::createWriter($word, 'Word2007');
try {
$objectWriter->save(storage_path('Financial.docx'));
} catch (Exception $e) {
}
return response()->download(storage_path('Financial.docx'));
}
return view('pdf.pdf', [
'pdf' => false
]);
}
以下是我的路线:
Route::get('financialPreviewWord', 'FinancialController@downloadWord')->name('financialPreviewWord');
下面是下载按钮links
<a href="{{ route('financialPreviewWord', [ 'downloadWord' => 'word']) }}&id={{ $tasks->id }}">Download Word</a>
如何以文档形式下载整个HTML页面,在此先感谢您,我的英语不好。