Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to remove default args via config #314

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Unreleased

### Added

- Extend `chrome_args` to allow removal of conflicting default args. Allows for use cases which were previously blocked by defaults such as rendering WebGL images. See #314 (@walter)

## [1.16.0] - 2024-06-25

### Changed
Expand Down
20 changes: 20 additions & 0 deletions lib/chromic_pdf.ex
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,26 @@ defmodule ChromicPDF do
[chrome_args: "--font-render-hinting=none"]
end

In some cases, chromic_pdf's default args may conflict with the ones you would like to add.

E.g. `--disable-gpu`

The problem is that you can't simply override them like so:

E.g. in default args within chromic_pdf's code, `--disable-gpu` and then
passing `chrome_args: "--enable-gpu"` won't work.

In this case, use Keyword form of the `:chrome_args` option which
allows targeted removing of problematic default args so that they don't
disrupt your chrome configuration.

defp chromic_pdf_opts do
[chrome_args: [append: "--headless=new --angle=swiftshader",
remove: ["--headless", "--disable-gpu"]]]
end

_Note: `:append` expects a string whereas `:remove` expects a list._
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's be liberal in what the API can accept; a List.wrap/1 somewhere will allow you to accept strings or lists for either of the options, and then we don't need this note in the docs.

Suggested change
_Note: `:append` expects a string whereas `:remove` expects a list._


The `:chrome_executable` option allows to specify a custom Chrome/Chromium executable.

defp chromic_pdf_opts do
Expand Down
24 changes: 23 additions & 1 deletion lib/chromic_pdf/pdf/chrome_runner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,37 @@ defmodule ChromicPDF.ChromeRunner do
# https://github.com/bitcrowd/chromic_pdf/issues/76
defp args(extra, opts) do
default_args()
|> remove_conflicting_args(opts[:chrome_args])
|> append_if("--no-sandbox", no_sandbox?(opts))
|> append_if(to_string(opts[:chrome_args]), !!opts[:chrome_args])
|> append_chrome_args_if(opts[:chrome_args])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the implementation could use another round of polishing :)

Suggested change
|> append_chrome_args_if(opts[:chrome_args])
|> handle_chrome_args(opts[:chrome_args])

Maybe go for a unified handler function. 3 Clauses, nil if option not set, then a is_binary clause for the original behaviour, and a new is_list clause for handling the extended args. Extracting options from this list should then be something like:

remove = List.wrap(Keyword.get(extended, :remove, []))

|> Kernel.++(List.wrap(extra))
|> append_if("2>/dev/null 3<&0 4>&1", discard_stderr?(opts))
end

defp append_if(list, _value, false), do: list
defp append_if(list, value, true), do: list ++ [value]

defp append_chrome_args_if(list, ""), do: list

defp append_chrome_args_if(list, chrome_args) when is_binary(chrome_args) do
append_if(list, chrome_args, true)
end

defp append_chrome_args_if(list, [{_, _} | _] = chrome_args) do
append = Keyword.get(chrome_args, :append)

append_if(list, append, !!append)
end

defp append_chrome_args_if(list, _), do: list

defp no_sandbox?(opts), do: Keyword.get(opts, :no_sandbox, false)
defp discard_stderr?(opts), do: Keyword.get(opts, :discard_stderr, true)

defp remove_conflicting_args(defaults, [{_, _} | _] = chrome_args) do
conflicting = Keyword.get(chrome_args, :remove, [])
Enum.reject(defaults, &Enum.member?(conflicting, &1))
end

defp remove_conflicting_args(defaults, _), do: defaults
end
2 changes: 1 addition & 1 deletion lib/chromic_pdf/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ defmodule ChromicPDF.Supervisor do
@type local_chrome_option ::
{:no_sandbox, boolean()}
| {:discard_stderr, boolean()}
| {:chrome_args, binary()}
| {:chrome_args, binary() | map()}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# above
@type extended_chrome_args :: [{:append, binary() | [binary()]} | {:remove, binary() | [binary()]}]
Suggested change
| {:chrome_args, binary() | map()}
| {:chrome_args, binary() | extended_chrome_args()}

| {:chrome_executable, binary()}

@typedoc """
Expand Down