convert html page to word docs using phpword in laravel

Viewed 1573

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 Answers
Related