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/Ubuntubrew 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
content()
generator()
generator() :: :wkhtmltopdf | :chrome
generator() :: :wkhtmltopdf | :chrome
html()
html() :: binary()
html() :: binary()
html_path()
html_path() :: path()
html_path() :: path()
opts()
opts() :: keyword()
opts() :: keyword()
path()
path() :: binary()
path() :: binary()
pdf_file_path()
pdf_file_path() :: binary()
pdf_file_path() :: binary()
pdf_path()
pdf_path() :: path()
pdf_path() :: path()
reason()
url()
url() :: binary()
url() :: binary()
Link to this section Functions
defaults()
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.
encrypt_pdf(pdf_input_path, user_pw, owner_pw)
generate(content, opts \\ [])
generate(content(), opts()) :: {:ok, pdf_file_path()} | {:error, reason()}
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
– eitherchrome
orwkhtmltopdf
(default):prefer_system_executable
- set totrue
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" )generate!(html, options \\ [])
Same as generate but returns PDF file name only (raises on error).
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.
generate_binary!(html, options \\ [])
Same as generate_binary but returns PDF content directly or raises on error.
make_command(atom, options, content, arg)
make_dimensions(options)
make_file_paths(options)
maybe_encrypt_pdf(pdf_file, open_password, edit_password)
maybe_write_html(html, html_file_path)
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 nodenode
.{:failover, node}
- used if the application is distributed and is started on the current node because of a failover on nodenode
, 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
.