Skip to content

Commit

Permalink
Merge pull request gutschilla#23 from praveenperera/master
Browse files Browse the repository at this point in the history
Add ability to set output PDF filename
  • Loading branch information
gutschilla authored Aug 9, 2017
2 parents 0dbab16 + a09962e commit 86a8b08
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ to use wkhtmltopdf on systems that have no X installed, e.g. a server.

- master
- improve REAMDE
- add option to pick output pdf filename
- 0.3.5
- add `generate_binray` and `generate_binary!` that immediately return the
PDF binary instead of an `{:ok, filename}` tuple.
Expand Down Expand Up @@ -120,6 +121,8 @@ config :pdf_generator,
- `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
- `filename` - filename you want for the output PDF (provide without .pdf extension),
defaults to a random string

## Heroku Setup

Expand Down
27 changes: 17 additions & 10 deletions lib/pdf_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,22 @@ defmodule PdfGenerator do

# return file name of generated pdf
# requires: Porcelain, Misc.Random

@doc """
Generates a pdf file from given html string. Returns a string containing a
temporary file path for that PDF.
options:
## 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
* `: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
* `: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
Expand All @@ -102,7 +105,8 @@ defmodule PdfGenerator do
open_password: "secret",
edit_password: "g3h31m",
shell_params: [ "--outline", "--outline-depth3", "3" ],
delete_temporary: true
delete_temporary: true,
filename: "my_awesome_pdf"
)
"""
def generate( html ) do
Expand All @@ -111,9 +115,9 @@ defmodule PdfGenerator do

def generate( html, options ) do
wkhtml_path = PdfGenerator.PathAgent.get.wkhtml_path
random_filebase = Path.join System.tmp_dir, Misc.Random.string
html_file = random_filebase <> ".html"
pdf_file = random_filebase <> ".pdf"
filebase = generate_filebase(options[:filename])
html_file = filebase <> ".html"
pdf_file = filebase <> ".pdf"
File.write html_file, html

shell_params = [
Expand Down Expand Up @@ -153,6 +157,9 @@ defmodule PdfGenerator do
end
end

defp generate_filebase(nil), do: generate_filebase(Misc.Random.string)
defp generate_filebase(filename), do: Path.join(System.tmp_dir, filename)

def encrypt_pdf( pdf_input_path, user_pw, owner_pw ) do
pdftk_path = PdfGenerator.PathAgent.get.pdftk_path
pdf_output_file = Path.join System.tmp_dir, Misc.Random.string <> ".pdf"
Expand Down
11 changes: 6 additions & 5 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
%{"earmark": {:hex, :earmark, "0.1.19"},
"ex_doc": {:hex, :ex_doc, "0.11.2"},
"misc_random": {:hex, :misc_random, "0.2.6"},
"porcelain": {:hex, :porcelain, "2.0.1"},
"random": {:git, "https://github.com/gutschilla/elixir-helper-random.git", "fb801765651e8e6fa02866803592eade96b6c4d7", []}}
%{"earmark": {:hex, :earmark, "0.1.19", "ffec54f520a11b711532c23d8a52b75a74c09697062d10613fa2dbdf8a9db36e", [:mix], []},
"ex_doc": {:hex, :ex_doc, "0.11.2", "8ac82c6144a27faca6a623eeebfbf5a791bc20a54ce29a9c02e499536d253d9b", [:mix], [{:earmark, "~> 0.1.17 or ~> 0.2", [hex: :earmark, optional: true]}]},
"misc_random": {:hex, :misc_random, "0.2.6", "959981142c96005731ed8b7df2440f5563a31c3a159edd5769fe28c77e3e0af7", [:mix], []},
"porcelain": {:hex, :porcelain, "2.0.1", "9c3db2b47d8cf6879c0d9ac79db8657333974a88faff09e856569e00c1b5e119", [:mix], []},
"random": {:git, "https://github.com/gutschilla/elixir-helper-random.git", "fb801765651e8e6fa02866803592eade96b6c4d7", []},
"zarex": {:hex, :zarex, "0.3.0", "e19145de2127671f3d3f4bdb6bb60a0d70830836a01a79ba63428a5bbe3a16e0", [:mix], []}}
6 changes: 6 additions & 0 deletions test/pdf_generator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ defmodule PdfGeneratorTest do
|> assert
end

test "generate! with filename option returns custom filename" do
filename = PdfGenerator.generate!(@html, filename: "custom_file_name")
assert File.exists?(filename)
assert Path.basename(filename, ".pdf") == "custom_file_name"
end

test "generate_binary! reads file" do
assert "%PDF-1" <> _pdf = @html |> PdfGenerator.generate_binary!
end
Expand Down

0 comments on commit 86a8b08

Please sign in to comment.