From c6c9ab2dc3a5ce9e8d5ab0c57b395aa12326c446 Mon Sep 17 00:00:00 2001 From: ajikamaludin Date: Tue, 31 Jan 2023 16:38:43 +0700 Subject: [PATCH] export pdf 50% --- app/Http/Controllers/DocumentController.php | 60 ++++ composer.json | 1 + composer.lock | 353 ++++++++++++++++++- resources/js/Components/NavDropdown.jsx | 8 +- resources/js/Layouts/AuthenticatedLayout.jsx | 16 +- resources/js/Pages/Company/FormModal.jsx | 2 +- resources/js/Pages/Document/Index.jsx | 4 +- resources/views/exports/documents.blade.php | 42 +++ routes/web.php | 1 + 9 files changed, 473 insertions(+), 14 deletions(-) create mode 100644 resources/views/exports/documents.blade.php diff --git a/app/Http/Controllers/DocumentController.php b/app/Http/Controllers/DocumentController.php index 887f6a6..5bd9b4c 100644 --- a/app/Http/Controllers/DocumentController.php +++ b/app/Http/Controllers/DocumentController.php @@ -7,7 +7,10 @@ use App\Models\Company; use App\Models\Document; use App\Models\Type; use App\Models\User; +use Barryvdh\DomPDF\Facade\Pdf; use Illuminate\Http\Request; +use OpenSpout\Writer\Common\Creator\Style\StyleBuilder; +use Rap2hpoutre\FastExcel\FastExcel; class DocumentController extends Controller { @@ -205,4 +208,61 @@ class DocumentController extends Controller { $doc->delete(); } + + public function import() + { + // + } + + + public function export(Request $request) + { + $query = Document::with(['variety', 'category', 'company.region.group'])->orderBy('created_at', 'desc'); + + $collections = collect([]); + foreach ($query->get() as $document) { + $collections->add([ + 'group' => $document->company->region->group->name, + 'region' => $document->company->region->name, + 'nama perusahaan' => $document->company->name, + 'jenis' => $document->variety->name, + 'kategori' => $document->category->name, + 'no' => $document->no_doc, + 'nama' => $document->name, + 'penerbit' => $document->publisher, + 'tipe' => $document->type == Document::TYPE_TETAP ? 'Tetap' : 'Tidak Tetap', + 'tanggal terbit' => $document->publish_date->format('d-m-Y'), + 'tanggal jatuh tempo' => $document->due_date->format('d-m-Y'), + 'keterangan' => $document->description, + 'file' => asset('documents/'.$document->document), + 'status' => $document->status == Document::STATUS_YES ? 'Ya' : 'Tidak', + 'catatan' => $document->due_status, + ]); + } + + if($request->type == 'pdf') { + return $this->exportAsPdf($collections); + } + if($request->type == 'excel') { + return $this->exportAsExcel($collections); + } + } + + private function exportAsPdf($collections) + { + $pdf = Pdf::loadView('exports.documents', ['collections' => $collections->toArray()]); + $date = now()->format('d-m-Y'); + + return $pdf->download("documents-$date.pdf"); + } + + private function exportAsExcel($collections) + { + $date = now()->format('d-m-Y'); + $header_style = (new StyleBuilder())->setFontBold()->build(); + + return (new FastExcel($collections)) + ->headerStyle($header_style) + ->download("document-$date.xlsx"); + } } diff --git a/composer.json b/composer.json index be0703b..dd27ab7 100644 --- a/composer.json +++ b/composer.json @@ -6,6 +6,7 @@ "license": "MIT", "require": { "php": "^8.0.2", + "barryvdh/laravel-dompdf": "^2.0", "guzzlehttp/guzzle": "^7.2", "inertiajs/inertia-laravel": "^0.6.9", "laravel/framework": "^9.48", diff --git a/composer.lock b/composer.lock index 9970e67..eedf0c5 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,85 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "6ea3a91a17dd8ccd5fa4886bcef58ef8", + "content-hash": "776f6b59288b5ff0657e18a74e116064", "packages": [ + { + "name": "barryvdh/laravel-dompdf", + "version": "v2.0.0", + "source": { + "type": "git", + "url": "https://github.com/barryvdh/laravel-dompdf.git", + "reference": "1d47648c6cef37f715ecb8bcc5f5a656ad372e27" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/barryvdh/laravel-dompdf/zipball/1d47648c6cef37f715ecb8bcc5f5a656ad372e27", + "reference": "1d47648c6cef37f715ecb8bcc5f5a656ad372e27", + "shasum": "" + }, + "require": { + "dompdf/dompdf": "^2", + "illuminate/support": "^6|^7|^8|^9", + "php": "^7.2 || ^8.0" + }, + "require-dev": { + "nunomaduro/larastan": "^1|^2", + "orchestra/testbench": "^4|^5|^6|^7", + "phpro/grumphp": "^1", + "squizlabs/php_codesniffer": "^3.5" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0-dev" + }, + "laravel": { + "providers": [ + "Barryvdh\\DomPDF\\ServiceProvider" + ], + "aliases": { + "Pdf": "Barryvdh\\DomPDF\\Facade\\Pdf", + "PDF": "Barryvdh\\DomPDF\\Facade\\Pdf" + } + } + }, + "autoload": { + "psr-4": { + "Barryvdh\\DomPDF\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Barry vd. Heuvel", + "email": "barryvdh@gmail.com" + } + ], + "description": "A DOMPDF Wrapper for Laravel", + "keywords": [ + "dompdf", + "laravel", + "pdf" + ], + "support": { + "issues": "https://github.com/barryvdh/laravel-dompdf/issues", + "source": "https://github.com/barryvdh/laravel-dompdf/tree/v2.0.0" + }, + "funding": [ + { + "url": "https://fruitcake.nl", + "type": "custom" + }, + { + "url": "https://github.com/barryvdh", + "type": "github" + } + ], + "time": "2022-07-06T11:12:10+00:00" + }, { "name": "brick/math", "version": "0.10.2", @@ -305,6 +382,68 @@ ], "time": "2022-12-15T16:57:16+00:00" }, + { + "name": "dompdf/dompdf", + "version": "v2.0.1", + "source": { + "type": "git", + "url": "https://github.com/dompdf/dompdf.git", + "reference": "c5310df0e22c758c85ea5288175fc6cd777bc085" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/dompdf/dompdf/zipball/c5310df0e22c758c85ea5288175fc6cd777bc085", + "reference": "c5310df0e22c758c85ea5288175fc6cd777bc085", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-mbstring": "*", + "masterminds/html5": "^2.0", + "phenx/php-font-lib": ">=0.5.4 <1.0.0", + "phenx/php-svg-lib": ">=0.3.3 <1.0.0", + "php": "^7.1 || ^8.0" + }, + "require-dev": { + "ext-json": "*", + "ext-zip": "*", + "mockery/mockery": "^1.3", + "phpunit/phpunit": "^7.5 || ^8 || ^9", + "squizlabs/php_codesniffer": "^3.5" + }, + "suggest": { + "ext-gd": "Needed to process images", + "ext-gmagick": "Improves image processing performance", + "ext-imagick": "Improves image processing performance", + "ext-zlib": "Needed for pdf stream compression" + }, + "type": "library", + "autoload": { + "psr-4": { + "Dompdf\\": "src/" + }, + "classmap": [ + "lib/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-2.1" + ], + "authors": [ + { + "name": "The Dompdf Community", + "homepage": "https://github.com/dompdf/dompdf/blob/master/AUTHORS.md" + } + ], + "description": "DOMPDF is a CSS 2.1 compliant HTML to PDF converter", + "homepage": "https://github.com/dompdf/dompdf", + "support": { + "issues": "https://github.com/dompdf/dompdf/issues", + "source": "https://github.com/dompdf/dompdf/tree/v2.0.1" + }, + "time": "2022-09-22T13:43:41+00:00" + }, { "name": "dragonmantank/cron-expression", "version": "v3.3.2", @@ -1686,6 +1825,75 @@ ], "time": "2022-04-17T13:12:02+00:00" }, + { + "name": "masterminds/html5", + "version": "2.7.6", + "source": { + "type": "git", + "url": "https://github.com/Masterminds/html5-php.git", + "reference": "897eb517a343a2281f11bc5556d6548db7d93947" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Masterminds/html5-php/zipball/897eb517a343a2281f11bc5556d6548db7d93947", + "reference": "897eb517a343a2281f11bc5556d6548db7d93947", + "shasum": "" + }, + "require": { + "ext-ctype": "*", + "ext-dom": "*", + "ext-libxml": "*", + "php": ">=5.3.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.8.35 || ^5.7.21 || ^6 || ^7" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.7-dev" + } + }, + "autoload": { + "psr-4": { + "Masterminds\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Matt Butcher", + "email": "technosophos@gmail.com" + }, + { + "name": "Matt Farina", + "email": "matt@mattfarina.com" + }, + { + "name": "Asmir Mustafic", + "email": "goetas@gmail.com" + } + ], + "description": "An HTML5 parser and serializer.", + "homepage": "http://masterminds.github.io/html5-php", + "keywords": [ + "HTML5", + "dom", + "html", + "parser", + "querypath", + "serializer", + "xml" + ], + "support": { + "issues": "https://github.com/Masterminds/html5-php/issues", + "source": "https://github.com/Masterminds/html5-php/tree/2.7.6" + }, + "time": "2022-08-18T16:18:26+00:00" + }, { "name": "monolog/monolog", "version": "2.8.0", @@ -2269,6 +2477,96 @@ ], "time": "2022-03-31T06:15:15+00:00" }, + { + "name": "phenx/php-font-lib", + "version": "0.5.4", + "source": { + "type": "git", + "url": "https://github.com/dompdf/php-font-lib.git", + "reference": "dd448ad1ce34c63d09baccd05415e361300c35b4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/dompdf/php-font-lib/zipball/dd448ad1ce34c63d09baccd05415e361300c35b4", + "reference": "dd448ad1ce34c63d09baccd05415e361300c35b4", + "shasum": "" + }, + "require": { + "ext-mbstring": "*" + }, + "require-dev": { + "symfony/phpunit-bridge": "^3 || ^4 || ^5" + }, + "type": "library", + "autoload": { + "psr-4": { + "FontLib\\": "src/FontLib" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-3.0" + ], + "authors": [ + { + "name": "Fabien Ménager", + "email": "fabien.menager@gmail.com" + } + ], + "description": "A library to read, parse, export and make subsets of different types of font files.", + "homepage": "https://github.com/PhenX/php-font-lib", + "support": { + "issues": "https://github.com/dompdf/php-font-lib/issues", + "source": "https://github.com/dompdf/php-font-lib/tree/0.5.4" + }, + "time": "2021-12-17T19:44:54+00:00" + }, + { + "name": "phenx/php-svg-lib", + "version": "0.5.0", + "source": { + "type": "git", + "url": "https://github.com/dompdf/php-svg-lib.git", + "reference": "76876c6cf3080bcb6f249d7d59705108166a6685" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/dompdf/php-svg-lib/zipball/76876c6cf3080bcb6f249d7d59705108166a6685", + "reference": "76876c6cf3080bcb6f249d7d59705108166a6685", + "shasum": "" + }, + "require": { + "ext-mbstring": "*", + "php": "^7.1 || ^8.0", + "sabberworm/php-css-parser": "^8.4" + }, + "require-dev": { + "phpunit/phpunit": "^7.5 || ^8.5 || ^9.5" + }, + "type": "library", + "autoload": { + "psr-4": { + "Svg\\": "src/Svg" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-3.0" + ], + "authors": [ + { + "name": "Fabien Ménager", + "email": "fabien.menager@gmail.com" + } + ], + "description": "A library to read, parse and export to PDF SVG files.", + "homepage": "https://github.com/PhenX/php-svg-lib", + "support": { + "issues": "https://github.com/dompdf/php-svg-lib/issues", + "source": "https://github.com/dompdf/php-svg-lib/tree/0.5.0" + }, + "time": "2022-09-06T12:16:56+00:00" + }, { "name": "phpoption/phpoption", "version": "1.9.0", @@ -3079,6 +3377,59 @@ ], "time": "2022-07-21T14:21:25+00:00" }, + { + "name": "sabberworm/php-css-parser", + "version": "8.4.0", + "source": { + "type": "git", + "url": "https://github.com/sabberworm/PHP-CSS-Parser.git", + "reference": "e41d2140031d533348b2192a83f02d8dd8a71d30" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sabberworm/PHP-CSS-Parser/zipball/e41d2140031d533348b2192a83f02d8dd8a71d30", + "reference": "e41d2140031d533348b2192a83f02d8dd8a71d30", + "shasum": "" + }, + "require": { + "ext-iconv": "*", + "php": ">=5.6.20" + }, + "require-dev": { + "codacy/coverage": "^1.4", + "phpunit/phpunit": "^4.8.36" + }, + "suggest": { + "ext-mbstring": "for parsing UTF-8 CSS" + }, + "type": "library", + "autoload": { + "psr-4": { + "Sabberworm\\CSS\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Raphael Schweikert" + } + ], + "description": "Parser for CSS Files written in PHP", + "homepage": "https://www.sabberworm.com/blog/2010/6/10/php-css-parser", + "keywords": [ + "css", + "parser", + "stylesheet" + ], + "support": { + "issues": "https://github.com/sabberworm/PHP-CSS-Parser/issues", + "source": "https://github.com/sabberworm/PHP-CSS-Parser/tree/8.4.0" + }, + "time": "2021-12-11T13:40:54+00:00" + }, { "name": "shiftonelabs/laravel-cascade-deletes", "version": "1.0.1", diff --git a/resources/js/Components/NavDropdown.jsx b/resources/js/Components/NavDropdown.jsx index 4185f0f..bfa5f1d 100644 --- a/resources/js/Components/NavDropdown.jsx +++ b/resources/js/Components/NavDropdown.jsx @@ -7,9 +7,11 @@ export default function NavDropdown({ name, items }) { return (
-