From a834913033ef425c441b7ec2535932a6d4275afb Mon Sep 17 00:00:00 2001
From: Guillaume Heurtier <guillaume.heurtier@maarch.org>
Date: Wed, 26 Aug 2020 11:49:58 +0200
Subject: [PATCH] FIX #14661 TIME 1:35 fix convert html files to pdf in debian

---
 .../controllers/ConvertPdfController.php      | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/src/app/convert/controllers/ConvertPdfController.php b/src/app/convert/controllers/ConvertPdfController.php
index 85cc508bd28..9826317a56c 100755
--- a/src/app/convert/controllers/ConvertPdfController.php
+++ b/src/app/convert/controllers/ConvertPdfController.php
@@ -36,11 +36,24 @@ class ConvertPdfController
     {
         $tmpPath = CoreConfigModel::getTmpPath();
         $extension = pathinfo($aArgs['fullFilename'], PATHINFO_EXTENSION);
-        if (strtolower($extension) == 'html') {
+        if (strtolower($extension) == 'html' || strtolower($extension) == 'htm') {
             $pdfFilepath = str_replace('.'.$extension, '', $aArgs['fullFilename']) . '.pdf';
             $command = "wkhtmltopdf -B 10mm -L 10mm -R 10mm -T 10mm --load-error-handling ignore --load-media-error-handling ignore ".$aArgs['fullFilename']." ".$pdfFilepath;
-    
-            exec('export DISPLAY=:0 && '.$command.' 2>&1', $output, $return);
+
+            // Check if xvfb-run is installed, and run wkhtmltopdf with it
+            // Necessary when running in a headless debian server
+            // src : https://github.com/wkhtmltopdf/wkhtmltopdf/issues/2037
+            exec('whereis xvfb-run', $outputWhereIs, $returnWk);
+            $outputWhereIs = explode(':', $outputWhereIs[0]);
+            $xvfb = !empty($outputWhereIs[1]);
+
+            if ($xvfb) {
+                $command = 'xvfb-run ' . $command;
+            } else {
+                $command = 'export DISPLAY=:0 && ' . $command;
+            }
+
+            exec($command.' 2>&1', $output, $return);
         } elseif (strtolower($extension) != 'pdf') {
             $url = str_replace('rest/', '', UrlController::getCoreUrl());
             if (OnlyOfficeController::canConvert(['url' => $url, 'fullFilename' => $aArgs['fullFilename']])) {
-- 
GitLab