Skip to content

Commit 4f46ef9

Browse files
committed
PDF Support
- Add pdf support with either dompdf or mpdf libraries - Added choice in config file - Added option for exportOnly on a column
1 parent d6e3445 commit 4f46ef9

File tree

7 files changed

+62
-12
lines changed

7 files changed

+62
-12
lines changed

config/config.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
*/
88
'exports' => \Rappasoft\LaravelLivewireTables\Exports\Export::class,
99

10+
/*
11+
* Which library you want to use for PDF generation
12+
* Supports dompdf, mpdf
13+
* You must install the appropriate third party package for each
14+
* See: https://docs.laravel-excel.com/3.1/exports/export-formats.html
15+
* And: https://phpspreadsheet.readthedocs.io/en/latest/topics/reading-and-writing-to-file/#pdf
16+
*/
17+
'pdf_library' => 'dompdf',
18+
1019
/*
1120
* The frontend styling framework to use
1221
* Options: bootstrap-4

resources/views/bootstrap-4/includes/export.blade.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
@if (in_array('xlsx', $exports, true))
1717
<a class="dropdown-item" href="#" wire:click.prevent="export('xlsx')">XLSX</a>
1818
@endif
19+
20+
@if (in_array('pdf', $exports, true))
21+
<a class="dropdown-item" href="#" wire:click.prevent="export('pdf')">PDF</a>
22+
@endif
1923
</div>
2024
</div><!--export-dropdown-->
2125
@endif

src/Exports/Export.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function map($row): array
6464
$map = [];
6565

6666
foreach ($this->columns as $column) {
67-
if ($column->isVisible() && $column->includedInExport()) {
67+
if ($column->isExportOnly() || ($column->isVisible() && $column->includedInExport())) {
6868
if ($column->isFormatted()) {
6969
if ($column->hasExportFormat()) {
7070
$map[] = $column->formattedForExport($row, $column);

src/Traits/ExportHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function getHeadingRow(): array
1515
$headers = [];
1616

1717
foreach ($this->columns as $column) {
18-
if ($column->isVisible() && $column->includedInExport()) {
18+
if ($column->isExportOnly() || ($column->isVisible() && $column->includedInExport())) {
1919
$headers[] = $column->getText();
2020
}
2121
}

src/Traits/Exports.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function export($type)
3131
{
3232
$type = strtolower($type);
3333

34-
if (! in_array($type, ['csv', 'xls', 'xlsx'], true)) {
34+
if (! in_array($type, ['csv', 'xls', 'xlsx', 'pdf'], true)) {
3535
throw new UnsupportedExportFormatException(__('This export type is not supported.'));
3636
}
3737

@@ -42,15 +42,28 @@ public function export($type)
4242
switch ($type) {
4343
case 'csv':default:
4444
$writer = Excel::CSV;
45-
break;
45+
break;
4646

4747
case 'xls':
4848
$writer = Excel::XLS;
49-
break;
49+
break;
5050

5151
case 'xlsx':
5252
$writer = Excel::XLSX;
53-
break;
53+
break;
54+
55+
case 'pdf':
56+
$writer = Excel::DOMPDF;
57+
$library = strtolower(config('laravel-livewire-tables.pdf_library'));
58+
59+
if (! in_array($library, ['dompdf', 'mpdf'], true)) {
60+
throw new UnsupportedExportFormatException(__('This PDF export library is not supported.'));
61+
}
62+
63+
if ($library === 'mpdf') {
64+
$writer = Excel::MPDF;
65+
}
66+
break;
5467
}
5568

5669
$class = config('laravel-livewire-tables.exports');

src/Traits/Options.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ trait Options
2121
'bootstrap' => [
2222
'classes' => [
2323
'buttons' => [
24-
'export' => 'btn btn-info',
24+
'export' => 'btn',
2525
],
2626
'table' => 'table table-bordered table-striped',
2727
'thead' => null,

src/Views/Column.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ class Column
3737
*/
3838
protected $raw = false;
3939

40+
/**
41+
* @var bool
42+
*/
43+
protected $includeInExport = true;
44+
45+
/**
46+
* @var bool
47+
*/
48+
protected $exportOnly = false;
49+
4050
/**
4151
* @var
4252
*/
@@ -57,11 +67,6 @@ class Column
5767
*/
5868
protected $searchCallback;
5969

60-
/**
61-
* @var bool
62-
*/
63-
protected $includeInExport = true;
64-
6570
/**
6671
* Column constructor.
6772
*
@@ -247,6 +252,25 @@ public function includedInExport(): bool
247252
return $this->includeInExport === true;
248253
}
249254

255+
/**
256+
* @return $this
257+
*/
258+
public function exportOnly(): self
259+
{
260+
$this->hidden = true;
261+
$this->exportOnly = true;
262+
263+
return $this;
264+
}
265+
266+
/**
267+
* @return bool
268+
*/
269+
public function isExportOnly(): bool
270+
{
271+
return $this->exportOnly === true;
272+
}
273+
250274
/**
251275
* @return $this
252276
*/

0 commit comments

Comments
 (0)