|
| 1 | +# Laravel Report Generators (PDF & Excel) |
| 2 | + |
| 3 | +This package is inspired by the package of [Jimmy-JS](https://github.com/Jimmy-JS/laravel-report-generator). Thanks Jimmy-JS. |
| 4 | + |
| 5 | +Rapidly Generate Simple Pdf Report on Laravel (Using [barryvdh/laravel-dompdf](https://github.com/barryvdh/laravel-dompdf) or [barryvdh/laravel-snappy](https://github.com/barryvdh/laravel-snappy)) or Excel Report (using [Maatwebsite/Laravel-Excel](https://github.com/Maatwebsite/Laravel-Excel)) |
| 6 | + |
| 7 | +This package provides a simple pdf & excel report generators to speed up your workflow |
| 8 | + |
| 9 | +## Installation |
| 10 | +Add package to your composer: |
| 11 | + |
| 12 | + composer require samuelterra22/laravel-report-generator |
| 13 | + |
| 14 | +Then, add the ServiceProvider to the providers array in config/app.php |
| 15 | + |
| 16 | + SamuelTerra22\ReportGenerator\ServiceProvider::class, |
| 17 | + |
| 18 | +**Optionally**, you can add this to your aliases array in config/app.php |
| 19 | + |
| 20 | + 'PdfReport' => SamuelTerra22\ReportGenerator\Facades\PdfReportFacade::class, |
| 21 | + 'ExcelReport' => SamuelTerra22\ReportGenerator\Facades\ExcelReportFacade::class, |
| 22 | + 'CSVReport' => SamuelTerra22\ReportGenerator\Facades\CSVReportFacade::class, |
| 23 | + |
| 24 | +## Usage |
| 25 | +This package is make use of `chunk` method (Eloquent / Query Builder) so it can handle big data without memory exhausted. |
| 26 | + |
| 27 | +Also, You can use `PdfReport`, `ExcelReport` or `CSVReport` facade for shorter code that already registered as an alias. |
| 28 | + |
| 29 | +### Example Display PDF Code |
| 30 | +```php |
| 31 | +// PdfReport Aliases |
| 32 | +use PdfReport; |
| 33 | + |
| 34 | +public function displayReport(Request $request) { |
| 35 | + // Retrieve any filters |
| 36 | + $fromDate = $request->input('from_date'); |
| 37 | + $toDate = $request->input('to_date'); |
| 38 | + $sortBy = $request->input('sort_by'); |
| 39 | + |
| 40 | + // Report title |
| 41 | + $title = 'Registered User Report'; |
| 42 | + |
| 43 | + // For displaying filters description on header |
| 44 | + $meta = [ |
| 45 | + 'Registered on' => $fromDate . ' To ' . $toDate, |
| 46 | + 'Sort By' => $sortBy |
| 47 | + ]; |
| 48 | + |
| 49 | + // Do some querying.. |
| 50 | + $queryBuilder = User::select(['name', 'balance', 'registered_at']) |
| 51 | + ->whereBetween('registered_at', [$fromDate, $toDate]) |
| 52 | + ->orderBy($sortBy); |
| 53 | + |
| 54 | + // Set Column to be displayed |
| 55 | + $columns = [ |
| 56 | + 'Name' => 'name', |
| 57 | + 'Registered At', // if no column_name specified, this will automatically seach for snake_case of column name (will be registered_at) column from query result |
| 58 | + 'Total Balance' => 'balance', |
| 59 | + 'Status' => function($result) { // You can do if statement or any action do you want inside this closure |
| 60 | + return ($result->balance > 100000) ? 'Rich Man' : 'Normal Guy'; |
| 61 | + } |
| 62 | + ]; |
| 63 | + |
| 64 | + /* |
| 65 | + Generate Report with flexibility to manipulate column class even manipulate column value (using Carbon, etc). |
| 66 | + |
| 67 | + - of() : Init the title, meta (filters description to show), query, column (to be shown) |
| 68 | + - editColumn() : To Change column class or manipulate its data for displaying to report |
| 69 | + - editColumns(): Mass edit column |
| 70 | + - showTotal() : Used to sum all value on specified column on the last table (except using groupBy method). 'point' is a type for displaying total with a thousand separator |
| 71 | + - groupBy() : Show total of value on specific group. Used with showTotal() enabled. |
| 72 | + - limit() : Limit record to be showed |
| 73 | + - make() : Will producing DomPDF / SnappyPdf instance so you could do any other DomPDF / snappyPdf method such as stream() or download() |
| 74 | + */ |
| 75 | + return PdfReport::of($title, $meta, $queryBuilder, $columns) |
| 76 | + ->editColumn('Registered At', [ |
| 77 | + 'displayAs' => function($result) { |
| 78 | + return $result->registered_at->format('d M Y'); |
| 79 | + } |
| 80 | + ]) |
| 81 | + ->editColumn('Total Balance', [ |
| 82 | + 'displayAs' => function($result) { |
| 83 | + return thousandSeparator($result->balance); |
| 84 | + } |
| 85 | + ]) |
| 86 | + ->editColumns(['Total Balance', 'Status'], [ |
| 87 | + 'class' => 'right bold' |
| 88 | + ]) |
| 89 | + ->showTotal([ |
| 90 | + 'Total Balance' => 'point' // if you want to show dollar sign ($) then use 'Total Balance' => '$' |
| 91 | + ]) |
| 92 | + ->limit(20) |
| 93 | + ->stream(); // or download('filename here..') to download pdf |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +Note: For downloading to excel, just change `PdfReport` facade to `ExcelReport` facade no more modifications |
| 98 | + |
| 99 | +### Data Manipulation |
| 100 | +```php |
| 101 | +$columns = [ |
| 102 | + 'Name' => 'name', |
| 103 | + 'Registered At' => 'registered_at', |
| 104 | + 'Total Balance' => 'balance', |
| 105 | + 'Status' => function($result) { // You can do data manipulation, if statement or any action do you want inside this closure |
| 106 | + return ($result->balance > 100000) ? 'Rich Man' : 'Normal Guy'; |
| 107 | + } |
| 108 | +]; |
| 109 | +``` |
| 110 | +Will produce a same result with: |
| 111 | +```php |
| 112 | +$columns = [ |
| 113 | + 'Name' => function($result) { |
| 114 | + return $result->name; |
| 115 | + }, |
| 116 | + 'Registered At' => function($result) { |
| 117 | + return $result->registered_at; |
| 118 | + }, |
| 119 | + 'Total Balance' => function($result) { |
| 120 | + return $result->balance; |
| 121 | + }, |
| 122 | + 'Status' => function($result) { // You can do if statement or any action do you want inside this closure |
| 123 | + return ($result->balance > 100000) ? 'Rich Man' : 'Normal Guy'; |
| 124 | + } |
| 125 | +]; |
| 126 | +``` |
| 127 | +So you can do some **eager loading relation** like: |
| 128 | + |
| 129 | +```php |
| 130 | +$post = Post::with('comment')->where('active', 1); |
| 131 | + |
| 132 | +$columns = [ |
| 133 | + 'Post Title' => function($result) { |
| 134 | + return $result->title; |
| 135 | + }, |
| 136 | + 'Slug' => 'slug', |
| 137 | + 'Top Comment' => function($result) { |
| 138 | + return $result->comment->body; |
| 139 | + } |
| 140 | +]; |
| 141 | +``` |
| 142 | +### Output Report |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | +### Example Code With Group By |
| 147 | +Or, you can total all records by group using `groupBy` method |
| 148 | +```php |
| 149 | + ... |
| 150 | + // Do some querying.. |
| 151 | + $queryBuilder = User::select(['name', 'balance', 'registered_at']) |
| 152 | + ->whereBetween('registered_at', [$fromDate, $toDate]) |
| 153 | + ->orderBy('registered_at', 'ASC'); // You should sort groupBy column to use groupBy() Method |
| 154 | + |
| 155 | + // Set Column to be displayed |
| 156 | + $columns = [ |
| 157 | + 'Registered At' => 'registered_at', |
| 158 | + 'Name' => 'name', |
| 159 | + 'Total Balance' => 'balance', |
| 160 | + 'Status' => function($result) { // You can do if statement or any action do you want inside this closure |
| 161 | + return ($result->balance > 100000) ? 'Rich Man' : 'Normal Guy'; |
| 162 | + } |
| 163 | + ]; |
| 164 | + return PdfReport::of($title, $meta, $queryBuilder, $columns) |
| 165 | + ->editColumn('Registered At', [ |
| 166 | + 'displayAs' => function($result) { |
| 167 | + return $result->registered_at->format('d M Y'); |
| 168 | + } |
| 169 | + ]) |
| 170 | + ->editColumn('Total Balance', [ |
| 171 | + 'class' => 'right bold', |
| 172 | + 'displayAs' => function($result) { |
| 173 | + return thousandSeparator($result->balance); |
| 174 | + } |
| 175 | + ]) |
| 176 | + ->editColumn('Status', [ |
| 177 | + 'class' => 'right bold', |
| 178 | + ]) |
| 179 | + ->groupBy('Registered At') |
| 180 | + ->showTotal([ |
| 181 | + 'Total Balance' => 'point' |
| 182 | + ]) |
| 183 | + ->stream(); |
| 184 | +``` |
| 185 | + |
| 186 | +**PLEASE TAKE NOTE TO SORT GROUPBY COLUMN VIA QUERY FIRST TO USE THIS GROUP BY METHOD.** |
| 187 | + |
| 188 | +### Output Report With Group By Registered At |
| 189 | + |
| 190 | + |
| 191 | + |
| 192 | +## Other Method |
| 193 | + |
| 194 | +### 1. setPaper($paper = 'a4') |
| 195 | +**Supported Media Type**: PDF |
| 196 | + |
| 197 | +**Description**: Set Paper Size |
| 198 | + |
| 199 | +**Params**: |
| 200 | +* $paper (Default: 'a4') |
| 201 | + |
| 202 | +**Usage:** |
| 203 | +```php |
| 204 | +PdfReport::of($title, $meta, $queryBuilder, $columns) |
| 205 | + ->setPaper('a6') |
| 206 | + ->make(); |
| 207 | +``` |
| 208 | + |
| 209 | +### 2. setCss(Array $styles) |
| 210 | +**Supported Media Type**: PDF, Excel |
| 211 | + |
| 212 | +**Description**: Set a new custom styles with given selector and style to apply |
| 213 | + |
| 214 | +**Params**: |
| 215 | +* Array $styles (Key: $selector, Value: $style) |
| 216 | + |
| 217 | +**Usage:** |
| 218 | +```php |
| 219 | +ExcelReport::of($title, $meta, $queryBuilder, $columns) |
| 220 | + ->editColumn('Registered At', [ |
| 221 | + 'class' => 'right bolder italic-red' |
| 222 | + ]) |
| 223 | + ->setCss([ |
| 224 | + '.bolder' => 'font-weight: 800;', |
| 225 | + '.italic-red' => 'color: red;font-style: italic;' |
| 226 | + ]) |
| 227 | + ->make(); |
| 228 | +``` |
| 229 | + |
| 230 | +### 3. setOrientation($orientation = 'portrait') |
| 231 | +**Supported Media Type**: PDF |
| 232 | + |
| 233 | +**Description**: Set Orientation to Landscape or Portrait |
| 234 | + |
| 235 | +**Params**: |
| 236 | +* $orientation (Default: 'portrait') |
| 237 | + |
| 238 | +**Usage:** |
| 239 | +```php |
| 240 | +PdfReport::of($title, $meta, $queryBuilder, $columns) |
| 241 | + ->setOrientation('landscape') |
| 242 | + ->make(); |
| 243 | +``` |
| 244 | + |
| 245 | +### 4. withoutManipulation() |
| 246 | +**Supported Media Type**: PDF, Excel, CSV |
| 247 | + |
| 248 | +**Description**: Faster generating report, but all columns properties must be matched the selected column from SQL Queries |
| 249 | + |
| 250 | +**Usage:** |
| 251 | +```php |
| 252 | +$queryBuilder = Customer::select(['name', 'age'])->get(); |
| 253 | +$columns = ['Name', 'Age']; |
| 254 | +PdfReport::of($title, $meta, $queryBuilder, $columns) |
| 255 | + ->withoutManipulation() |
| 256 | + ->make(); |
| 257 | +``` |
| 258 | + |
| 259 | +### 5. showMeta($value = true) |
| 260 | +**Supported Media Type**: PDF, Excel, CSV |
| 261 | + |
| 262 | +**Description**: Show / hide meta attribute on report |
| 263 | + |
| 264 | +**Params**: |
| 265 | +* $value (Default: true) |
| 266 | + |
| 267 | +**Usage:** |
| 268 | +```php |
| 269 | +PdfReport::of($title, $meta, $queryBuilder, $columns) |
| 270 | + ->showMeta(false) // Hide meta |
| 271 | + ->make(); |
| 272 | +``` |
| 273 | + |
| 274 | +### 6. showHeader($value = true) |
| 275 | +**Supported Media Type**: PDF, Excel, CSV |
| 276 | + |
| 277 | +**Description**: Show / hide column header on report |
| 278 | + |
| 279 | +**Params**: |
| 280 | +* $value (Default: true) |
| 281 | + |
| 282 | +**Usage:** |
| 283 | +```php |
| 284 | +PdfReport::of($title, $meta, $queryBuilder, $columns) |
| 285 | + ->showHeader(false) // Hide column header |
| 286 | + ->make(); |
| 287 | +``` |
0 commit comments