From 50443d7a17b9fcc6b31117f9624eb087c21451cd Mon Sep 17 00:00:00 2001
From: Florian Schmitt <mrflos@gmail.com>
Date: Tue, 20 Apr 2021 09:25:37 +0300
Subject: [PATCH] feat: add default config values for HeadlessChromium, and a
 very high value for page loaded timeout

---
 config.yaml           | 25 +++++++++++++++++--------
 handlers/page/pdf.php |  2 +-
 2 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/config.yaml b/config.yaml
index 56a0e64..2358ff8 100644
--- a/config.yaml
+++ b/config.yaml
@@ -1,23 +1,36 @@
 parameters:
-  htmltopdf_path: '/usr/bin/chromium'  # MacOs: /Applications/Chromium.app/Contents/MacOS/Chromium
+  htmltopdf_path: '/usr/bin/chromium'   # MacOs: /Applications/Chromium.app/Contents/MacOS/Chromium
   htmltopdf_service_url: ''             # Should be a complete url like https://example.org/yeswiki/?PagePrincipale/pdf
-  # Advanced
+  # duration before considering the pageload completed (60000ms = 1 minute)
+  page_load_timeout: 60000
+  # for edit config action
+  publication_editable_config_params:
+    - 'htmltopdf_service_url'
+
+  # Advanced Configuration
   # Has to comply with HeadlessChromium::createBrowser() options
-  # see https://github.com/chrome-php/headless-chromium-php#options
+  # See https://github.com/chrome-php/headless-chromium-php#options
   htmltopdf_options:
     windowSize: [1440, 780]
     noSandbox: true
     headless: true
     userAgent: 'YesWiki/4.0'
+    # maximum time to wait for the browser to start, before navigating to a page
+    startupTimeout: 30
+    # maximum time in ms to wait for synchronous messages to send to the browser
+    sendSyncDefaultTimeout: 10000
     customFlags:
     - '--crash-dumps-dir=/tmp'
+    # uncomment to view browser logs in PHP/Apache logs
     # debugLogger: 'php://stdout'
+
+  # Advanced Configuration
   # if you want to propose this website as a service for other domains
   htmltopdf_service_authorized_domains:
   #  - 'example.org'
   #  - 'another-one.com'
 
-  # ADVANCED SETUP
+  # Advanced Configuration
   # Configure this when the HTTP server does not have access to the YesWiki via its public URL.
   #
   # Eg, if Docker/base_url is https://example.com/? and Apache runs as http://localhost:8000,
@@ -25,7 +38,3 @@ parameters:
   # Use cases: reverse-proxy, within a YesWiki Docker container.
   htmltopdf_base_url: ~
   #htmltopdf_base_url: http://localhost:8000/?
-  #
-  # for edit config action
-  publication_editable_config_params:
-    - 'htmltopdf_service_url'
diff --git a/handlers/page/pdf.php b/handlers/page/pdf.php
index 5ef8b3a..54a2b10 100644
--- a/handlers/page/pdf.php
+++ b/handlers/page/pdf.php
@@ -93,7 +93,7 @@
             $page = $browser->createPage();
             $page->navigate($sourceUrl)->waitForNavigation(HeadlessChromium\Page::NETWORK_IDLE);
 
-            $value = $page->evaluate('__is_yw_publication_ready()')->getReturnValue(20000);
+            $value = $page->evaluate('__is_yw_publication_ready()')->getReturnValue($this->config['page_load_timeout']);
 
             // now generate PDF
             $page->pdf(array(