Repository: bitcrowd/chromic_pdf (elixir)

Issue:
Custom options to chromium
Hi again!

We need to pass a custom option to chromium on startup, in this case we need `"--font-render-hinting=none"`. I don't see an immediate way to do this.

See https://github.com/puppeteer/puppeteer/issues/2410 for why we need font-render-hinting=none. In any case, it would be useful to be able to pass custom options.

Pull request description:
[91] Custom options to chromium
Fixes #91.

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9fa9f29..001177b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,7 @@
 - Added option `wait_for` to wait for DOM element attribute to be set dynamically. (@jarimatti)
 - New global `timeout` option for session pool allows to configure timeout of print processes.
 - New global `ignore_certificate_errors` option allows to bypass SSL certificate verification.
+- New global `chrome_args` option allows to pass custom flags to chrome command.
 
 ## [0.6.2] - 2020-12-28
 
diff --git a/lib/chromic_pdf.ex b/lib/chromic_pdf.ex
index abd1ebb..70b4a58 100644
--- a/lib/chromic_pdf.ex
+++ b/lib/chromic_pdf.ex
@@ -179,7 +179,17 @@ defmodule ChromicPDF do
   the `prod` environment. However, obviously you won't be able to print PDFs in development or
   test then.
 
-  ## Debugging Chrome errors
+  ## Chrome Options
+
+  ### Custom command line switches
+
+  The `:chrome_args` option allows to pass arbitrary options to the Chrome/Chromium executable.
+
+      defp chromic_pdf_opts do
+        [chrome_args: "--font-render-hinting=none"]
+      end
+
+  ### Debugging Chrome errors
 
   Chrome's stderr logging is silently discarded to not obscure your logfiles. In case you would
   like to take a peek, add the `discard_stderr: false` option.
diff --git a/lib/chromic_pdf/pdf/chrome_impl.ex b/lib/chromic_pdf/pdf/chrome_impl.ex
index 66ae61f..0255494 100644
--- a/lib/chromic_pdf/pdf/chrome_impl.ex
+++ b/lib/chromic_pdf/pdf/chrome_impl.ex
@@ -25,6 +25,7 @@ defmodule ChromicPDF.ChromeImpl do
       "--headless --disable-gpu --remote-debugging-pipe"
     ]
     |> append_if("--no-sandbox", no_sandbox?(opts))
+    |> append_if(to_string(opts[:chrome_args]), !!opts[:chrome_args])
     |> append_if("2>/dev/null 3<&0 4>&1", discard_stderr?(opts))
     |> Enum.join(" ")
   end
diff --git a/lib/chromic_pdf/pdf/connection.ex b/lib/chromic_pdf/pdf/connection.ex
index e2a944b..22f4735 100644
--- a/lib/chromic_pdf/pdf/connection.ex
+++ b/lib/chromic_pdf/pdf/connection.ex
@@ -43,7 +43,7 @@ defmodule ChromicPDF.Connection do
 
   defp spawn_chrome(opts) do
     opts
-    |> Keyword.take([:discard_stderr, :no_sandbox])
+    |> Keyword.take([:chrome_args, :discard_stderr, :no_sandbox])
     |> @chrome.spawn()
   end
 
diff --git a/lib/chromic_pdf/supervisor.ex b/lib/chromic_pdf/supervisor.ex
index 5b12d46..89b8832 100644
--- a/lib/chromic_pdf/supervisor.ex
+++ b/lib/chromic_pdf/supervisor.ex
@@ -193,6 +193,7 @@ defmodule ChromicPDF.Supervisor do
               | {:session_pool, [session_pool_option()]}
               | {:no_sandbox, boolean()}
               | {:discard_stderr, boolean()}
+              | {:chrome_args, binary()}
               | {:ignore_certificate_errors, boolean()}
               | {:ghostscript_pool, [ghostscript_pool_option()]}
               | {:on_demand, boolean()}


Test patch:
diff --git a/test/unit/chromic_pdf/pdf/connection_test.exs b/test/unit/chromic_pdf/pdf/connection_test.exs
index 44b7782..391a609 100644
--- a/test/unit/chromic_pdf/pdf/connection_test.exs
+++ b/test/unit/chromic_pdf/pdf/connection_test.exs
@@ -24,7 +24,7 @@ defmodule ChromicPDF.ConnectionTest do
 
   describe "initialization" do
     test "it spawns Chrome and initializes its state" do
-      opts = [discard_stderr: false, no_sandbox: true]
+      opts = [discard_stderr: false, no_sandbox: true, chrome_args: "--foo"]
       expect(ChromeMock, :spawn, fn ^opts -> {:ok, @port} end)
       assert init({self(), opts}) == {:ok, new_state()}
     end