salvatorecervone / laravel-report-generator
快速生成简单的 Laravel 5 Pdf & Excel 报表(使用 Barryvdh/DomPdf 或 Barryvdh/laravel-snappy & maatwebsite/excel)
Requires
- php: >=7.1.0
- illuminate/support: >=5.0
- maatwebsite/excel: ^3.1
This package is auto-updated.
Last update: 2024-09-09 19:54:27 UTC
README
在 Laravel 上快速生成简单的 Pdf 报表(使用 barryvdh/laravel-dompdf 或 barryvdh/laravel-snappy)或 CSV / Excel 报表(使用 Maatwebsite/Laravel-Excel)
此包提供简单的 pdf、csv & excel 报表生成器,以加快您的工作流程。它还允许您无缝地 stream()、download() 或 store() 报表。
版本
在此处查找 chunk
和 cursor
的比较 here
安装
将包添加到您的 composer
composer require salvatorecervone/laravel-report-generator
如果您正在运行 Laravel > 5.5,那么您只需这样做。如果您正在使用 Laravel < 5.5,请将 ServiceProvider 添加到 config/app.php 中的 providers 数组
salvatorecervone\ReportGenerator\ServiceProvider::class,
可选,您可以将此添加到 config/app.php 中的 aliases 数组
'PdfReport' => salvatorecervone\ReportGenerator\Facades\PdfReportFacade::class,
'ExcelReport' => salvatorecervone\ReportGenerator\Facades\ExcelReportFacade::class,
'CSVReport' => salvatorecervone\ReportGenerator\Facades\CSVReportFacade::class,
可选,您可以将配置文件发布(然后它将在 config/report-generator.php 中可用)
php artisan vendor:publish --provider="salvatorecervone\ReportGenerator\ServiceProvider"
如果您要生成 pdf 报表,请安装 dompdf / snappy pdf。此包将自动使用 snappy pdf。如果您想使用 dompdf,请更改 config/report-generator.php
return [
'flush' => false,
'pdfLibrary' => 'dompdf'
];
为了更好地生成 pdf 报表的性能,我建议您使用 laravel snappy 包。要使用 laravel snappy,您应安装 wkhtmltopdf
来与此包一起工作 (跳转到 wkhtmltopdf 安装)
示例显示 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). $PdfRreport = new PdfReport; 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: store('path/to/file.pdf') to save to disk, 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');