Giter Club home page Giter Club logo

laravel-report-generator's Introduction

Laravel Report Generators (PDF & Excel)

This package is inspired by the package of Jimmy-JS. Thanks Jimmy-JS.

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

This package provides a simple pdf & excel report generators to speed up your workflow

Installation

Add package to your composer:

composer require samuelterra22/laravel-report-generator

Then, add the ServiceProvider to the providers array in config/app.php

SamuelTerra22\ReportGenerator\ServiceProvider::class,

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

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

Usage

This package is make use of chunk method (Eloquent / Query Builder) so it can handle big data without memory exhausted.

Also, You can use PdfReport, ExcelReport or CSVReport facade for shorter code that already registered as an alias.

Example Display PDF Code

// PdfReport Aliases
use PdfReport;

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',
        // 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).

        - 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
        - editColumns(): Mass edit column
        - 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 / SnappyPdf instance so you could do any other DomPDF / snappyPdf method such as stream() or download()
    */
    return PdfReport::of($title, $meta, $queryBuilder, $columns)
        ->editColumn('Registered At', [
            'displayAs' => function ($result) {
                return $result->registered_at->format('d M Y');
            }
        ])
        ->editColumn('Total Balance', [
            'displayAs' => function ($result) {
                return thousandSeparator($result->balance);
            }
        ])
        ->editColumns([
            'Total Balance',
            'Status'
        ], [
            'class' => 'right bold'
        ])
        ->showTotal([
            'Total Balance' => 'point'
            // if you want to show dollar sign ($) then use 'Total Balance' => '$'
        ])
        ->limit(20)
        ->stream(); // or download('filename here..') to download pdf
}

Note: For downloading to excel, just change PdfReport facade to ExcelReport facade no more modifications

Data Manipulation

$columns = [
    'Name' => 'name',
    'Registered At' => 'registered_at',
    'Total Balance' => 'balance',
    'Status' => function($result) { // You can do data manipulation, if statement or any action do you want inside this closure
        return ($result->balance > 100000) ? 'Rich Man' : 'Normal Guy';
    }
];

Will produce a same result with:

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

So you can do some eager loading relation like:

$post = Post::with('comment')->where('active', 1);

$columns = [
    'Post Title' => function($result) {
        return $result->title;
    },
    'Slug' => 'slug',
    'Top Comment' => function($result) {
        return $result->comment->body;
    }
];

Output Report

Output Report with Total

Example Code With Group By

Or, you can total all records by group using groupBy method

    // ...
    // 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

    // Set Column to be displayed
    $columns = [
        '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')
        ->showTotal([
            'Total Balance' => 'point'
        ])
        ->stream();

PLEASE TAKE NOTE TO SORT GROUPBY COLUMN VIA QUERY FIRST TO USE THIS GROUP BY METHOD.

Output Report With Group By Registered At

Output Report with Group By Total

Other Method

1. setPaper($paper = 'a4')

Supported Media Type: PDF

Description: Set Paper Size

Params:

  • $paper (Default: 'a4')

Usage:

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

2. setCss(Array $styles)

Supported Media Type: PDF, Excel

Description: Set a new custom styles with given selector and style to apply

Params:

  • Array $styles (Key: $selector, Value: $style)

Usage:

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

Description: Set Orientation to Landscape or Portrait

Params:

  • $orientation (Default: 'portrait')

Usage:

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

4. withoutManipulation()

Supported Media Type: PDF, Excel, CSV

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

Usage:

$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

Description: Show / hide meta attribute on report

Params:

  • $value (Default: true)

Usage:

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

6. showHeader($value = true)

Supported Media Type: PDF, Excel, CSV

Description: Show / hide column header on report

Params:

  • $value (Default: true)

Usage:

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

laravel-report-generator's People

Contributors

dependabot-preview[bot] avatar heidilux avatar samuelterra22 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

laravel-report-generator's Issues

Laravel Framework 6.17.1 Installation Problem

Your requirements could not be resolved to an installable set of packages.

