jhosep32 / laravel-report
快速生成简单的 Laravel 5 Pdf & Excel 报告(使用 Barryvdh/DomPdf 或 Barryvdh/laravel-snappy & maatwebsite/excel)
Requires
- php: >=5.5.9
- illuminate/support: ~5
- maatwebsite/excel: ~3.1.2
This package is auto-updated.
Last update: 2024-09-17 10:14:13 UTC
README
快速在 Laravel 上生成简单的 Pdf 报告(使用 barryvdh/laravel-dompdf 或 barryvdh/laravel-snappy)或 Excel 报告(使用 Maatwebsite/Laravel-Excel)
此包提供简单的 pdf & excel 报告生成器,以加快您的工效
安装
将包添加到您的 composer
composer require jimmyjs/laravel-report-generator
然后,将 ServiceProvider 添加到 config/app.php 中的 providers 数组
Jimmyjs\ReportGenerator\ServiceProvider::class,
为了更好地生成 pdf 报告的速度,我建议您使用 laravel snappy 包。要使用 laravel snappy,您应该安装 wkhtmltopdf
以与该包一起使用 (跳转到 wkhtmltopdf 安装)
可选,您可以将此添加到 config/app.php 中的 aliases 数组
'PdfReport' => Jimmyjs\ReportGenerator\Facades\PdfReportFacade::class,
'ExcelReport' => Jimmyjs\ReportGenerator\Facades\ExcelReportFacade::class,
'CSVReport' => Jimmyjs\ReportGenerator\Facades\CSVReportFacade::class,
用法
此包使用 chunk
方法(Eloquent / Query Builder),因此它可以处理大数据而不会耗尽内存。
您还可以使用 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 报告没有样式,但生成报告更快)
参数:
- None
用法
ExcelReport::of($title, $meta, $queryBuilder, $columns) ->simple() ->download('filename');