Skip to content

Commit

Permalink
set chrome-option --no-sandbox to allow execution as root (for docker)
Browse files Browse the repository at this point in the history
  • Loading branch information
gutschilla committed Apr 18, 2019
1 parent 3310ad5 commit d6ef825
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
11 changes: 7 additions & 4 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use Mix.Config

# config :pdf_generator,
# wkhtml_path: "/usr/bin/wkhtmltopdf",
# pdftk_path: "/usr/bin/pdftk",
# command_prefix: "/usr/bin/xvfb-run"
config :pdf_generator,
# wkhtml_path: "/usr/bin/wkhtmltopdf",
# pdftk_path: "/usr/bin/pdftk",
# command_prefix: "/usr/bin/xvfb-run",

# allow chrome to run as root
disable_chrome_sandbox: true

import_config "#{Mix.env}.exs"

22 changes: 15 additions & 7 deletions lib/pdf_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ defmodule PdfGenerator do
## Options
* `:generator` – either `chrome` or `wkhtmltopdf` (default)
* `:no_sandbox` – disable sandbox for chrome, required to run as root (read: _docker_)
* `:page_size` - output page size, defaults to "A4"
* `:open_password` - password required to open PDF. Will apply encryption to PDF
* `:edit_password` - password required to edit PDF
* `:shell_params` - list of command-line arguments to wkhtmltopdf
* `:shell_params` - list of command-line arguments to wkhtmltopdf or chrome
see http://wkhtmltopdf.org/usage/wkhtmltopdf.txt for all options
* `:delete_temporary` - true to remove the temporary html generated in
the system tmp dir
Expand Down Expand Up @@ -171,6 +173,7 @@ defmodule PdfGenerator do
def make_command(:chrome, options, content, {html_path, pdf_path}) do
executable_on_path = System.find_executable("chrome-headless-render-pdf")
nodejs_on_path = System.find_executable("nodejs")
disable_sandbox = Application.get_env(:pdf_generator, :disable_chrome_sandbox) || options[:no_sandbox]
js_file = Application.app_dir(:pdf_generator) <> "/../../../../node_modules/chrome-headless-render-pdf/dist/cli/chrome-headless-render-pdf.js"

{executable, executable_args} =
Expand All @@ -187,12 +190,17 @@ defmodule PdfGenerator do
{:url, url} -> url
_html -> "file://" <> html_path
end
arguments = executable_args ++ [
"--url", source,
"--pdf", pdf_path,
"--paper-width", width,
"--paper-height", height,
] ++ more_params
arguments = List.flatten([
executable_args,
[
"--url", source,
"--pdf", pdf_path,
"--paper-width", width,
"--paper-height", height,
],
more_params,
if(disable_sandbox, do: ["--chrome-option", "--no-sandbox"], else: [])
])
{executable, arguments} # |> IO.inspect()
end

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ defmodule PdfGenerator.Mixfile do
defp deps do
[
# communication with external programs
{:ex_doc, "~> 0.16", only: :dev, runtime: false}
{:ex_doc, "~> 0.19", only: :dev, runtime: false}
]
end
end
7 changes: 5 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
%{
"earmark": {:hex, :earmark, "1.2.3", "206eb2e2ac1a794aa5256f3982de7a76bf4579ff91cb28d0e17ea2c9491e46a4", [:mix], []},
"ex_doc": {:hex, :ex_doc, "0.16.2", "3b3e210ebcd85a7c76b4e73f85c5640c011d2a0b2f06dcdf5acdb2ae904e5084", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, optional: false]}]},
"earmark": {:hex, :earmark, "1.3.2", "b840562ea3d67795ffbb5bd88940b1bed0ed9fa32834915125ea7d02e35888a5", [:mix], [], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.20.2", "1bd0dfb0304bade58beb77f20f21ee3558cc3c753743ae0ddbb0fd7ba2912331", [:mix], [{:earmark, "~> 1.3", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.10", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"},
"makeup": {:hex, :makeup, "0.8.0", "9cf32aea71c7fe0a4b2e9246c2c4978f9070257e5c9ce6d4a28ec450a839b55f", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
"makeup_elixir": {:hex, :makeup_elixir, "0.13.0", "be7a477997dcac2e48a9d695ec730b2d22418292675c75aa2d34ba0909dcdeda", [:mix], [{:makeup, "~> 0.8", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"},
"nimble_parsec": {:hex, :nimble_parsec, "0.5.0", "90e2eca3d0266e5c53f8fbe0079694740b9c91b6747f2b7e3c5d21966bba8300", [:mix], [], "hexpm"},
"porcelain": {:hex, :porcelain, "2.0.3", "2d77b17d1f21fed875b8c5ecba72a01533db2013bd2e5e62c6d286c029150fdc", [:mix], []},
}
8 changes: 7 additions & 1 deletion test/pdf_generator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule PdfGeneratorTest do
assert {:ok, "%PDF-1" <> _pdf} = @html |> PdfGenerator.generate_binary
end

test "chrome-headless" do
test "chrome-headless from file" do
{:ok, temp_filename } = PdfGenerator.generate(@html, generator: :chrome, page_size: "A5")

# File should exist and has some size
Expand All @@ -43,6 +43,12 @@ defmodule PdfGeneratorTest do
assert String.slice( pdf, 0, 6) == "%PDF-1"
end

test "chrome-headless from URL (assuming google.com is up and running)" do
{status, result} = PdfGenerator.generate({:url, "http://google.com"}, generator: :chrome)
assert status == :ok
assert result |> File.read! |> String.slice(0, 6) == "%PDF-1"
end

test "generate! returns a filename" do
@html
|> PdfGenerator.generate!
Expand Down

0 comments on commit d6ef825

Please sign in to comment.