Problem 1
- Conclusion: don't install jimmyjs/laravel-report-generator 1.1.1
- Conclusion: remove laravel/framework v6.17.1
- Installation request for jimmyjs/laravel-report-generator ^1.1 -> satisfiable by jimmyjs/laravel-report-generator[1.1.0, 1.1.1].
- Conclusion: don't install laravel/framework v6.17.1
- jimmyjs/laravel-report-generator 1.1.0 requires illuminate/support ~5 -> satisfiable by illuminate/support[5.0.x-dev, 5.1.x-dev, 5.2.x-dev, 5.3.x-dev, 5.4.x-dev, 5.5.x-dev, 5.6.x-dev, 5.7.17, 5.7.18, 5.7.19, 5.7.x-dev, 5.8.x-dev, v5.0.0, v5.0.22, v5.0.25, v5.0.26, v5.0.28, v5.0.33, v5.0.4, v5.1.1, v5.1.13, v5.1.16, v5.1.2, v5.1.20, v5.1.22, v5.1.25, v5.1.28, v5.1.30, v5.1.31, v5.1.41, v5.1.6, v5.1.8, v5.2.0, v5.2.19, v5.2.21, v5.2.24, v5.2.25, v5.2.26, v5.2.27, v5.2.28, v5.2.31, v5.2.32, v5.2.37, v5.2.43, v5.2.45, v5.2.6, v5.2.7, v5.3.0, v5.3.16, v5.3.23, v5.3.4, v5.4.0, v5.4.13, v5.4.17, v5.4.19, v5.4.27, v5.4.36, v5.4.9, v5.5.0, v5.5.16, v5.5.17, v5.5.2, v5.5.28, v5.5.33, v5.5.34, v5.5.35, v5.5.36, v5.5.37, v5.5.39, v5.5.40, v5.5.41, v5.5.43, v5.5.44, v5.6.0, v5.6.1, v5.6.10, v5.6.11, v5.6.12, v5.6.13, v5.6.14, v5.6.15, v5.6.16, v5.6.17, v5.6.19, v5.6.2, v5.6.20, v5.6.21, v5.6.22, v5.6.23, v5.6.24, v5.6.25, v5.6.26, v5.6.27, v5.6.28, v5.6.29, v5.6.3, v5.6.30, v5.6.31, v5.6.32, v5.6.33, v5.6.34, v5.6.35, v5.6.36, v5.6.37, v5.6.38, v5.6.39, v5.6.4, v5.6.5, v5.6.6, v5.6.7, v5.6.8, v5.6.9, v5.7.0, v5.7.1, v5.7.10, v5.7.11, v5.7.15, v5.7.2, v5.7.20, v5.7.21, v5.7.22, v5.7.23, v5.7.26, v5.7.27, v5.7.28, v5.7.3, v5.7.4, v5.7.5, v5.7.6, v5.7.7, v5.7.8, v5.7.9, v5.8.0, v5.8.11, v5.8.12, v5.8.14, v5.8.15, v5.8.17, v5.8.18, v5.8.19, v5.8.2, v5.8.20, v5.8.22, v5.8.24, v5.8.27, v5.8.28, v5.8.29, v5.8.3, v5.8.30, v5.8.31, v5.8.32, v5.8.33, v5.8.34, v5.8.35, v5.8.36, v5.8.4, v5.8.8, v5.8.9].

Method Illuminate\Support\Collection::cursor does not exist.

I have successfully installed report generator without any errors in Laravel 7.
Upon creating my first report I am getting this error :

Facade\Ignition\Exceptions\ViewException
Method Illuminate\Support\Collection::cursor does not exist. (View: E:\ZAH\Laravel\myaccounts-laravelreport\vendor\samuelterra22\laravel-report-generator\src\views\general-pdf-template.blade.php)

Call to undefined method Symfony\Component\HttpFoundation\BinaryFileResponse::export()

Frist it will give this error Call to undefined method Maatwebsite\Excel\Excel::create()

after that i manually change the method name from create to download on your package because of excel package new version remove the create function and change to download and store so i changed them to download.
Getting this error when try to download the excel file.

Call to undefined method Symfony\Component\HttpFoundation\BinaryFileResponse::export()

Thank you.

App::make() throws BindingResolutionException now instead of ReflectionException

I'm using dompdf instead of snappy on a Laravel v6 install. The package attempts to load snappy first in a try catch, but it's attempting to catch a ReflectionException, when the make() method throws a BindingResolutionException.

If I make the quick change in ReportMedia/PdfReport, it seems to fix the issue.
Lines 41 and 44.

Call to undefined method Maatwebsite\Excel\Excel::create()

It seems that with the maatwebsite/excel dependency bump to 3.1, the ExcelReport no longer functions.

See: https://docs.laravel-excel.com/3.1/getting-started/upgrade.html

Deprecations

ALL Laravel Excel 2.* methods are deprecated and will not be able to use in 3.0 .

Excel::load() is removed and replaced by Excel::import($yourImport)
Excel::create() is removed and replaced by Excel::download/Excel::store($yourExport)
Excel::create()->string('xlsx') is removed an replaced by Excel::raw($yourExport, Excel::XLSX)
3.0 provides no convenience methods for styling, you are encouraged to use PhpSpreadsheets native methods.

In this case, I'm running into an issue at ExcelReport::make() ln:153, but there are likely others.

Do you intend to update this library to work with v3.1 of maatwebsite/excel dependency?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.