Skip to content

Beautiful Open-Source Form Builder

Add UTF-8 BOM to async CSV exports (#1217)

Nameless-Monster-Nerdac82524ee8f1
diff --git a/api/app/Service/Forms/FormExportService.php b/api/app/Service/Forms/FormExportService.php
index b4a0404fb..f5c51a249 100644
--- a/api/app/Service/Forms/FormExportService.php
+++ b/api/app/Service/Forms/FormExportService.php
@@ -112,6 +112,9 @@ class FormExportService
$output = fopen('php://temp', 'r+');
+ // Help spreadsheet applications such as Excel detect UTF-8 correctly.
+ fwrite($output, "\xEF\xBB\xBF");
+
// Write header row (clean column names)
$headers = $this->cleanColumnNames(array_keys($rows[0]));
fputcsv($output, $headers);
diff --git a/api/tests/Feature/Forms/FormSubmissionExportTest.php b/api/tests/Feature/Forms/FormSubmissionExportTest.php
index f05443b45..e4d7cdf95 100644
--- a/api/tests/Feature/Forms/FormSubmissionExportTest.php
+++ b/api/tests/Feature/Forms/FormSubmissionExportTest.php
@@ -450,6 +450,22 @@ it('uses the workspace policy for asynchronous CSV download links', function ()
Storage::assertExists(FormExportService::EXPORT_FILE_PATH . 'weekend-submissions.csv');
});
+it('includes a UTF-8 BOM in asynchronous CSV exports', function () {
+ Storage::fake();
+ $exportService = app(FormExportService::class);
+ $fileName = 'unicode-submissions.csv';
+
+ $exportService->generateAndUploadCsvFile([
+ ['name' => 'பெயர்'],
+ ], $fileName, now()->addHour());
+
+ $csv = Storage::get(FormExportService::EXPORT_FILE_PATH . $fileName);
+
+ expect($csv)
+ ->toStartWith("\xEF\xBB\xBF")
+ ->toContain('பெயர்');
+});
+
it('allows export status polling when the general api rate limit is exhausted', function () {
$this->withMiddleware(ThrottleRequests::class);