2017 © Pedro Peláez
 

library laravel-report-generator

Rapidly Generate Simple Pdf & Excel Report on Laravel 5 (Using Barryvdh/DomPdf or Barryvdh/laravel-snappy & maatwebsite/excel)

image

jimmyjs/laravel-report-generator

Rapidly Generate Simple Pdf & Excel Report on Laravel 5 (Using Barryvdh/DomPdf or Barryvdh/laravel-snappy & maatwebsite/excel)

  • Monday, July 23, 2018
  • by JimmyJS
  • Repository
  • 10 Watchers
  • 85 Stars
  • 5,494 Installations
  • HTML
  • 0 Dependents
  • 0 Suggesters
  • 23 Forks
  • 4 Open issues
  • 22 Versions
  • 30 % Grown

The README.md

Laravel Report Generators (PDF, CSV & Excel)

Rapidly Generate Simple Pdf Report on Laravel (Using barryvdh/laravel-dompdf or barryvdh/laravel-snappy) or CSV / Excel Report (using Maatwebsite/Laravel-Excel), (*1)

This package provides a simple pdf, csv & excel report generators to speed up your workflow. It also allows you to stream(), download(), or store() the report seamlessly., (*2)

Version

Version Laravel Version Php Version Maatwebsite/Excel Ver Feature
1.0 <= 5.6 <=7.0 ~2.1.0 using chunk() to handle big data
1.1 <= 5.6 <=7.0 ~2.1.0 using cursor() to handle big data
2.0 >= 5.5 ^7.0 ^3.1 Using new version of maatwebsite (v3.1)

Find the comparison between chunk and cursor in here, (*3)

Installation

Add package to your composer:, (*4)

composer require jimmyjs/laravel-report-generator

If you are running Laravel > 5.5 that's all you need to do. If you are using Laravel < 5.5 add the ServiceProvider to the providers array in config/app.php, (*5)

Jimmyjs\ReportGenerator\ServiceProvider::class,

Optionally, you can add this to your aliases array in config/app.php, (*6)

'PdfReport' => Jimmyjs\ReportGenerator\Facades\PdfReportFacade::class,
'ExcelReport' => Jimmyjs\ReportGenerator\Facades\ExcelReportFacade::class,
'CSVReport' => Jimmyjs\ReportGenerator\Facades\CSVReportFacade::class,

Optionally, You can publish the config file (then it will be available in config/report-generator.php), (*7)

php artisan vendor:publish --provider="Jimmyjs\ReportGenerator\ServiceProvider"

If you want to generate a pdf report, please install either dompdf / snappy pdf. This package will automatically use snappy pdf. If you want to use dompdf then please change config/report-generator.php:, (*8)

return [
    'flush' => false,
    'pdfLibrary' => 'dompdf'
];

For better speed on generating pdf report, I recommend you to use laravel snappy package. To using laravel snappy, you should install wkhtmltopdf to work with this package (Jump to wkhtmltopdf installation), (*9)

Example Display PDF Code

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: 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()
}

Note: For downloading to excel / CSV, just change PdfReport facade to ExcelReport / CSVReport facade with no more modifications, (*10)

Data Manipulation

$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';
    }
];

Will produce a same result with:, (*11)

$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';
    }
];

Report Output

Report Output with Grand Total, (*12)

With this manipulation, you could do some eager loading relation like:, (*13)

$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;
    }
];

Example Code With Group By

Or, you can total all records by group using groupBy method, (*14)

    ...
    // 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();

PLEASE TAKE NOTE TO SORT GROUPBY COLUMN VIA QUERY FIRST TO USE THIS GROUP BY METHOD., (*15)

Output Report With Group By Registered At

Output Report with Group By Grand Total, (*16)

Wkhtmltopdf Installation

  • Download wkhtmltopdf from https://wkhtmltopdf.org/downloads.html
  • Change your snappy config located in /config/snappy.php (run php artisan vendor:publish if snappy.php file is not created) to:
    'pdf' => array(
        'enabled' => true,
        'binary'  => '/usr/local/bin/wkhtmltopdf', // Or specified your custom wkhtmltopdf path
        'timeout' => false,
        'options' => array(),
        'env'     => array(),
    ),

Other Method

1. setPaper($paper = 'a4')

Supported Media Type: PDF, (*17)

Description: Set Paper Size, (*18)

Params: * $paper (Default: 'a4'), (*19)

Usage:, (*20)

PdfReport::of($title, $meta, $queryBuilder, $columns)
         ->setPaper('a6')
         ->make();

2. setCss(Array $styles)

Supported Media Type: PDF, Excel, (*21)

Description: Set a new custom styles with given selector and style to apply, (*22)

Params: * Array $styles (Key: $selector, Value: $style), (*23)

Usage:, (*24)

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, (*25)

Description: Set Orientation to Landscape or Portrait, (*26)

