在 Laravel 5.8 中用 PHPWord 生成 Word 文件
Generate Word file in PHPWord in Laravel 5.8
我是 Laravel 的初学者,我在我的项目中使用 Laravel 5.8。
我有生成PDF文件的功能:
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');
}
我的刀锋:
<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>
工作正常。
现在我要生成Word文件。
我找到了这个库:https://github.com/PHPOffice/PHPWord
我尝试将我的函数更改为这个库(导出到 Word 文件):
使用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');
...
}
如何将我的数据和视图添加到 Word?
对于您拥有的视图,您需要渲染并分配给一个变量:
$view_content = View::make('prints.events-view', ['events' => $events, 'logo' => $logo])->render();
然后通过这个库创建一个 word 文档并将内容附加到
// 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));
然后文件的 URL 将是 url($file_name)
我是 Laravel 的初学者,我在我的项目中使用 Laravel 5.8。
我有生成PDF文件的功能:
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');
}
我的刀锋:
<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>
工作正常。
现在我要生成Word文件。 我找到了这个库:https://github.com/PHPOffice/PHPWord
我尝试将我的函数更改为这个库(导出到 Word 文件):
使用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');
...
}
如何将我的数据和视图添加到 Word?
对于您拥有的视图,您需要渲染并分配给一个变量:
$view_content = View::make('prints.events-view', ['events' => $events, 'logo' => $logo])->render();
然后通过这个库创建一个 word 文档并将内容附加到
// 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));
然后文件的 URL 将是 url($file_name)