I am beginner in Laravel and I use Laravel 5.8 in my project.
I have function to generate PDF file:
public function getCalendar(Request $request)
{
$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');
$pdf = \PDF::loadView('prints.events-view', ['events' => $events, 'logo' => $logo]);
$pdf->setOptions(['isPhpEnabled' => true, 'dpi' => 150, 'setIsRemoteEnabled' => true]);
$pdf->setPaper('A4', 'letter');
return $pdf->download('Event Calendar' . '-' . now()->toDateString() . '.pdf');
}
My Blade:
<div id="header" class="fontSize14">
<table width="100%">
<tr>
<td align="left" style="width: 20%;">
<img src="{{ $logo }}" 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">Calendar</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">{{ $loop->iteration }}</th>
<td>{{ $event->date_from }}</td>
<td align="left">{{ $hourFrom }}-{{ $hourTo }}</td>
<td align="left">{{ $event->title }}</td>
<td align="left">@if(isset($event->localization)) {{ $event->localization->name }},
{{ $event->localization->city }}
{{ $event->localization->street }} @endif</td>
</tr>
@endforeach
</tbody>
</table>
</div><div id="header" class="fontSize14">
<table width="100%">
<tr>
<td align="left" style="width: 20%;">
<img src="{{ $logo }}" class="logo" />
</td>
<td align="left" style="width: 80%;">
<span class="fontSize19"><b>My name</b></span><br />
street name
</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">{{ $loop->iteration }}</th>
<td>{{ $event->date_from }}</td>
<td align="left">{{ $hourFrom }}-{{ $hourTo }}</td>
<td align="left">{{ $event->title }}</td>
<td align="left">@if(isset($event->localization)) {{ $event->localization->name }},
{{ $event->localization->city }}
{{ $event->localization->street }} @endif</td>
</tr>
@endforeach
</tbody>
</table>
</div>
It's work fine.
Now I want make generate Word file. I found this lib: https://github.com/PHPOffice/PHPWord
I try change my function to this lib (export to Word file):
use PhpOffice\PhpWord\PhpWord; ....
public function getCalendar(Request $request)
{
$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');
...
}
How can I add my data and view to Word?
for the view you have you need to render and assign to a variable:
$view_content = View::make('prints.events-view', ['events' => $events, 'logo' => $logo])->render();
then create a word document by this library and append to content to
// Creating the new document...
$phpWord = new \PhpOffice\PhpWord\PhpWord();
/* Note: any element you append to a document must reside inside of a Section. */
// Adding an empty Section to the document...
$section = $phpWord->addSection();
// Adding Text element to the Section having font styled by default...
$section->addText($view_content);
// Saving the document as HTML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$file_name = 'event_calendar_' . '-' . now()->toDateString() . '.doc';
$objWriter->save(public_path($file_name));
and then the URL of the file will be url($file_name)