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

added weasyprint as another pdf generator #96

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ Or, run `cd priv && npm install`
For other distributions, refer to http://wkhtmltopdf.org/downloads.html – For
example, replace `bionic` with `xenial` if you're on Ubuntu 16.04.

### weasyprint

for installation instructions visit https://doc.courtbouillon.org/weasyprint/stable/first_steps.html

## Optional Dependencies

3. _optional:_ Install `xvfb` (shouldn't be required with the binary mentioned above):
Expand Down
23 changes: 20 additions & 3 deletions lib/pdf_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ defmodule PdfGenerator do

## Options

* `:generator` – either `chrome` or `wkhtmltopdf` (default)
* `:generator` – either `weasyprint`, `chrome` or `wkhtmltopdf` (default)

* `:prefer_system_executable` - set to `true` if you installed
chrome-headless-render-pdf globally

* `:no_sandbox` – disable sandbox for chrome, required to run as root (read: _docker_)

* `:page_size` - output page size, defaults to "A4", other options are
"letter" (US letter) and "A5"
"letter" (US letter) and "A5" (does not work for weasyprint, set page sizes with @page property in the html)

* `:open_password` - password required to open PDF. Will apply encryption to PDF

Expand Down Expand Up @@ -132,7 +132,7 @@ defmodule PdfGenerator do
@type path :: binary()
@type html_path :: path
@type pdf_path :: path
@type generator :: :wkhtmltopdf | :chrome
@type generator :: :wkhtmltopdf | :chrome | :weasyprint

@spec generate(content, opts) :: {:ok, pdf_file_path} | {:error, reason}
def generate(content, opts \\ []) do
Expand Down Expand Up @@ -256,6 +256,22 @@ defmodule PdfGenerator do
{executable, arguments}
end

def make_command(:weasyprint, options, content, {html_path, pdf_path}) do
executable = PdfGenerator.PathAgent.get.weasyprint_path
source =
case content do
{:url, url} -> url
_html -> html_path
end
shell_params = options[:shell_params] || []
arguments = List.flatten([
source, pdf_path,
shell_params
])
{executable, arguments} |> inspect() |> Logger.debug()
{executable, arguments}
end

defp maybe_delete_temp(true, file), do: File.rm(file)
defp maybe_delete_temp(_falsy, _file), do: :ok

Expand All @@ -271,6 +287,7 @@ defmodule PdfGenerator do
defp result_ok(:chrome, _string, 0), do: true
defp result_ok(:chrome, _string, _exit_code), do: false
defp result_ok(:wkhtmltopdf, string, _exit_code), do: String.match?(string, ~r/Done/ms)
defp result_ok(:weasyprint, _string, 0), do: true

defp get_command_prefix(options) do
options[:command_prefix] || Application.get_env(:pdf_generator, :command_prefix)
Expand Down
2 changes: 2 additions & 0 deletions lib/pdf_generator_path_agent.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule PdfGenerator.PathAgent do
pdftk_path: nil,
chrome_path: nil,
node_path: nil,
weasyprint_path: nil
]

@moduledoc """
Expand All @@ -30,6 +31,7 @@ defmodule PdfGenerator.PathAgent do
pdftk_path: System.find_executable("pdftk"),
chrome_path: System.find_executable("chrome-headless-render-pdf"),
node_path: System.find_executable("nodejs") || System.find_executable("node"),
weasyprint_path: System.find_executable("weasyprint"),
]
++ paths_from_options
|> Enum.dedup()
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defmodule PdfGenerator.Mixfile do
defp package do
[
description:
"A wrapper for wkhtmltopdf and chrome-headless (puppeteer) with optional " <>
"A wrapper for wkhtmltopdf, weasyprint and chrome-headless (puppeteer) with optional " <>
"support for encryption via pdftk.",
files: ["lib", "mix.exs", "README.md", "LICENSE", "test", "priv"],
maintainers: ["Martin Gutsch"],
Expand Down