diff --git a/lib/pdf_generator.ex b/lib/pdf_generator.ex index b578177..47fc09f 100644 --- a/lib/pdf_generator.ex +++ b/lib/pdf_generator.ex @@ -78,21 +78,29 @@ defmodule PdfGenerator do end # return file name of generated pdf - # requires: Porcelain, Misc.Random + # requires: Porcelain, Misc.Random, zarex + @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 - - 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 + * `: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 pdf_path_1 = PdfGenerator.generate "

Boom

" @@ -102,7 +110,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 @@ -111,9 +120,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 = [ @@ -153,6 +162,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, Zarex.sanitize(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" diff --git a/mix.exs b/mix.exs index 3c33330..20f131f 100644 --- a/mix.exs +++ b/mix.exs @@ -49,6 +49,8 @@ defmodule PdfGenerator.Mixfile do {:porcelain, "~> 2.0"}, # a helper {:misc_random, ">=0.2.6" }, + # filename sanitizer + {:zarex, "~> 0.2"}, # generate docs {:earmark, "~> 0.1", only: :dev}, {:ex_doc, "~> 0.7", only: :dev} diff --git a/mix.lock b/mix.lock index ef07487..644b982 100644 --- a/mix.lock +++ b/mix.lock @@ -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], []}}