Laravel Report Generators (PDF & Excel)
This package is inspired by the package of Jimmy-JS. Thanks Jimmy-JS., (*1)
Rapidly Generate Simple Pdf Report on Laravel (Using barryvdh/laravel-dompdf or barryvdh/laravel-snappy) or Excel Report (using Maatwebsite/Laravel-Excel), (*2)
This package provides a simple pdf & excel report generators to speed up your workflow, (*3)
Installation
Add package to your composer:, (*4)
composer require samuelterra22/laravel-report-generator
Then, add the ServiceProvider to the providers array in config/app.php, (*5)
SamuelTerra22\ReportGenerator\ServiceProvider::class,
Optionally, you can add this to your aliases array in config/app.php, (*6)
'PdfReport' => SamuelTerra22\ReportGenerator\Facades\PdfReportFacade::class,
'ExcelReport' => SamuelTerra22\ReportGenerator\Facades\ExcelReportFacade::class,
'CSVReport' => SamuelTerra22\ReportGenerator\Facades\CSVReportFacade::class,
Usage
This package is make use of chunk
method (Eloquent / Query Builder) so it can handle big data without memory exhausted., (*7)
Also, You can use PdfReport
, ExcelReport
or CSVReport
facade for shorter code that already registered as an alias., (*8)
Example Display PDF Code
// PdfReport Aliases
use PdfReport;
public function displayReport(Request $request)
{
// Retrieve any filters
$fromDate = $request->input('from_date');
$toDate = $request->input('to_date');
$sortBy = $request->input('sort_by');
// Report title
$title = 'Registered User Report';
// For displaying filters description on header
$meta = [
'Registered on' => $fromDate . ' To ' . $toDate,
'Sort By' => $sortBy
];
// Do some querying..
$queryBuilder = User::select([
'name',
'balance',
'registered_at'
])
->whereBetween('registered_at', [
$fromDate,
$toDate
])
->orderBy($sortBy);
// Set Column to be displayed
$columns = [
'Name' => 'name',
'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
'Total Balance' => 'balance',
'Status' => function ($result) { // You can do if statement or any action do you want inside this closure
return ($result->balance > 100000) ? 'Rich Man' : 'Normal Guy';
}
];
/*
Generate Report with flexibility to manipulate column class even manipulate column value (using Carbon, etc).
- of() : Init the title, meta (filters description to show), query, column (to be shown)
- editColumn() : To Change column class or manipulate its data for displaying to report
- editColumns(): Mass edit column
- 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
- groupBy() : Show total of value on specific group. Used with showTotal() enabled.
- limit() : Limit record to be showed
- make() : Will producing DomPDF / SnappyPdf instance so you could do any other DomPDF / snappyPdf method such as stream() or download()
*/
return PdfReport::of($title, $meta, $queryBuilder, $columns)
->editColumn('Registered At', [
'displayAs' => function ($result) {
return $result->registered_at->format('d M Y');
}
])
->editColumn('Total Balance', [
'displayAs' => function ($result) {
return thousandSeparator($result->balance);
}
])
->editColumns([
'Total Balance',
'Status'
], [
'class' => 'right bold'
])
->showTotal([
'Total Balance' => 'point'
// if you want to show dollar sign ($) then use 'Total Balance' => '$'
])
->limit(20)
->stream(); // or download('filename here..') to download pdf
}
Note: For downloading to excel, just change PdfReport
facade to ExcelReport
facade no more modifications, (*9)
Data Manipulation
$columns = [
'Name' => 'name',
'Registered At' => 'registered_at',
'Total Balance' => 'balance',
'Status' => function($result) { // You can do data manipulation, if statement or any action do you want inside this closure
return ($result->balance > 100000) ? 'Rich Man' : 'Normal Guy';
}
];
Will produce a same result with:, (*10)
$columns = [
'Name' => function($result) {
return $result->name;
},
'Registered At' => function($result) {
return $result->registered_at;
},
'Total Balance' => function($result) {
return $result->balance;
},
'Status' => function($result) { // You can do if statement or any action do you want inside this closure
return ($result->balance > 100000) ? 'Rich Man' : 'Normal Guy';
}
];
So you can do some eager loading relation like:, (*11)
$post = Post::with('comment')->where('active', 1);
$columns = [
'Post Title' => function($result) {
return $result->title;
},
'Slug' => 'slug',
'Top Comment' => function($result) {
return $result->comment->body;
}
];
Output Report
, (*12)
Example Code With Group By
Or, you can total all records by group using groupBy
method, (*13)
// ...
// Do some querying..
$queryBuilder = User::select(['name', 'balance', 'registered_at'])
->whereBetween('registered_at', [$fromDate, $toDate])
->orderBy('registered_at', 'ASC'); // You should sort groupBy column to use groupBy() Method
// Set Column to be displayed
$columns = [
'Registered At' => 'registered_at',
'Name' => 'name',
'Total Balance' => 'balance',
'Status' => function($result) { // You can do if statement or any action do you want inside this closure
return ($result->balance > 100000) ? 'Rich Man' : 'Normal Guy';
}
];
return PdfReport::of($title, $meta, $queryBuilder, $columns)
->editColumn('Registered At', [
'displayAs' => function ($result) {
return $result->registered_at->format('d M Y');
}
])
->editColumn('Total Balance', [
'class' => 'right bold',
'displayAs' => function ($result) {
return thousandSeparator($result->balance);
}
])
->editColumn('Status', [
'class' => 'right bold',
])
->groupBy('Registered At')
->showTotal([
'Total Balance' => 'point'
])
->stream();
PLEASE TAKE NOTE TO SORT GROUPBY COLUMN VIA QUERY FIRST TO USE THIS GROUP BY METHOD., (*14)
Output Report With Group By Registered At
, (*15)
Other Method
1. setPaper($paper = 'a4')
Supported Media Type: PDF, (*16)
Description: Set Paper Size, (*17)
Params:
* $paper (Default: 'a4'), (*18)
Usage:, (*19)
PdfReport::of($title, $meta, $queryBuilder, $columns)
->setPaper('a6')
->make();
2. setCss(Array $styles)
Supported Media Type: PDF, Excel, (*20)
Description: Set a new custom styles with given selector and style to apply, (*21)
Params:
* Array $styles (Key: $selector, Value: $style), (*22)
Usage:, (*23)
ExcelReport::of($title, $meta, $queryBuilder, $columns)
->editColumn('Registered At', [
'class' => 'right bolder italic-red'
])
->setCss([
'.bolder' => 'font-weight: 800;',
'.italic-red' => 'color: red;font-style: italic;'
])
->make();
3. setOrientation($orientation = 'portrait')
Supported Media Type: PDF, (*24)
Description: Set Orientation to Landscape or Portrait, (*25)
Params:
* $orientation (Default: 'portrait'), (*26)
Usage:, (*27)
PdfReport::of($title, $meta, $queryBuilder, $columns)
->setOrientation('landscape')
->make();
4. withoutManipulation()
Supported Media Type: PDF, Excel, CSV, (*28)
Description: Faster generating report, but all columns properties must be matched the selected column from SQL Queries, (*29)
Usage:, (*30)
$queryBuilder = Customer::select(['name', 'age'])->get();
$columns = ['Name', 'Age'];
PdfReport::of($title, $meta, $queryBuilder, $columns)
->withoutManipulation()
->make();
Supported Media Type: PDF, Excel, CSV, (*31)
Description: Show / hide meta attribute on report, (*32)
Params:
* $value (Default: true), (*33)
Usage:, (*34)
PdfReport::of($title, $meta, $queryBuilder, $columns)
->showMeta(false) // Hide meta
->make();
Supported Media Type: PDF, Excel, CSV, (*35)
Description: Show / hide column header on report, (*36)
Params:
* $value (Default: true), (*37)
Usage:, (*38)
PdfReport::of($title, $meta, $queryBuilder, $columns)
->showHeader(false) // Hide column header
->make();