From 902221cae81ae0171195080486b466c322d41e50 Mon Sep 17 00:00:00 2001 From: Constantin Graf Date: Tue, 3 Dec 2024 16:57:14 +0100 Subject: [PATCH] Add debug flag to pdf export --- .../Api/V1/TimeEntryController.php | 19 +++++++++++++++++-- .../TimeEntryAggregateExportRequest.php | 9 +++++++++ .../TimeEntry/TimeEntryIndexExportRequest.php | 9 +++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Api/V1/TimeEntryController.php b/app/Http/Controllers/Api/V1/TimeEntryController.php index 35fc32a2..001961a5 100644 --- a/app/Http/Controllers/Api/V1/TimeEntryController.php +++ b/app/Http/Controllers/Api/V1/TimeEntryController.php @@ -173,6 +173,7 @@ public function indexExport(Organization $organization, TimeEntryIndexExportRequ } else { $this->checkPermission($organization, 'time-entries:view:all'); } + $debug = $request->getDebug(); $format = $request->getFormatValue(); if ($format === ExportFormat::PDF && ! $this->canAccessPremiumFeatures($organization)) { throw new FeatureIsNotAvailableInFreePlanApiException; @@ -195,7 +196,7 @@ public function indexExport(Organization $organization, TimeEntryIndexExportRequ $export = new TimeEntriesDetailedCsvExport(config('filesystems.private'), $folderPath, $filename, $timeEntriesQuery, 1000, $timezone); $export->export(); } elseif ($format === ExportFormat::PDF) { - if (config('services.gotenberg.url') === null) { + if (config('services.gotenberg.url') === null && ! $debug) { throw new PdfRendererIsNotConfiguredException; } $viewFile = file_get_contents(resource_path('views/reports/time-entry-index/pdf.blade.php')); @@ -225,6 +226,13 @@ public function indexExport(Organization $organization, TimeEntryIndexExportRequ throw new \LogicException('View file not found'); } $footerHtml = Blade::render($footerViewFile); + if ($debug) { + return response()->json([ + 'html' => $html, + 'footer_html' => $footerHtml, + ]); + } + $client = new Client([ 'auth' => config('services.gotenberg.basic_auth_username') !== null && config('services.gotenberg.basic_auth_password') !== null ? [ config('services.gotenberg.basic_auth_username'), @@ -347,6 +355,7 @@ public function aggregateExport(Organization $organization, TimeEntryAggregateEx if ($format === ExportFormat::PDF && ! $this->canAccessPremiumFeatures($organization)) { throw new FeatureIsNotAvailableInFreePlanApiException; } + $debug = $request->getDebug(); $user = $this->user(); $group = $request->getGroup(); @@ -381,7 +390,7 @@ public function aggregateExport(Organization $organization, TimeEntryAggregateEx $path = $folderPath.'/'.$filename; if ($format === ExportFormat::PDF) { - if (config('services.gotenberg.url') === null) { + if (config('services.gotenberg.url') === null && ! $debug) { throw new PdfRendererIsNotConfiguredException; } $client = new Client([ @@ -408,6 +417,12 @@ public function aggregateExport(Organization $organization, TimeEntryAggregateEx throw new \LogicException('View file not found'); } $footerHtml = Blade::render($footerViewFile); + if ($debug) { + return response()->json([ + 'html' => $html, + 'footer_html' => $footerHtml, + ]); + } $request = Gotenberg::chromium(config('services.gotenberg.url')) ->pdf() ->waitForExpression("window.status === 'ready'") diff --git a/app/Http/Requests/V1/TimeEntry/TimeEntryAggregateExportRequest.php b/app/Http/Requests/V1/TimeEntry/TimeEntryAggregateExportRequest.php index 9e403cf0..63e2b1ba 100644 --- a/app/Http/Requests/V1/TimeEntry/TimeEntryAggregateExportRequest.php +++ b/app/Http/Requests/V1/TimeEntry/TimeEntryAggregateExportRequest.php @@ -160,9 +160,18 @@ public function rules(): array 'string', 'in:true,false', ], + 'debug' => [ + 'string', + 'in:true,false', + ], ]; } + public function getDebug(): bool + { + return $this->input('debug') === 'true'; + } + public function getGroup(): TimeEntryAggregationType { return TimeEntryAggregationType::from($this->input('group')); diff --git a/app/Http/Requests/V1/TimeEntry/TimeEntryIndexExportRequest.php b/app/Http/Requests/V1/TimeEntry/TimeEntryIndexExportRequest.php index b8e72e8c..84c21562 100644 --- a/app/Http/Requests/V1/TimeEntry/TimeEntryIndexExportRequest.php +++ b/app/Http/Requests/V1/TimeEntry/TimeEntryIndexExportRequest.php @@ -129,9 +129,18 @@ public function rules(): array 'string', 'in:true,false', ], + 'debug' => [ + 'string', + 'in:true,false', + ], ]; } + public function getDebug(): bool + { + return $this->input('debug', 'false') === 'true'; + } + public function getStart(): Carbon { $start = Carbon::createFromFormat('Y-m-d\TH:i:s\Z', $this->input('start'), 'UTC');