Skip to content

A simple wrapper for wkhtmltopdf (HTML to PDF) for use in Elixir projects.

License

Notifications You must be signed in to change notification settings

jaylib/elixir-pdf-generator

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

elixir-pdf-generator

A wrapper for wkhtmltopdf (HTML to PDF) and PDFTK (adds in encryption) for use in Elixir projects. If available, it will use xvfb-run (x virtual frame buffer) to use wkhtmltopdf on systems that have no X installed, e.g. a server.

New in 0.3.2 and 0.3.3

  • 0.3.3 -BUGFIX: typo in config/prod.exs
  • 0.3.2
    • support for command prefixes, most notabably xvfb-run to let a wkhtmltopdf which was compiled without an unpatched version of qt run on machines without an x server
    • (add in precompiled, patched binaries for wkhtmltopdf and libjpeg8 that are needed to run wkhtmltopdf without xvfb-run)

For a proper changelog, see CHANGES

Usage

Download wkhtmltopdf and place it in your $PATH. Current binaries can be found here: http://wkhtmltopdf.org/downloads.html

(optional) To use wkhtmltopdf on systems without an X window server installed, please install xvfb-run from your repository or via homebrew (Mac)

(optional) For best results, download goon and place it yout $PATH. Current binaries can be found here: https://github.com/alco/goon/releases

(optional) Install pdftk (optional) via your package manager or homebrew. The project page also contains a Windows installer

Add this to your dependencies in your mix.exs:

def application do
    [applications: [
        :logger, 
        :pdf_generator # <-- add this
    ]]
end

defp deps do
    [
        # ... whatever else
        { :pdf_generator, ">=0.3.0" }, # <-- and this
    ]
end

Then pass some html to PdfGenerator.generate

$ iex -S mix

html = "<html><body><p>Hi there!</p></body></html>"
# be aware, this may take a while...
{ :ok, file_name } = PdfGenerator.generate html, page_size: "A5", open_password: "s3cr3t" 
{ :ok, pdf_content } = File.read file_name 

Configuration

This module will automatically try to finde both wkhtmltopdf and pdftk in your path. But you may override or explicitly set their paths in your config/config.exs:

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

Documentation

For more info, read the docs on hex or issue h PdfGenerator in your iex shell.

TODO

  • Pass some useful base path so wkhtmltopdf can resolve static files (styles, images etc) linked in the HTML

About

A simple wrapper for wkhtmltopdf (HTML to PDF) for use in Elixir projects.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Elixir 100.0%