From ea07b3850be1a58ce11eb1628db525a65b5af423 Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Thu, 18 Jul 2019 14:54:56 +0100
Subject: [PATCH] FIX #11287 wrong encoding in txt converted in pdf

---
 .../convert/controllers/ConvertPdfController.php   | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/src/app/convert/controllers/ConvertPdfController.php b/src/app/convert/controllers/ConvertPdfController.php
index 7eab74f75b9..35d64d13059 100755
--- a/src/app/convert/controllers/ConvertPdfController.php
+++ b/src/app/convert/controllers/ConvertPdfController.php
@@ -37,9 +37,8 @@ class ConvertPdfController
 
         $tmpPath = CoreConfigModel::getTmpPath();
 
-
+        self::addBom($aArgs['fullFilename']);
         $command = "unoconv -f pdf " . escapeshellarg($aArgs['fullFilename']);
-        
 
         exec('export HOME=' . $tmpPath . ' && '.$command.' 2>&1', $output, $return);
 
@@ -94,6 +93,7 @@ class ConvertPdfController
         copy($pathToDocument, $tmpPath.$fileNameOnTmp.'.'.$docInfo["extension"]);
 
         if (strtolower($docInfo["extension"]) != 'pdf') {
+            self::addBom($tmpPath.$fileNameOnTmp.'.'.$docInfo["extension"]);
             $command = "unoconv -f pdf " . escapeshellarg($tmpPath.$fileNameOnTmp.'.'.$docInfo["extension"]);
             exec('export HOME=' . $tmpPath . ' && '.$command, $output, $return);
 
@@ -148,6 +148,7 @@ class ConvertPdfController
 
         file_put_contents($tmpPath . $tmpFilename, base64_decode($aArgs['encodedResource']));
 
+        self::addBom($tmpPath.$tmpFilename);
         $command = "unoconv -f pdf {$tmpPath}{$tmpFilename}";
         exec('export HOME=' . $tmpPath . ' && '.$command, $output, $return);
 
@@ -204,4 +205,13 @@ class ConvertPdfController
 
         return $canConvert;
     }
+
+    public static function addBom($filePath) {
+        $extension = pathinfo($filePath, PATHINFO_EXTENSION);
+        if (strtolower($extension) == strtolower('txt')) {
+            $content = file_get_contents($filePath);
+            $bom = chr(239) . chr(187) . chr(191); # use BOM to be on safe side
+            file_put_contents($filePath, $bom.$content);
+        }
+    }
 }
-- 
GitLab