Skip to content

Commit

Permalink
Add debug flag to pdf export
Browse files Browse the repository at this point in the history
  • Loading branch information
korridor committed Dec 3, 2024
1 parent d3f2d0a commit ad49c40
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
20 changes: 18 additions & 2 deletions app/Http/Controllers/Api/V1/TimeEntryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'));
Expand Down Expand Up @@ -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,
]);

Check warning on line 233 in app/Http/Controllers/Api/V1/TimeEntryController.php

View check run for this annotation

Codecov / codecov/patch

app/Http/Controllers/Api/V1/TimeEntryController.php#L230-L233

Added lines #L230 - L233 were not covered by tests
}

$client = new Client([
'auth' => config('services.gotenberg.basic_auth_username') !== null && config('services.gotenberg.basic_auth_password') !== null ? [
config('services.gotenberg.basic_auth_username'),
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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([
Expand All @@ -402,12 +411,19 @@ public function aggregateExport(Organization $organization, TimeEntryAggregateEx
'subGroup' => $subGroup,
'start' => $request->getStart()->timezone($timezone),
'end' => $request->getEnd()->timezone($timezone),
'debug' => $debug,
]);
$footerViewFile = file_get_contents(resource_path('views/reports/time-entry-aggregate/pdf-footer.blade.php'));
if ($footerViewFile === false) {
throw new \LogicException('View file not found');
}
$footerHtml = Blade::render($footerViewFile);
if ($debug) {
return response()->json([
'html' => $html,
'footer_html' => $footerHtml,
]);

Check warning on line 425 in app/Http/Controllers/Api/V1/TimeEntryController.php

View check run for this annotation

Codecov / codecov/patch

app/Http/Controllers/Api/V1/TimeEntryController.php#L422-L425

Added lines #L422 - L425 were not covered by tests
}
$request = Gotenberg::chromium(config('services.gotenberg.url'))
->pdf()
->waitForExpression("window.status === 'ready'")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion resources/pdf-js/echarts.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion resources/views/reports/time-entry-aggregate/pdf.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<script>
window.status = 'processing';
</script>
<script src="echarts.min.js"></script>
<script src="{{ $debug ? 'https://cdn.jsdelivr.net/npm/echarts@5.5.1/dist/echarts.min.js' : 'echarts.min.js' }}"></script>
</head>
<body>

Expand Down

0 comments on commit ad49c40

Please sign in to comment.