├── .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 | [](https://packagist.org/packages/mokhosh/laravel-reporter)
4 | [](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 |