duanqin9507 2018-08-31 08:33
浏览 1167

在laravel中使用phpword将html页面转换为word文档

I try to convert HTML page to word docs and download the content, but it gives me a blank file after downloaded.

below here is my code:

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
    ]);
}

Below is for my Route:

Route::get('financialPreviewWord', 'FinancialController@downloadWord')->name('financialPreviewWord');

And below is the download button links

<a href="{{ route('financialPreviewWord', [ 'downloadWord' => 'word']) }}&id={{ $tasks->id }}">Download Word</a>

How to get the entire HTML page to be download as docs, Thanks in advance and sorry for my bad English.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog
    • ¥15 Excel发现不可读取的内容
    • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
    • ¥20 yolov5自定义Prune报错,如何解决?