PDF Generator v0.4.0 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
  • pdftk (optional, for encrypted PDFs)
  • goon (optional, for Porcelain shalle wrapper)

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)

goon is available here: https://github.com/alco/goon/releases

Link to this section Summary

Functions

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 Functions

Link to this function encrypt_pdf(pdf_input_path, user_pw, owner_pw)

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

Options

  • :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 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: “A5”, 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)
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 get_command_prefix(options)
Link to this function make_command_tuple(command_prefix, wkhtml_executable, arguments)
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 (e.g., 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.