Skip to content

Commit 5e77446

Browse files
committed
Initial commit
0 parents  commit 5e77446

20 files changed

+1866
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/vendor
2+
composer.phar
3+
composer.lock
4+
.DS_Store
5+
.idea/

.travis.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
language: php
2+
3+
php:
4+
- 5.5
5+
- 5.6
6+
- 7.0
7+
- hhvm
8+
9+
before_script:
10+
- travis_retry composer self-update
11+
- travis_retry composer install --prefer-source --no-interaction --dev
12+
13+
script: phpunit

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Jimmy Setiawan <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "samuelterra22/laravel-report-generator",
3+
"description": "Rapidly Generate Simple Pdf & Excel Report on Laravel 5 (Using Barryvdh/DomPdf or Barryvdh/laravel-snappy & maatwebsite/excel)",
4+
"keywords": [
5+
"laravel",
6+
"excel",
7+
"pdf",
8+
"report",
9+
"excel report",
10+
"pdf report"
11+
],
12+
"license": "MIT",
13+
"authors": [
14+
{
15+
"name": "Samuel Terra",
16+
"email": "[email protected]"
17+
}
18+
],
19+
"require": {
20+
"php": ">=5.5.9",
21+
"illuminate/support": "~5",
22+
"maatwebsite/excel": "~2.1.0"
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"SamuelTerra22\\ReportGenerator\\": "src/"
27+
}
28+
},
29+
"extra": {
30+
"laravel": {
31+
"providers": [
32+
"SamuelTerra22\\ReportGenerator\\ServiceProvider"
33+
]
34+
}
35+
},
36+
"minimum-stability": "dev"
37+
}

config/report-generator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
'flush' => false
5+
];

readme.md

Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
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+
![Output Report with Grand Total](https://raw.githubusercontent.com/Jimmy-JS/laravel-report-generator/master/screenshots/report-with-total.png)
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+
![Output Report with Group By Grand Total](https://raw.githubusercontent.com/Jimmy-JS/laravel-report-generator/master/screenshots/report-with-group-by.png)
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+
```

screenshots/report-with-group-by.png

125 KB
Loading

screenshots/report-with-total.png

161 KB
Loading

src/Facades/CSVReportFacade.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace SamuelTerra22\ReportGenerator\Facades;
4+
5+
use Illuminate\Support\Facades\Facade as IlluminateFacade;
6+
7+
/**
8+
* @see \SamuelTerra22\ReportGenerator\ReportMedia\CSVReport
9+
*/
10+
class CSVReportFacade extends IlluminateFacade
11+
{
12+
/**
13+
* Get the registered name of the component.
14+
*
15+
* @return string
16+
*/
17+
protected static function getFacadeAccessor()
18+
{
19+
return 'csv.report.generator';
20+
}
21+
}

0 commit comments

Comments
 (0)