Params: * $orientation (Default: 'portrait'), (*27)

Usage:, (*28)

PdfReport::of($title, $meta, $queryBuilder, $columns)
         ->setOrientation('landscape')
         ->make();

4. withoutManipulation()

Supported Media Type: PDF, Excel, CSV, (*29)

Description: Faster generating report, but all columns properties must be matched the selected column from SQL Queries, (*30)

Usage:, (*31)

$queryBuilder = Customer::select(['name', 'age'])->get();
$columns = ['Name', 'Age'];
PdfReport::of($title, $meta, $queryBuilder, $columns)
         ->withoutManipulation()
         ->make();

5. showMeta($value = true)

Supported Media Type: PDF, Excel, CSV, (*32)

Description: Show / hide meta attribute on report, (*33)

Params: * $value (Default: true), (*34)

Usage:, (*35)

PdfReport::of($title, $meta, $queryBuilder, $columns)
         ->showMeta(false) // Hide meta
         ->make();

6. showHeader($value = true)

Supported Media Type: PDF, Excel, CSV, (*36)

Description: Show / hide column header on report, (*37)

Params: * $value (Default: true), (*38)

Usage:, (*39)

PdfReport::of($title, $meta, $queryBuilder, $columns)
         ->showHeader(false) // Hide column header
         ->make();

7. showNumColumn($value = true)

Supported Media Type: PDF, Excel, CSV, (*40)

Description: Show / hide number column on report, (*41)

Params: * $value (Default: true), (*42)

Usage:, (*43)

PdfReport::of($title, $meta, $queryBuilder, $columns)
         ->showNumColumn(false) // Hide number column
         ->make();

8. simple()

Supported Media Type: Excel, (*44)

Description: Generate excel in simple mode (no styling on generated excel report, but faster in generating report), (*45)

Params: * None, (*46)

Usage:, (*47)

ExcelReport::of($title, $meta, $queryBuilder, $columns)
         ->simple()
         ->download('filename');

The Versions

23/07 2018

dev-master

9999999-dev

Rapidly Generate Simple Pdf & Excel Report on Laravel 5 (Using Barryvdh/DomPdf or Barryvdh/laravel-snappy & maatwebsite/excel)

  Sources   Download

MIT

The Requires

 

by Jimmy Setiawan

laravel excel pdf report excel report pdf report

23/07 2018

1.0.20

1.0.20.0

Rapidly Generate Simple Pdf & Excel Report on Laravel 5 (Using Barryvdh/DomPdf or Barryvdh/laravel-snappy & maatwebsite/excel)

  Sources   Download

MIT

The Requires

 

by Jimmy Setiawan

laravel excel pdf report excel report pdf report

23/07 2018

1.0.19

1.0.19.0

Rapidly Generate Simple Pdf & Excel Report on Laravel 5 (Using Barryvdh/DomPdf or Barryvdh/laravel-snappy & maatwebsite/excel)

  Sources   Download

MIT

The Requires

 

by Jimmy Setiawan

laravel excel pdf report excel report pdf report

12/06 2018

1.0.18

1.0.18.0

Rapidly Generate Simple Pdf & Excel Report on Laravel 5 (Using Barryvdh/DomPdf or Barryvdh/laravel-snappy & maatwebsite/excel)

  Sources   Download

MIT

The Requires

 

by Jimmy Setiawan

laravel excel pdf report excel report pdf report

11/06 2018

1.0.17

1.0.17.0

Rapidly Generate Simple Pdf & Excel Report on Laravel 5 (Using Barryvdh/DomPdf or Barryvdh/laravel-snappy & maatwebsite/excel)

  Sources   Download

MIT

The Requires

 

by Jimmy Setiawan

laravel excel pdf report excel report pdf report

26/02 2018

1.0.16

1.0.16.0

Rapidly Generate Simple Pdf & Excel Report on Laravel 5 (Using Barryvdh/DomPdf or Barryvdh/laravel-snappy & maatwebsite/excel)

  Sources   Download

MIT

The Requires

 

by Jimmy Setiawan

laravel excel pdf report excel report pdf report

04/12 2017

1.0.15

1.0.15.0

Rapidly Generate Simple Pdf & Excel Report on Laravel 5 (Using Barryvdh/DomPdf or Barryvdh/laravel-snappy & maatwebsite/excel)

  Sources   Download

MIT

The Requires

 

by Jimmy Setiawan

laravel excel pdf report excel report pdf report

20/11 2017

1.0.14

1.0.14.0

Rapidly Generate Simple Pdf & Excel Report on Laravel 5 (Using Barryvdh/DomPdf or Barryvdh/laravel-snappy & maatwebsite/excel)

  Sources   Download

MIT

The Requires

 

by Jimmy Setiawan

laravel excel pdf report excel report pdf report

17/11 2017

1.0.13

1.0.13.0

