Skip to content

Commit

Permalink
Merge branch 'master' of github.com:gutschilla/elixir-pdf-generator
Browse files Browse the repository at this point in the history
  • Loading branch information
gutschilla committed Aug 25, 2016
2 parents 9ee1de8 + ee48f8a commit 3d126ee
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 44 deletions.
14 changes: 11 additions & 3 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
# Changes

- 0.3.4
- BUGFIX: fix merge confusion to **realy** support `xvfb-run` or other
command prefixes to wkhtmltopdf
- support explicit deletion of temporary files thanks to
[Edipo Vinicius da Silva](https://github.com/edipox)
- Improve README
- 0.3.3
-BUGFIX: typo in config/prod.exs
- 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)
- 0.3.1
- implement this as proper application, look for executables at startup (and possibly fail on that)
- implement this as proper application, look for executables at startup (and
possibly fail on that)
- save paths in a PfdGenerator.Agent
- make paths configurable in `config/ENV.exs` as well
- add some tests (Yay!)
- better README- 0.3.0

- 0.2.0
- adding support for PDFTK to create encrypted PDFs
- **API-CHANGE** PdfGenerator.generate now returns tuple `{ :ok, file_name }` instead of just `file_name`
- **API-CHANGE** PdfGenerator.generate now returns tuple `{:ok, file_name}`
instead of just `file_name`
- Adding some docs, issue `h PdfGenerator` in your iex shell for more info

78 changes: 52 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,37 @@

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
# New in 0.3.4

- 0.3.3
-BUGFIX: typo in config/prod.exs
- 0.3.2
- support for command prefixes, most notably **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)
- 0.3.4
- BUGFIX: fix merge confusion to **realy** support `xvfb-run` or other
command prefixes to wkhtmltopdf
- support explicit deletion of temporary files thanks to
[Edipo Vinicius da Silva](https://github.com/edipox)
- Improve README

For a proper changelog, see [CHANGES](CHANGES.md)

# Usage
# System prerequisites

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

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 (on Debian/Ubuntu: `sudo apt-get
install xvfb`).

_(optional)_ To use wkhtmltopdf on systems without an X window server installed, please install `xvfb-run` from your repository or via `homebrew` (Mac)
On current (2016) Macintosh computers `/usr/X11/bin/xvfb` should be available
and is reported to do the same thing. _warning:_ This is untested. PLS report to
me if you ran this successfully on a Mac.

_(optional)_ For best results, download goon and place it in your $PATH. Current binaries can be found here:
https://github.com/alco/goon/releases
_(optional)_ For best results, download goon and place it in your $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
_(optional)_ Install pdftk (optional) via your package manager or homebrew. The
project page also contains a Windows installer

# Usage

Add this to your dependencies in your mix.exs:

Expand Down Expand Up @@ -54,26 +61,45 @@ html = "<html><body><p>Hi there!</p></body></html>"
{ :ok, pdf_content } = File.read file_name
```

# Configuration
# Options and 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/config.exs`.

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

## Running headless (server-mode)

If you happen to want to run an wkhtmltopdf with an unpatched version of webkit
that requires an X Window server - but on your server (or Mac) ain't one, you
might find a `command_prefix` handy:

```
PdfGenerator.generate "<html..", command_prefix: "xvfb-run"
```

This can also be configured globally in cour `config/config.exs`:

```
config :pdf_generator,
command_prefix: "/usr/bin/xvfb-run"
```

## More options

- `page_size`: defaults to `A4`, see wkhtmltopdf for more options
- `open_password`: requires `pdftk`, password to encrypt PDFs with
- `edit_password`: requires `pdftk`, sets password for edit permissions on PDF
- `shell_params`: pass custom parameters to wkhtmltopdf. **CAUTION: BEWARE OF SHELL INJECTIONS!**
- `command_prefix`: prefix wkhtmltopdf with some command (e.g. `xvfb-run`, `sudo` ..)
- `delete_temporary`: immediately remove temp files after generation

# Documentation

For more info, read the [docs on hex](http://hexdocs.pm/pdf_generator) 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
19 changes: 14 additions & 5 deletions lib/pdf_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ defmodule PdfGenerator do
# worker(TestApp.Worker, [arg1, arg2, arg3])
worker(
PdfGenerator.PathAgent, [[
wkhtml_path: Application.get_env(:pdf_generator, :wkhtml_path ),
pdftk_path: Application.get_env(:pdf_generator, :pdftk_path ),
wkhtml_path: Application.get_env(:pdf_generator, :wkhtml_path),
pdftk_path: Application.get_env(:pdf_generator, :pdftk_path),
]]
)
]
Expand Down Expand Up @@ -121,7 +121,7 @@ defmodule PdfGenerator do

executable = wkhtml_path
arguments = List.flatten( [ shell_params, html_file, pdf_file ] )
command_prefix = Keyword.get( options, :command_prefix ) || PdfGenerator.PathAgent.get |> Map.get( :command_prefix )
command_prefix = Keyword.get( options, :command_prefix ) || Application.get_env( :pdf_generator, :command_prefix )

# allow for xvfb-run wkhtmltopdf arg1 arg2
# or sudo wkhtmltopdf ...
Expand Down Expand Up @@ -156,8 +156,17 @@ defmodule PdfGenerator do
def encrypt_pdf( pdf_input_path, user_pw, owner_pw ) do
pdftk_path = PdfGenerator.PathAgent.get.pdftk_path

if owner_pw == nil, do: owner_pw = Misc.Random.string(16)
if user_pw == nil, do: user_pw = Misc.Random.string(16)
owner_pw =
case owner_pw do
nil -> Misc.Random.string(16)
_ -> owner_pw
end

user_pw =
case user_pw do
nil -> Misc.Random.string(16)
_ -> user_pw
end

pdf_output_file = Path.join System.tmp_dir, Misc.Random.string <> ".pdf"

Expand Down
16 changes: 7 additions & 9 deletions lib/pdf_generator_path_agent.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,25 @@ defmodule PdfGenerator.PathAgent do
defstruct wkhtml_path: nil, pdftk_path: nil

@moduledoc """
Will check system requirements on startup and keep
Will check system requirements on startup and keep
a path map as state in an Agent process.
"""

@name __MODULE__

def start_link( path_options ) do
Agent.start_link( __MODULE__, :init_opts, [ path_options ], name: @name )
end

def init_opts( paths_from_options ) do

# options override system default paths
options =
options =
[
wkhtml_path: System.find_executable( "wkhtmltopdf" ),
pdftk_path: System.find_executable( "pdftk" ),
# command_prefix: System.find_executable( "xvfb-run" )
]
pdftk_path: System.find_executable( "pdftk" )
]
++ paths_from_options
|> Enum.filter( fn { _, v } -> v != nil end )
|> Enum.filter( fn { _, v } -> v != nil end )

# at least, wkhtmltopdf executable sould be there
if Keyword.fetch!( options, :wkhtml_path ) == nil do
Expand All @@ -44,5 +42,5 @@ defmodule PdfGenerator.PathAgent do
def get do
Agent.get( @name, fn( data ) -> data end )
end

end
2 changes: 1 addition & 1 deletion test/pdf_generator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule PdfGeneratorTest do
end

test "command prefix with noop env" do
{:ok, temp_filename } = PdfGenerator.generate @html, [ command_prefix: "env" ]
{:ok, _temp_filename } = PdfGenerator.generate @html, [ command_prefix: "env" ]
end

test "generate_binary! reads file" do
Expand Down

0 comments on commit 3d126ee

Please sign in to comment.