从 Laravel 导出 PDF
Export PDF from Laravel
我正在尝试以 PDF 格式导出 table,但我有一个 foreach,我想从 foreach 导出所有数据,但不适用于所有数据,仅适用于一行。
代码如下:
foreach ($posts as $post) {
$html = '<div class="table-scrollable">
<table id="posts" class="table table-bordered table-hover">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Title</th>
</tr>
</thead>
<tbody id="body"><tr>
<td>'
. $post->id . '
</td>
<td>' .
$post->name .
'</td>
<td>'
. $post->title .
'</td>
</tr>
</tbody>
</table>
</div>';
}
return PDF::load($html, 'A4', 'portrait')->download('my_pdf');
在 foreach 构造中,您覆盖了 $html
变量,您需要在 foreach
之前初始化 $html
并使用串联赋值 [=] 添加到字符串的末尾 html 14=]
$html = '';
foreach ($posts as $post) {
$html .= '<div class="table-scrollable">
<table id="posts" class="table table-bordered table-hover">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Title</th>
</tr>
</thead>
<tbody id="body"><tr>
<td>'
. $post->id . '
</td>
<td>' .
$post->name .
'</td>
<td>'
. $post->title .
'</td>
</tr>
</tbody>
</table>
</div>';
}
return PDF::load($html, 'A4', 'portrait')->download('my_pdf');
我正在尝试以 PDF 格式导出 table,但我有一个 foreach,我想从 foreach 导出所有数据,但不适用于所有数据,仅适用于一行。
代码如下:
foreach ($posts as $post) {
$html = '<div class="table-scrollable">
<table id="posts" class="table table-bordered table-hover">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Title</th>
</tr>
</thead>
<tbody id="body"><tr>
<td>'
. $post->id . '
</td>
<td>' .
$post->name .
'</td>
<td>'
. $post->title .
'</td>
</tr>
</tbody>
</table>
</div>';
}
return PDF::load($html, 'A4', 'portrait')->download('my_pdf');
在 foreach 构造中,您覆盖了 $html
变量,您需要在 foreach
之前初始化 $html
并使用串联赋值 [=] 添加到字符串的末尾 html 14=]
$html = '';
foreach ($posts as $post) {
$html .= '<div class="table-scrollable">
<table id="posts" class="table table-bordered table-hover">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Title</th>
</tr>
</thead>
<tbody id="body"><tr>
<td>'
. $post->id . '
</td>
<td>' .
$post->name .
'</td>
<td>'
. $post->title .
'</td>
</tr>
</tbody>
</table>
</div>';
}
return PDF::load($html, 'A4', 'portrait')->download('my_pdf');