Rapidly Generate Simple Pdf & Excel Report on Laravel 5 (Using Barryvdh/DomPdf or Barryvdh/laravel-snappy & maatwebsite/excel)

  Sources   Download

MIT

The Requires

 

by Jimmy Setiawan

laravel excel pdf report excel report pdf report

16/09 2017

1.0.12

1.0.12.0

Rapidly Generate Simple Pdf & Excel Report on Laravel 5 (Using Barryvdh/DomPdf or Barryvdh/laravel-snappy & maatwebsite/excel)

  Sources   Download

MIT

The Requires

 

by Jimmy Setiawan

laravel excel pdf report excel report pdf report

16/09 2017

1.0.11

1.0.11.0

Rapidly Generate Simple Pdf & Excel Report on Laravel 5 (Using Barryvdh/DomPdf or Barryvdh/laravel-snappy & maatwebsite/excel)

  Sources   Download

MIT

The Requires

 

by Jimmy Setiawan

laravel excel pdf report excel report pdf report

14/09 2017

1.0.10

1.0.10.0

Rapidly Generate Simple Pdf & Excel Report on Laravel 5 (Using Barryvdh/DomPdf or Barryvdh/laravel-snappy & maatwebsite/excel)

  Sources   Download

MIT

The Requires

 

by Jimmy Setiawan

laravel excel pdf report excel report pdf report

14/09 2017

1.0.9

1.0.9.0

Rapidly Generate Simple Pdf & Excel Report on Laravel 5 (Using Barryvdh/DomPdf or Barryvdh/laravel-snappy & maatwebsite/excel)

  Sources   Download

MIT

The Requires

 

by Jimmy Setiawan

laravel excel pdf report excel report pdf report

14/09 2017

1.0.8

1.0.8.0

Rapidly Generate Simple Pdf & Excel Report on Laravel 5 (Using Barryvdh/DomPdf or Barryvdh/laravel-snappy & maatwebsite/excel)

  Sources   Download

MIT

The Requires

 

by Jimmy Setiawan

laravel excel pdf report excel report pdf report

09/09 2017

1.0.7

1.0.7.0

Rapidly Generate Simple Pdf & Excel Report on Laravel 5 (Using Barryvdh/DomPdf or Barryvdh/laravel-snappy & maatwebsite/excel)

  Sources   Download

MIT

The Requires

 

by Jimmy Setiawan

laravel excel pdf report excel report pdf report

11/08 2017

1.0.6

1.0.6.0

Rapidly Generate Simple Pdf & Excel Report on Laravel 5 (Using Barryvdh/DomPdf or Barryvdh/laravel-snappy & maatwebsite/excel)

  Sources   Download

MIT

The Requires

 

by Jimmy Setiawan

laravel excel pdf report excel report pdf report

09/08 2017

1.0.5

1.0.5.0

Rapidly Generate Simple Pdf & Excel Report on Laravel 5 (Using Barryvdh/DomPdf or Barryvdh/laravel-snappy & maatwebsite/excel)

  Sources   Download

MIT

The Requires

 

by Jimmy Setiawan

laravel excel pdf report excel report pdf report

09/08 2017

1.0.4

1.0.4.0

Rapidly Generate Simple Pdf & Excel Report on Laravel 5 (Using Barryvdh/DomPdf or Barryvdh/laravel-snappy & maatwebsite/excel)

  Sources   Download

MIT

The Requires

 

by Jimmy Setiawan

laravel excel pdf report excel report pdf report

12/06 2017

1.0.3

1.0.3.0

Rapidly Generate Simple Pdf & Excel Report on Laravel 5 (Using Barryvdh/DomPdf or Barryvdh/laravel-snappy & maatwebsite/excel)

  Sources   Download

MIT

The Requires

 

by Jimmy Setiawan

laravel excel pdf report excel report pdf report

07/06 2017

1.0.2

1.0.2.0

Rapidly Generate Simple Pdf & Excel Report on Laravel 5 (Using Barryvdh/DomPdf or Barryvdh/laravel-snappy & maatwebsite/excel)

  Sources   Download

MIT

The Requires

 

by Jimmy Setiawan

laravel excel pdf report excel report pdf report

12/05 2017

1.0.1

1.0.1.0

Rapidly Generate Simple Pdf & Excel Report on Laravel 5 (Using Barryvdh/DomPdf or Barryvdh/laravel-snappy & maatwebsite/excel)

  Sources   Download

MIT

The Requires

 

by Jimmy Setiawan

laravel excel pdf report excel report pdf report

10/05 2017

1.0.0

1.0.0.0

Rapidly Generate Simple Pdf & Excel Report on Laravel 5 (Using Barryvdh/DomPdf or Barryvdh/laravel-snappy & maatwebsite/excel)

  Sources   Download

MIT

The Requires

 

by Jimmy Setiawan

laravel excel pdf report excel report pdf report