├── .gitignore ├── .travis.yml ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── config └── config.php ├── package-lock.json ├── phpunit.xml.dist ├── resources └── views │ ├── components │ ├── meta.blade.php │ ├── row.blade.php │ └── table-header.blade.php │ ├── footer.blade.php │ └── pdf.blade.php ├── src ├── Console │ └── Commands │ │ └── InstallCommand.php ├── Reporter.php ├── ReporterServiceProvider.php ├── Services │ ├── ExcelService.php │ └── PdfService.php └── View │ └── Components │ └── Row.php └── tests ├── BaseTest.php ├── ColumnsTest.php ├── ExcelTest.php ├── HtmlTest.php ├── PdfTest.php └── ReporterTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | composer.lock 3 | docs 4 | vendor 5 | coverage 6 | .idea 7 | testdox.* 8 | junit.xml 9 | teamcity.txt 10 | logfile.txt 11 | .phpunit.result.cache 12 | node_modules 13 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 7.1 5 | - 7.2 6 | - 7.3 7 | - 7.4 8 | - 8.0 9 | 10 | env: 11 | matrix: 12 | - COMPOSER_FLAGS="--prefer-lowest" 13 | - COMPOSER_FLAGS="" 14 | 15 | before_script: 16 | - travis_retry composer self-update 17 | - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source 18 | 19 | script: 20 | - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover 21 | 22 | after_script: 23 | - php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover 24 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Mo Khosh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Create PDF and Excel reports in Laravel and style them with Tailwind CSS 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/mokhosh/laravel-reporter.svg?style=flat-square)](https://packagist.org/packages/mokhosh/laravel-reporter) 4 | [![Total Downloads](https://img.shields.io/packagist/dt/mokhosh/laravel-reporter.svg?style=flat-square)](https://packagist.org/packages/mokhosh/laravel-reporter) 5 | 6 | This is basically going to be a wrapper for an Excel generator and a Pdf generator. For the time being these generators are Maatweb and Barry's snappy, that might change in the future. 7 | 8 | There is already a package that does this, but I didn't like the API, the coding style and the overall design, with all due respect of course. 9 | 10 | ## Requirements 11 | 12 | This package requires PHP 8. 13 | 14 | ## Installation 15 | 16 | You can install the package via composer. Make sure to run the Artisan install command to install npm dependencies. 17 | 18 | ```bash 19 | composer require mokhosh/laravel-reporter 20 | php artisan reporter:install 21 | ``` 22 | 23 | You shouldn't need to do anything else on your client. But on some servers you might need more dependencies. For example on Ubuntu you can run `ldd chrome | grep not` and it will give you a list of dependencies that Headless Chrome needs to run but you don't have. Then you can install them by running `apt install`. Here's a guide to help you with that: 24 | https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md 25 | 26 | ## Usage 27 | This is the simplest way to get a PDF. This will report all non-hidden columns: 28 | 29 | ```php 30 | use Mokhosh\Reporter\Reporter; 31 | 32 | $users = User::query(); 33 | 34 | return Reporter::report($users)->pdf(); // view in browser, aka inline 35 | ``` 36 | If you prefer to download the PDF file instead of showing it in the browser you can do this: 37 | ```php 38 | return Reporter::report($users)->download()->pdf(); // download, aka attachment 39 | ``` 40 | You can download the Excel version of your report: 41 | ```php 42 | return Reporter::report($users)->excel(); 43 | ``` 44 | ### Styles and Transforms 45 | If you don't pass a filter, `Reporter` will use your database columns to create its table. 46 | But if you use a filter, you can use any `accessor` on your model, and you can filter out some columns like this 47 | ```php 48 | $filter = [ 49 | 'id', 50 | 'name', 51 | 'email', 52 | 'created_at', 53 | ]; 54 | return Reporter::report($users, $filter)->pdf(); 55 | ``` 56 | That will use a Title Case version of column names for your table headers. If you wish to use custom table headers you can do so like this: 57 | ```php 58 | $filter = [ 59 | 'id' => 'ID', 60 | 'email' => '@', 61 | 'created_at' => 'Joined', 62 | ]; 63 | ``` 64 | You can also transform the data by passing a closure: 65 | ```php 66 | $filter = [ 67 | 'created_at' => fn($date) => $date->format('Y-m'), 68 | ]; 69 | ``` 70 | You can add Tailwind CSS classes to your table cells if you want. Cool, right? 71 | ```php 72 | $filter = [ 73 | 'id' => [ 74 | 'class' => 'font-bold text-gray-600 bg-gray-50' 75 | ], 76 | ]; 77 | ``` 78 | You can also mix and match in a million ways: 79 | ```php 80 | $filter = [ 81 | 'id' => 'ID', 82 | 'name', 83 | 'email' => [ 84 | 'transform' => fn($email) => strtoupper($email), 85 | 'class' => 'text-green-700 bg-green-100', 86 | ], 87 | 'created_at' => fn($date) => $date->format('Y-m'), 88 | 'updated_at' => [ 89 | 'title' => 'Last Seen', 90 | 'class' => 'text-red-400', 91 | ], 92 | ]; 93 | ```` 94 | The whole `model` is also passed to the `transform` closure, so you can access other columns if needed: 95 | ```php 96 | $filter = [ 97 | 'amount' => fn($amount, $model) => $model->currency . $amount, 98 | ]; 99 | ``` 100 | With this you can even create made-up columns that don't exist on your model and in your database: 101 | ```php 102 | $filter = [ 103 | 'madeup_name' => fn($_, $model) => $model->amount * $model->discount, 104 | ]; 105 | ``` 106 | Note that because this column does not really exist as a database column or even an accessor on your model, the first argument will be `null`. That's why I've named it `$_`. 107 | 108 | You can also change the Title of the generated pdf and add metadata 109 | ```php 110 | $title = 'Users Report'; 111 | $meta = [ 112 | 'Admin' => 'Mo Khosh', 113 | ]; 114 | 115 | return Reporter::report($query, $columns, $title, $meta, footer: true)->pdf(); 116 | ``` 117 | You can decide to show generation date, total pages and page number in footer 118 | ```php 119 | return Reporter::report($query, $columns, footer: true)->pdf(); 120 | ``` 121 | You can put your logo in the header 122 | ```php 123 | return Reporter::report($query, $columns, logo: 'https://address-to-logo.com')->pdf(); 124 | ``` 125 | ## TODO 126 | - [ ] I'm thinking of adding header classes 127 | ```php 128 | $filter = [ 129 | 'id' => 'ID', 130 | 'email' => [ 131 | 'class' => 'text-green-700 bg-green-100', 132 | 'header-class' => 'text-green-100 bg-green-700', 133 | ], 134 | ]; 135 | ``` 136 | I'm also thinking of conditional classes that are added to a cell when its content meets a condition passed through a closure that returns a boolean value. 137 | ```php 138 | $filter = [ 139 | 'created_at' => [ 140 | 'conditional-classes' => [ 141 | [ 142 | 'class' => 'text-red-600', 143 | 'condition' => fn($date) => $date->gt(now()->subWeek()), 144 | ], 145 | [ 146 | 'class' => 'text-green-600', 147 | 'condition' => fn($date) => $date->lt(now()->subYear()), 148 | ], 149 | ], 150 | ], 151 | ]; 152 | ``` 153 | - [ ] A good API for is passing default styles for the table header, and even/odd rows 154 | - [ ] Add headers and footers with page number, date, etc. 155 | 156 | ### Testing 157 | 158 | ``` bash 159 | composer test 160 | ``` 161 | 162 | ### Changelog 163 | 164 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 165 | 166 | ## Contributing 167 | 168 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 169 | 170 | ## Credits 171 | 172 | - [Mo Khosh](https://github.com/mokhosh) 173 | - [All Contributors](../../contributors) 174 | 175 | ## License 176 | 177 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 178 | 179 | ## Laravel Package Boilerplate 180 | 181 | This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com). 182 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mokhosh/laravel-reporter", 3 | "description": "Create PDF and Excel reports in Laravel and style them with Tailwind CSS.", 4 | "keywords": [ 5 | "mokhosh", 6 | "laravel-reporter" 7 | ], 8 | "homepage": "https://github.com/mokhosh/laravel-reporter", 9 | "license": "MIT", 10 | "type": "library", 11 | "authors": [ 12 | { 13 | "name": "Mo Khosh", 14 | "email": "mskhoshnazar@gmail.com", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^8.0", 20 | "illuminate/support": "^8.0", 21 | "maatwebsite/excel": "^3.1", 22 | "nesk/puphpeteer": "^2.0" 23 | }, 24 | "require-dev": { 25 | "orchestra/testbench": "^6.0", 26 | "phpunit/phpunit": "^9.5.0" 27 | }, 28 | "autoload": { 29 | "psr-4": { 30 | "Mokhosh\\Reporter\\": "src" 31 | } 32 | }, 33 | "autoload-dev": { 34 | "psr-4": { 35 | "Mokhosh\\Reporter\\Tests\\": "tests" 36 | } 37 | }, 38 | "scripts": { 39 | "test": "vendor/bin/phpunit", 40 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage" 41 | }, 42 | "config": { 43 | "sort-packages": true 44 | }, 45 | "extra": { 46 | "laravel": { 47 | "providers": [ 48 | "Mokhosh\\Reporter\\ReporterServiceProvider" 49 | ], 50 | "aliases": { 51 | "Reporter": "Mokhosh\\Reporter\\ReporterFacade" 52 | } 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /config/config.php: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | tests 15 | 16 | 17 | 21 | 22 | src/ 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /resources/views/components/meta.blade.php: -------------------------------------------------------------------------------- 1 | @if(! empty($meta)) 2 |
3 | @foreach($meta as $name => $value) 4 |
5 |
{{ $name }}
6 |
{{ ucwords($value) }}
7 |
8 | @endforeach 9 |
10 | @endif 11 | -------------------------------------------------------------------------------- /resources/views/components/row.blade.php: -------------------------------------------------------------------------------- 1 | {{-- todo config or something for even/odd classes? --}} 2 | 3 | @foreach($formattedRow as $column) 4 | {{ $column->title }} 5 | @endforeach 6 | 7 | -------------------------------------------------------------------------------- /resources/views/components/table-header.blade.php: -------------------------------------------------------------------------------- 1 | @if ($header) 2 | 3 | 4 | @foreach ($columns as $title => $modifier) 5 | {{ is_array($modifier) ? $modifier['title'] : $modifier }} 6 | @endforeach 7 | 8 | 9 | @endif 10 | -------------------------------------------------------------------------------- /resources/views/footer.blade.php: -------------------------------------------------------------------------------- 1 | 13 | -------------------------------------------------------------------------------- /resources/views/pdf.blade.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {{ $title }} 6 | 7 | 8 | 9 | 10 |
11 | @if(! is_null($logo)) 12 | 13 | @endif 14 |
{{ $title }}
15 |
16 | 17 | 18 |
19 | 20 | 21 | @foreach($query->cursor() as $row) 22 | 23 | @endforeach 24 |
25 |
26 | 27 | 28 | -------------------------------------------------------------------------------- /src/Console/Commands/InstallCommand.php: -------------------------------------------------------------------------------- 1 | setHidden(true); 18 | } 19 | } 20 | 21 | public function handle() 22 | { 23 | shell_exec('npm install @nesk/puphpeteer'); 24 | return 0; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Reporter.php: -------------------------------------------------------------------------------- 1 | getHtml(), filename: $this->getFileName(), options: $this->getOptions()); 40 | return $this->download ? $service->download() : $service->inline(); 41 | } 42 | 43 | public function excel() 44 | { 45 | $service = new ExcelService(view: $this->getView(), filename: $this->getFileName()); 46 | return $service->download(); 47 | } 48 | 49 | public function download($download = true): static 50 | { 51 | $this->download = $download; 52 | 53 | return $this; 54 | } 55 | 56 | public function getColumns(): array 57 | { 58 | if (empty($this->columns)) $this->columns = $this->getColumnsFromModel(); 59 | 60 | $columns = []; 61 | foreach ($this->columns as $key => $value) { 62 | if (is_numeric($key) && is_string($value)) { 63 | $columns[$value] = $this->getTitleFromColumnName($value); 64 | } elseif (is_string($key)) { 65 | $columns[$key] = $value; 66 | if (is_object($value) && $value instanceof Closure) $columns[$key] = ['transform' => $value]; 67 | if (is_array($columns[$key])) { 68 | $columns[$key]['title'] ??= $this->getTitleFromColumnName($key); 69 | $columns[$key]['class'] ??= ''; 70 | $columns[$key]['transform'] ??= fn($a) => $a; 71 | $columns[$key]['format'] ??= '@'; 72 | } 73 | } else { 74 | throw new Exception('Wrong set of columns'); 75 | } 76 | } 77 | 78 | return $columns; 79 | } 80 | 81 | public function getColumnsFromModel(): array 82 | { 83 | return array_diff( 84 | Schema::getColumnListing($this->query->getQuery()->from), 85 | $this->query->getModel()->getHidden() 86 | ); 87 | } 88 | 89 | public function getTitle(): string 90 | { 91 | return $this->title; 92 | } 93 | 94 | public function getMeta(): array 95 | { 96 | return $this->meta; 97 | } 98 | 99 | public function getView(): Renderable 100 | { 101 | return View::make('laravel-reporter::pdf', [ 102 | 'query' => $this->query, 103 | 'columns' => $this->getColumns(), 104 | 'title' => $this->title, 105 | 'meta' => $this->meta, 106 | 'logo' => $this->logo, 107 | 'header' => $this->header, 108 | ]); 109 | } 110 | 111 | public function getHtml(): string 112 | { 113 | return $this->getView()->render(); 114 | } 115 | 116 | protected function getTitleFromColumnName(string $value): string 117 | { 118 | return (string)Str::of($value)->title()->replace('_', ' '); 119 | } 120 | 121 | protected function getOptions(): array 122 | { 123 | return [ 124 | 'format' => 'a4', 125 | 'margin' => [ 126 | 'top' => '36px', 127 | 'right' => '36px', 128 | 'bottom' => '56px', 129 | 'left' => '36px', 130 | ], 131 | 'displayHeaderFooter' => $this->footer, 132 | 'footerTemplate' => $this->getFooterTemplate(), 133 | 'headerTemplate' => '', 134 | ]; 135 | } 136 | 137 | protected function getFooterTemplate(): string 138 | { 139 | return View::make('laravel-reporter::footer')->render(); 140 | } 141 | 142 | public function getFileName() 143 | { 144 | $fileName = $this->title; 145 | 146 | foreach ($this->meta as $key => $value) { 147 | $fileName = $fileName.'-'.$key.'-'.$value; 148 | } 149 | 150 | return Str::kebab($fileName); 151 | } 152 | } 153 | -------------------------------------------------------------------------------- /src/ReporterServiceProvider.php: -------------------------------------------------------------------------------- 1 | loadViewsFrom(__DIR__.'/../resources/views', 'laravel-reporter'); 14 | $this->loadViewComponentsAs('laravel-reporter', [ 15 | Row::class, 16 | ]); 17 | 18 | if ($this->app->runningInConsole()) { 19 | 20 | $this->commands([ 21 | InstallCommand::class, 22 | ]); 23 | 24 | // Publishing the configs. 25 | /*$this->publishes([ 26 | __DIR__.'/../config/config.php' => config_path('laravel-reporter.php'), 27 | ], 'config');*/ 28 | // Publishing the views. 29 | /*$this->publishes([ 30 | __DIR__.'/../resources/views' => resource_path('views/vendor/laravel-reporter'), 31 | ], 'views');*/ 32 | } 33 | } 34 | 35 | public function register() 36 | { 37 | // Automatically apply the package configuration 38 | // $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'laravel-reporter'); 39 | 40 | // Register the main class to use with the facade 41 | // $this->app->singleton('laravel-reporter', function () { 42 | // return new Reporter; 43 | // }); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Services/ExcelService.php: -------------------------------------------------------------------------------- 1 | filename = $this->filename.'.xlsx'; 20 | } 21 | 22 | public function createExcel(): ExportView 23 | { 24 | return new ExportView($this->view); 25 | } 26 | 27 | public function download(): BinaryFileResponse 28 | { 29 | return Excel::download($this->createExcel(), $this->filename); 30 | } 31 | 32 | public function getFilename(): string 33 | { 34 | return $this->filename; 35 | } 36 | } 37 | 38 | class ExportView implements FromView, ShouldAutoSize 39 | { 40 | use Exportable; 41 | public function __construct(private View $view) {} 42 | public function view(): View {return $this->view;} 43 | } 44 | -------------------------------------------------------------------------------- /src/Services/PdfService.php: -------------------------------------------------------------------------------- 1 | filename = $this->filename.'.pdf'; 18 | } 19 | 20 | public function createPdf(): string 21 | { 22 | $browser = (new Puppeteer)->launch(); 23 | $page = $browser->newPage(); 24 | $page->setContent($this->html); 25 | $page->pdf(array_merge($this->options, ['path' => storage_path($this->filename)])); 26 | $browser->close(); 27 | 28 | return storage_path($this->filename); 29 | } 30 | 31 | public function download(): BinaryFileResponse 32 | { 33 | return response()->download($this->createPdf(), $this->filename, [ 34 | 'Content-Type' => 'application/pdf', 35 | 'Content-Disposition' => 'attachment; filename="'.$this->filename.'"' 36 | ])->deleteFileAfterSend(true); 37 | } 38 | 39 | public function inline(): BinaryFileResponse 40 | { 41 | return response()->file($this->createPdf(), [ 42 | 'Content-Type' => 'application/pdf', 43 | 'Content-Disposition' => 'inline; filename="'.$this->filename.'"' 44 | ])->deleteFileAfterSend(true); 45 | } 46 | 47 | public function getFilename(): string 48 | { 49 | return $this->filename; 50 | } 51 | } -------------------------------------------------------------------------------- /src/View/Components/Row.php: -------------------------------------------------------------------------------- 1 | columns as $title => $modifier) { 24 | $columns[] = (object) [ 25 | 'class' => is_array($modifier) 26 | ? $modifier['class'] 27 | : '', 28 | 'title' => is_array($modifier) 29 | ? $modifier['transform']($this->row->{$title}, $this->row) 30 | : $this->stringify($this->row->{$title}), 31 | 'format' => is_array($modifier) 32 | ? $modifier['format'] 33 | : '@', 34 | ]; 35 | } 36 | 37 | return $columns; 38 | } 39 | 40 | #[Pure] 41 | public function stringify(mixed $mixed): string 42 | { 43 | if (is_array($mixed)) return implode($mixed); 44 | if (is_object($mixed) && !!! $mixed instanceof Stringable) return '-'; 45 | return (string) $mixed; 46 | } 47 | 48 | public function render(): View 49 | { 50 | return view('laravel-reporter::components.row'); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /tests/BaseTest.php: -------------------------------------------------------------------------------- 1 | loadLaravelMigrations(); 17 | } 18 | 19 | protected function getPackageProviders($app): array 20 | { 21 | return [ 22 | ReporterServiceProvider::class, 23 | ExcelServiceProvider::class, 24 | ]; 25 | } 26 | } 27 | 28 | Class User extends BaseUser 29 | { 30 | protected $guarded = []; 31 | protected $hidden = ['password']; 32 | } 33 | -------------------------------------------------------------------------------- /tests/ColumnsTest.php: -------------------------------------------------------------------------------- 1 | 'Mo Khosh', 16 | 'email' => 'mskhoshnazar@gmail.com', 17 | 'password' => 'password', 18 | ]); 19 | 20 | $data = Reporter::report(User::query())->getColumns(); 21 | 22 | $this->assertArrayHasKey('email', $data); 23 | $this->assertArrayNotHasKey('password', $data); 24 | } 25 | 26 | /** @test */ 27 | public function it_filters_query_data() 28 | { 29 | User::create([ 30 | 'name' => 'Mo Khosh', 31 | 'email' => 'mskhoshnazar@gmail.com', 32 | 'password' => 'password', 33 | ]); 34 | 35 | $data = Reporter::report(User::query(), columns: ['id', 'email'])->getColumns(); 36 | 37 | $this->assertArrayHasKey('email', $data); 38 | $this->assertArrayNotHasKey('name', $data); 39 | } 40 | 41 | 42 | /** @test */ 43 | public function it_interprets_simple_columns_correctly() 44 | { 45 | $filter = [ 46 | 'id', 47 | 'name', 48 | 'email', 49 | 'created_at', 50 | ]; 51 | 52 | $expected = [ 53 | 'id' => 'Id', 54 | 'name' => 'Name', 55 | 'email' => 'Email', 56 | 'created_at' => 'Created At', 57 | ]; 58 | 59 | $actual = Reporter::report(User::query(), columns: $filter)->getColumns(); 60 | 61 | $this->assertEquals($expected, $actual); 62 | } 63 | 64 | /** @test */ 65 | public function it_interprets_associative_columns_correctly() 66 | { 67 | $filter = [ 68 | 'id' => 'ID', 69 | 'name' => 'Name', 70 | 'email' => 'Email', 71 | 'created_at' => 'Created', 72 | ]; 73 | 74 | $actual = Reporter::report(User::query(), columns: $filter)->getColumns(); 75 | 76 | $this->assertEquals($filter, $actual); 77 | } 78 | 79 | /** @test */ 80 | public function it_interprets_complex_columns_correctly() 81 | { 82 | $filter = [ 83 | 'id', 84 | 'name', 85 | 'email' => [ 86 | 'transform' => fn($email) => strtolower($email), 87 | 'class' => 'text-green-700 bg-green-100', 88 | ], 89 | 'created_at' => fn($date) => $date->format('Y-m'), 90 | ]; 91 | 92 | $expected = [ 93 | 'id' => 'Id', 94 | 'name' => 'Name', 95 | 'email' => [ 96 | 'title' => 'Email', 97 | 'transform' => fn($email) => strtolower($email), 98 | 'class' => 'text-green-700 bg-green-100', 99 | ], 100 | 'created_at' => [ 101 | 'title' => 'Created At', 102 | 'transform' => fn($date) => $date->format('Y-m'), 103 | 'class' => '', 104 | ], 105 | ]; 106 | 107 | $actual = Reporter::report(User::query(), columns: $filter)->getColumns(); 108 | 109 | $this->assertEquals($expected, $actual); 110 | 111 | $filter = [ 112 | 'id' => 'ID', 113 | 'name', 114 | 'email' => ['title' => '@'], 115 | 'created_at' => fn($date) => $date->format('Y-m'), 116 | ]; 117 | 118 | $expected = [ 119 | 'id' => 'ID', 120 | 'name' => 'Name', 121 | 'email' => [ 122 | 'title' => '@', 123 | 'transform' => fn($email) => $email, 124 | 'class' => '', 125 | ], 126 | 'created_at' => [ 127 | 'title' => 'Created At', 128 | 'transform' => fn($date) => $date->format('Y-m'), 129 | 'class' => '', 130 | ], 131 | ]; 132 | 133 | $actual = Reporter::report(User::query(), columns: $filter)->getColumns(); 134 | 135 | $this->assertEquals($expected, $actual); 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /tests/ExcelTest.php: -------------------------------------------------------------------------------- 1 | 'Mo Khosh', 18 | 'email' => 'mskhoshnazar@gmail.com', 19 | 'password' => 'password', 20 | ]); 21 | 22 | $users = User::query(); 23 | 24 | $response = Reporter::report($users)->download()->excel(); 25 | 26 | $this->assertEquals($response->getStatusCode(), 200); 27 | $this->assertStringContainsString('attachment', (string) $response); 28 | 29 | $response = Reporter::report($users, download: true)->excel(); 30 | 31 | $this->assertEquals($response->getStatusCode(), 200); 32 | $this->assertStringContainsString('attachment', (string) $response); 33 | 34 | $response = Reporter::report($users)->excel(); 35 | 36 | $this->assertEquals($response->getStatusCode(), 200); 37 | $this->assertStringContainsString('attachment', (string) $response); 38 | 39 | $response = Reporter::report($users, download: false)->excel(); 40 | 41 | $this->assertEquals($response->getStatusCode(), 200); 42 | $this->assertStringContainsString('attachment', (string) $response); 43 | } 44 | 45 | /** @test */ 46 | public function it_will_name_files_with_correct_extension() 47 | { 48 | $service = new ExcelService(view: View::make('laravel-reporter::pdf'), filename: 'some-file-name'); 49 | 50 | $this->assertEquals('some-file-name.xlsx', $service->getFilename()); 51 | } 52 | } -------------------------------------------------------------------------------- /tests/HtmlTest.php: -------------------------------------------------------------------------------- 1 | 'Mo Khosh', 17 | 'email' => 'mskhoshnazar@gmail.com', 18 | 'password' => 'password', 19 | ]); 20 | 21 | $data = Reporter::report(User::query(), columns: ['id', 'email'])->getHtml(); 22 | 23 | $this->assertStringContainsString('mskhoshnazar', $data); 24 | $this->assertStringNotContainsString('Mo Khosh', $data); 25 | 26 | $data = Reporter::report(User::query(), columns: ['id', 'name', 'email'])->getHtml(); 27 | 28 | $this->assertStringContainsString('mskhoshnazar', $data); 29 | $this->assertStringContainsString('Mo Khosh', $data); 30 | } 31 | 32 | /** @test */ 33 | public function it_renders_title_and_meta_to_html() 34 | { 35 | $title = 'Title'; 36 | $meta = ['Something' => 'Nothing']; 37 | 38 | $data = Reporter::report(User::query(), title: $title, meta: $meta)->getHtml(); 39 | 40 | $this->assertStringContainsString($title, $data); 41 | $this->assertStringContainsString(reset($meta), $data); 42 | } 43 | 44 | /** @test */ 45 | public function it_formats_rows_correctly() 46 | { 47 | $user = User::create([ 48 | 'name' => 'Mo Khosh', 49 | 'email' => 'mskhoshnazar@gmail.com', 50 | 'password' => 'password', 51 | ]); 52 | 53 | $filter = [ 54 | 'id', 55 | 'name', 56 | 'email' => [ 57 | 'transform' => fn($email) => strtoupper($email), 58 | 'class' => 'text-green-700 bg-green-100', 59 | 'title' => 'Email', 60 | ], 61 | 'created_at' => fn($date) => $date->format('Y-m'), 62 | ]; 63 | 64 | $columns = Reporter::report(User::query(), columns: $filter)->getColumns(); 65 | 66 | $expected = [ 67 | 0 => (object) [ 68 | "class" => "", 69 | "title" => 1, 70 | ], 71 | 1 => (object) [ 72 | "class" => "", 73 | "title" => "Mo Khosh", 74 | ], 75 | 2 => (object) [ 76 | "class" => "text-green-700 bg-green-100", 77 | "title" => "MSKHOSHNAZAR@GMAIL.COM", 78 | ], 79 | 3 => (object) [ 80 | "class" => "", 81 | "title" => now()->format('Y-m'), 82 | ], 83 | ]; 84 | 85 | $actual = (new Row($user, $columns, isEven: false))->formattedRow(); 86 | 87 | $this->assertEquals($expected, $actual); 88 | } 89 | 90 | /** @test */ 91 | public function it_renders_logo() 92 | { 93 | $html = Reporter::report(User::query(), logo: 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png')->getHtml(); 94 | 95 | $this->assertStringContainsString('assertStringContainsString('google.com/images', $html); 97 | } 98 | 99 | /** @test */ 100 | public function it_passes_the_whole_model() 101 | { 102 | $user = User::create([ 103 | 'name' => 'Mo Khosh', 104 | 'email' => 'mskhoshnazar@gmail.com', 105 | 'password' => 'password', 106 | ]); 107 | 108 | $filter = [ 109 | 'email' => [ 110 | 'transform' => fn($email, $model) => $model->name, 111 | ], 112 | ]; 113 | 114 | $columns = Reporter::report(User::query(), columns: $filter)->getColumns(); 115 | 116 | $expected = [ 117 | 0 => (object) [ 118 | "class" => "", 119 | "title" => "Mo Khosh", 120 | ], 121 | ]; 122 | 123 | $actual = (new Row($user, $columns, isEven: false))->formattedRow(); 124 | 125 | $this->assertEquals($expected, $actual); 126 | } 127 | 128 | /** @test */ 129 | public function it_can_handle_madeup_columns() 130 | { 131 | $user = User::create([ 132 | 'name' => 'Mo Khosh', 133 | 'email' => 'mskhoshnazar@gmail.com', 134 | 'password' => 'password', 135 | ]); 136 | 137 | $filter = [ 138 | 'something' => [ 139 | 'transform' => fn($_, $model) => $model->name, 140 | ], 141 | ]; 142 | 143 | $columns = Reporter::report(User::query(), columns: $filter)->getColumns(); 144 | 145 | $expected = [ 146 | 0 => (object) [ 147 | "class" => "", 148 | "title" => "Mo Khosh", 149 | ], 150 | ]; 151 | 152 | $actual = (new Row($user, $columns, isEven: false))->formattedRow(); 153 | 154 | $this->assertEquals($expected, $actual); 155 | } 156 | } 157 | -------------------------------------------------------------------------------- /tests/PdfTest.php: -------------------------------------------------------------------------------- 1 | 'Mo Khosh', 15 | 'email' => 'mskhoshnazar@gmail.com', 16 | 'password' => 'password', 17 | ]); 18 | 19 | $users = User::query(); 20 | 21 | $response = Reporter::report($users)->download()->pdf(); 22 | 23 | $this->assertEquals($response->getStatusCode(), 200); 24 | $this->assertStringContainsString('attachment', (string) $response); 25 | 26 | $response = Reporter::report($users, download: true)->pdf(); 27 | 28 | $this->assertEquals($response->getStatusCode(), 200); 29 | $this->assertStringContainsString('attachment', (string) $response); 30 | } 31 | 32 | /** @test */ 33 | public function it_will_show_pdf_without_download() 34 | { 35 | User::create([ 36 | 'name' => 'Mo Khosh', 37 | 'email' => 'mskhoshnazar@gmail.com', 38 | 'password' => 'password', 39 | ]); 40 | 41 | $users = User::query(); 42 | 43 | $response = Reporter::report($users)->pdf(); 44 | 45 | $this->assertEquals($response->getStatusCode(), 200); 46 | $this->assertStringContainsString('inline', (string) $response); 47 | $this->assertStringContainsString('application/pdf', (string) $response); 48 | } 49 | 50 | /** @test */ 51 | public function it_will_name_files_with_correct_extension() 52 | { 53 | $service = new PdfService(html: '
', filename: 'some-file-name'); 54 | 55 | $this->assertEquals('some-file-name.pdf', $service->getFilename()); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /tests/ReporterTest.php: -------------------------------------------------------------------------------- 1 | assertTrue($reporter instanceof Reporter); 17 | } 18 | 19 | /** @test */ 20 | public function it_has_title_and_meta() 21 | { 22 | $title = 'Title'; 23 | $meta = ['Something' => 'Nothing']; 24 | 25 | $reporter = Reporter::report(User::query(), title: $title, meta: $meta); 26 | 27 | $this->assertEquals($title, $reporter->getTitle()); 28 | $this->assertEquals($meta, $reporter->getMeta()); 29 | } 30 | 31 | /** @test */ 32 | public function it_generates_filename_based_on_meta() 33 | { 34 | $title = 'User Report'; 35 | $meta = ['Admin' => 'Nothing']; 36 | 37 | $reporter = Reporter::report(User::query(), title: $title, meta: $meta); 38 | 39 | $this->assertEquals('user-report-admin-nothing', $reporter->getFileName()); 40 | } 41 | 42 | // todo orientation 43 | // todo limit and group 44 | // todo show total 45 | // todo paper size 46 | // todo simple version 47 | // todo header and footer 48 | // todo manual number column? 49 | } 50 | --------------------------------------------------------------------------------