I am beginner in Laravel. I use in my project Laravel 5.8.
I have this code for generate Word file:
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$month = $request->input('month');
if ($month == null) {
$now = Carbon::now();
$month = $now->month;
}
$events = $this->frontendGateway->getEventCalendarDownload($request, $month);
$logo = public_path('assets/images/logo3.jpg');
$view_content = View::make('psCMS.prints.events-view', ['events' => $events, 'logo' => $logo])->render();
$section = $phpWord->addSection();
$text = $section->addText('aaaaaaaa');
$text = $section->addText('bbbbbbbbbb');
$text = $section->addText('ccccccccccc');
$text = $section->addText($view_content);
//ob_clean();
$fileName = 'Event_calendar' . '-' . now()->toDateString() . '.doc';
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save($fileName);
header('Content-Type: application/octet-stream');
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-Disposition: attachment; filename=$fileName");
ob_clean();
readfile($fileName);
and this is my Blade file:
<div id="header" class="fontSize14">
<table width="100%">
<tr>
<td align="left" style="width: 20%;">
<img src="" class="logo" />
</td>
<td align="left" style="width: 80%;">
<span class="fontSize19"><b>my name</b></span><br />
street<br />
</td>
</tr>
</table>
</div>
<div id="content" class="fontSize11">
<b class="fontSize19">Kalendarz wydarzeń</b><br /><br />
<table width="100%">
<thead style="background-color: lightgray;">
<tr>
<th>#</th>
<th>Data</th>
<th>Godzina</th>
<th>Nazwa imprezy</th>
<th>Miejsce</th>
</tr>
</thead>
<tbody>
@foreach($events as $event)
@php
$hourFromX = explode(":", $event->hour_from);
$hourToX = explode(":", $event->hour_to);
$hourFrom = $hourFromX['0'].":".$hourFromX['1'];
$hourTo = $hourToX['0'].":".$hourToX['1'];
@endphp
<tr>
<th scope="row"></th>
<td></td>
<td align="left">-</td>
<td align="left"></td>
<td align="left">@if(isset($event->localization)) ,
@endif</td>
</tr>
@endforeach
</tbody>
</table>
</div>
When I run this code, I have error: Word file is corrupted.
When I comment this line:
$text = $section->addText($view_content);
Word file is okey. Generated file was open in Word without any problems.
How can I repair it?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire