coolsam / laravel-report-generator
在 Laravel 5 上快速生成简单的 PDF 和 Excel 报告(使用 Barryvdh/DomPdf 或 Barryvdh/laravel-snappy & maatwebsite/excel)
Requires
- php: >=7.2
- illuminate/support: ~5
- maatwebsite/excel: ~3.1.0
README
在 Laravel 上快速生成简单的 PDF 报告(使用 barryvdh/laravel-dompdf 或 barryvdh/laravel-snappy)或 CSV / Excel 报告(使用 Maatwebsite/Laravel-Excel)
此包提供简单的 PDF、CSV 和 Excel 报告生成器,以加快您的工作流程
安装
将包添加到您的 composer
composer require coolsam/laravel-report-generator
如果您正在运行 Laravel > 5.5,那么您需要做的就是这一点。如果您正在使用 Laravel < 5.5,请在 config/app.php 中的 providers 数组中添加 ServiceProvider
Coolsam\ReportGenerator\ServiceProvider::class,
可选,您可以将此添加到 config/app.php 中的 aliases 数组
'PdfReport' => Coolsam\ReportGenerator\Facades\PdfReportFacade::class,
'ExcelReport' => Coolsam\ReportGenerator\Facades\ExcelReportFacade::class,
'CSVReport' => Coolsam\ReportGenerator\Facades\CSVReportFacade::class,
为了在生成 PDF 报告时获得更好的速度,我建议您使用 laravel snappy 包。要使用 laravel snappy,您应该安装 wkhtmltopdf
来与此包一起使用 (跳转到 wkhtmltopdf 安装)
信息
此包可以使用 cursor
或 chunk
方法(Eloquent / Query Builder),因此它比 chunk 处理大数据更快,并最小化内存使用。
在 此处 查找 chunk
和 cursor
的比较
此外,您可以使用 PdfReport
、ExcelReport
或 CSVReport
门面来缩短已注册为别名的代码。
示例显示 PDF 代码
use PdfReport; public function displayReport(Request $request) { $fromDate = $request->input('from_date'); $toDate = $request->input('to_date'); $sortBy = $request->input('sort_by'); $title = 'Registered User Report'; // Report title $meta = [ // For displaying filters description on header 'Registered on' => $fromDate . ' To ' . $toDate, 'Sort By' => $sortBy ]; $queryBuilder = User::select(['name', 'balance', 'registered_at']) // Do some querying.. ->whereBetween('registered_at', [$fromDate, $toDate]) ->orderBy($sortBy); $columns = [ // Set Column to be displayed '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). return PdfReport::of($title, $meta, $queryBuilder, $columns) ->editColumn('Registered At', [ // Change column class or manipulate its data for displaying to report 'displayAs' => function($result) { return $result->registered_at->format('d M Y'); }, 'class' => 'left' ]) ->editColumns(['Total Balance', 'Status'], [ // Mass edit column 'class' => 'right bold' ]) ->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 'Total Balance' => 'point' // if you want to show dollar sign ($) then use 'Total Balance' => '$' ]) ->limit(20) // Limit record to be showed ->stream(); // other available method: download('filename') to download pdf / make() that will producing DomPDF / SnappyPdf instance so you could do any other DomPDF / snappyPdf method such as stream() or download() }
注意:对于下载到 Excel / CSV,只需将 PdfReport
门面更改为 ExcelReport
/ CSVReport
门面,无需进行更多修改
数据处理
$columns = [ 'Name' => 'name', 'Registered At' => 'registered_at', 'Total Balance' => 'balance', 'Status' => function($customer) { // You can do data manipulation, if statement or any action do you want inside this closure return ($customer->balance > 100000) ? 'Rich Man' : 'Normal Guy'; } ];
将与以下相同的结果产生
$columns = [ 'Name' => function($customer) { return $customer->name; }, 'Registered At' => function($customer) { return $customer->registered_at; }, 'Total Balance' => function($customer) { return $customer->balance; }, 'Status' => function($customer) { // You can do if statement or any action do you want inside this closure return ($customer->balance > 100000) ? 'Rich Man' : 'Normal Guy'; } ];
报告输出
使用此操作,您可以执行一些 预加载关系
$post = Post::with('comments')->where('active', 1); $columns = [ 'Post Title' => function($post) { return $post->title; }, 'Slug' => 'slug', 'Latest Comment' => function($post) { return $post->comments->first()->body; } ];
带有 Group By 的示例代码
或者,您可以使用 groupBy
方法按组总计所有记录
... // 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 $columns = [ // Set Column to be displayed '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') // Show total of value on specific group. Used with showTotal() enabled. ->showTotal([ 'Total Balance' => 'point' ]) ->stream();
请注意,在使用此 Group By 方法之前,请先通过查询对 GROUP BY 列进行排序。
按注册时间输出带 Group By 的报告
Wkhtmltopdf 安装
- 从 https://wkhtmltopdf.org/downloads.html 下载 wkhtmltopdf
- 将位于
/config/snappy.php
的 snappy 配置(如果snappy.php
文件尚未创建,请运行php artisan vendor:publish
)更改为
'pdf' => array(
'enabled' => true,
'binary' => '/usr/local/bin/wkhtmltopdf', // Or specified your custom wkhtmltopdf path
'timeout' => false,
'options' => array(),
'env' => array(),
),
其他方法
1. setPaper($paper = 'a4')
支持的媒体类型:PDF
描述:设置纸张大小
参数:
- $paper (默认: 'a4')
用法
PdfReport::of($title, $meta, $queryBuilder, $columns) ->setPaper('a6') ->make();
2. setCss(Array $styles)
支持的媒体类型:PDF,Excel
描述:使用给定的选择器和样式设置新的自定义样式
参数:
- Array $styles (键: $selector, 值: $style)
用法
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')
支持的媒体类型:PDF
描述:设置方向为横幅或纵向
参数:
- $orientation (默认: 'portrait')
用法
PdfReport::of($title, $meta, $queryBuilder, $columns) ->setOrientation('landscape') ->make();
4. withoutManipulation()
支持的媒体类型:PDF,Excel,CSV
描述:更快地生成报告,但所有列属性必须与 SQL 查询中选择的列匹配
用法
$queryBuilder = Customer::select(['name', 'age'])->get(); $columns = ['Name', 'Age']; PdfReport::of($title, $meta, $queryBuilder, $columns) ->withoutManipulation() ->make();
5. showMeta($value = true)
支持的媒体类型:PDF,Excel,CSV
描述:在报告中显示/隐藏元属性
参数:
- $value (默认: true)
用法
PdfReport::of($title, $meta, $queryBuilder, $columns) ->showMeta(false) // Hide meta ->make();
6. showHeader($value = true)
支持的媒体类型:PDF,Excel,CSV
描述:在报告中显示/隐藏列标题
参数:
- $value (默认: true)
用法
PdfReport::of($title, $meta, $queryBuilder, $columns) ->showHeader(false) // Hide column header ->make();
7. showNumColumn($value = true)
支持的媒体类型:PDF,Excel,CSV
描述:在报告中显示/隐藏数字列
参数:
- $value (默认: true)
用法
PdfReport::of($title, $meta, $queryBuilder, $columns) ->showNumColumn(false) // Hide number column ->make();
8. simple()
支持的媒体类型:Excel
描述:以简单模式生成Excel(生成的Excel报告无样式,但生成报告更快)
参数:
- 无
用法
ExcelReport::of($title, $meta, $queryBuilder, $columns) ->simple() ->download('filename');