Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update pdf_generator.ex #8

Merged
merged 3 commits into from
Aug 10, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions lib/pdf_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,30 @@ defmodule PdfGenerator do

Provides a simple wrapper around [wkhtmltopdf](http://wkhtmltopdf.org) and
[pdftk](https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/) to generate
possibly encrypted PDFs from an HTML source.
possibly encrypted PDFs from an HTML source.

# Configuration (optional)

if no or partial configuration is given, PdfGenerator will search for
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
Expand All @@ -43,7 +43,7 @@ defmodule PdfGenerator do
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
Expand Down Expand Up @@ -81,7 +81,7 @@ defmodule PdfGenerator do
# requires: Porcelain, Misc.Random
@doc """
Generates a pdf file from given html string. Returns a string containing a
temporary file path for that PDF.
temporary file path for that PDF.

options:

Expand All @@ -90,12 +90,14 @@ defmodule PdfGenerator do
- 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: :html to remove the temporary html generated in
the system tmp dir

# Examples

pdf_path_1 = PdfGenerator.generate "<html><body><h1>Boom</h1></body></html>"
pdf_path_2 = PdfGenerator.generate(
"<html><body><h1>Boom</h1></body></html>",
"<html><body><h1>Boom</h1></body></html>",
page_size: "A5",
open_password: "secret",
edit_password: "g3h31m",
Expand Down Expand Up @@ -133,6 +135,10 @@ defmodule PdfGenerator do
executable, arguments, [in: "", out: :string, err: :string]
)

if Keyword.get(options, :delete_temporary) == :html do
File.rm html_file
end

case status do
0 ->
case Keyword.get options, :open_password do
Expand All @@ -156,12 +162,12 @@ defmodule PdfGenerator do
pdf_output_file = Path.join System.tmp_dir, Misc.Random.string <> ".pdf"

%Result{ out: _output, status: status } = Porcelain.exec(
pdftk_path, [
pdf_input_path,
"output", pdf_output_file,
pdftk_path, [
pdf_input_path,
"output", pdf_output_file,
"owner_pw", owner_pw,
"user_pw", user_pw,
"encrypt_128bit",
"encrypt_128bit",
"allow", "Printing", "CopyContents"
]
)
Expand Down