PHP code example of jimmyjs / pdf-report-generators
1. Go to this page and download the library: Download jimmyjs/pdf-report-generators library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jimmyjs / pdf-report-generators example snippets
use PdfReportGenerator;
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' => 'registered_at',
'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
- 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 instance so you could do any other DomPDF method such as stream() or download()
*/
return PdfReportGenerator::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'
])
->showTotal([
'Total Balance' => 'point'
])
->limit(20)
->make()
->stream(); // or download() to download pdf
}
$columns = [
'Name' => 'name',
'Registered At' => 'registered_at',
'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';
}
];
$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';
}
];