PDF Generator v0.6.2 PdfGenerator

PdfGenerator

Provides a simple wrapper around wkhtmltopdf and pdftk to generate possibly encrypted PDFs from an HTML source.

Configuration (optional)

if no or partial configuration is given, PdfGenerator will search for executables on path. This will rais an error when wkhtmltopdf cannot be found.

config :pdf_generator,
      wkhtml_path: "/path/to/wkhtmltopdf",
      pdftk_path:  "/path/to/pdftk",

In your config/config.exs. Add :pdf_generator to your mix.exs: Note that this is optional but advised to as it will perform a check on startup whether it can find a suitable wkhtmltopdf executable. It's generally better to have an app fail at startup than at later runtime.

def application do

[applications: [ .., :pdf_generator, ..], .. ]

end

If you don't want to autostart, issue

PdfGenerator.start wkhtml_path: "/path/to/wkhtml_path"

System requirements

  • wkhtmltopdf or chrome-headless
  • pdftk (optional, for encrypted PDFs)

Precompiled wkhtmltopdf binaries can be obtained here: http://wkhtmltopdf.org/downloads.html

pdftk should be available as package on your system via

  • apt-get install pdftk on Debian/Ubuntu
  • brew pdftk on OSX (you'll need homebrew, of course)
  • Install the Exe-Installer on Windows found the project's homepage (link above)

Link to this section Summary

Functions

Returns {width, height} tuple for page sizes either as given or for A4 and A5. Defaults to A4 sizes. In inches. Because chrome wants imperial.

Generates a pdf file from given html string. Returns a string containing a temporary file path for that PDF.

Same as generate but returns PDF file name only (raises on error).

Takes same options as generate but will return an {:ok, binary_pdf_content} tuple.

Same as generate_binary but returns PDF content directly or raises on error.

Called when an application is started.

Link to this section Types

Link to this type

content()
content() :: html() | {:ok, url()}

Link to this type

generator()
generator() :: :wkhtmltopdf | :chrome

Link to this type

html()
html() :: binary()

Link to this type

html_path()
html_path() :: path()

Link to this type

opts()
opts() :: keyword()

Link to this type

path()
path() :: binary()

Link to this type

pdf_file_path()
pdf_file_path() :: binary()

Link to this type

pdf_path()
pdf_path() :: path()

Link to this type

reason()
reason() :: atom() | {atom(), any()}

Link to this type

url()
url() :: binary()

Link to this section Functions

Link to this function

dimensions_for(map)

Returns {width, height} tuple for page sizes either as given or for A4 and A5. Defaults to A4 sizes. In inches. Because chrome wants imperial.

Link to this function

encrypt_pdf(pdf_input_path, user_pw, owner_pw)

Link to this function

generate(content, opts \\ [])
generate(content(), opts()) :: {:ok, pdf_file_path()} | {:error, reason()}

Generates a pdf file from given html string. Returns a string containing a temporary file path for that PDF.

Options

  • :generator – either 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"
  • :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 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
  • :filename - filename you want for the output PDF (provide without .pdf extension), defaults to a random string

Examples

pdf_path_1 = PdfGenerator.generate "

Boom

" pdf_path_2 = PdfGenerator.generate( "

Boom

", page_size: "letter", open_password: "secret", edit_password: "g3h31m", shell_params: [ "--outline", "--outline-depth3", "3" ], delete_temporary: true, filename: "my_awesome_pdf" )

Link to this function

generate!(html, options \\ [])

Same as generate but returns PDF file name only (raises on error).

Link to this function

generate_binary(html, options \\ [])

Takes same options as generate but will return an {:ok, binary_pdf_content} tuple.

In case option delete_temporary is true, will as well delete the temporary pdf file.

Link to this function

generate_binary!(html, options \\ [])

Same as generate_binary but returns PDF content directly or raises on error.

Link to this function

make_command(atom, options, content, arg)
make_command(generator(), opts(), content(), {html_path(), pdf_path()}) ::
  {path(), list()}

Link to this function

make_dimensions(options)

Link to this function

make_file_paths(options)
make_file_paths(keyword()) :: {html_path(), pdf_path()}

Link to this function

maybe_encrypt_pdf(pdf_file, open_password, edit_password)

Link to this function

maybe_write_html(html, html_file_path)
maybe_write_html(content(), path()) :: :ok | {:error, reason()}

Link to this function

start(type, args)

Called when an application is started.

This function is called when an application is started using Application.start/2 (and functions on top of that, such as Application.ensure_started/2). This function should start the top-level process of the application (which should be the top supervisor of the application's supervision tree if the application follows the OTP design principles around supervision).

start_type defines how the application is started:

  • :normal - used if the startup is a normal startup or if the application is distributed and is started on the current node because of a failover from another node and the application specification key :start_phases is :undefined.
  • {:takeover, node} - used if the application is distributed and is started on the current node because of a failover on the node node.
  • {:failover, node} - used if the application is distributed and is started on the current node because of a failover on node node, and the application specification key :start_phases is not :undefined.

start_args are the arguments passed to the application in the :mod specification key (for example, mod: {MyApp, [:my_args]}).

This function should either return {:ok, pid} or {:ok, pid, state} if startup is successful. pid should be the PID of the top supervisor. state can be an arbitrary term, and if omitted will default to []; if the application is later stopped, state is passed to the stop/1 callback (see the documentation for the c:stop/1 callback for more information).

use Application provides no default implementation for the start/2 callback.

Callback implementation for Application.start/2.