Skip to content

Commit

Permalink
add generate_binary
Browse files Browse the repository at this point in the history
  • Loading branch information
gutschilla committed Aug 25, 2016
1 parent 7ccd4ac commit 9ee1de8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
24 changes: 24 additions & 0 deletions lib/pdf_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,28 @@ defmodule PdfGenerator do

end

@doc """
Takes same options as `generate` but will return an
`{:ok, binary_pdf_content}` tuple.
"""
def generate_binary( options \\ [] ) do
result = generate options
case result do
{:ok, filename} -> {:ok, filename |> File.read! }
{:error, reason} -> {:error, reason}
end
end

@doc """
Same as generate_binary but returns PDF content directly or raises on
error.
"""
def generate_binary!( options \\ [] ) do
result = generate_binary options
case result do
{:ok, content} -> content
{:error, reason} -> raise reason
end
end

end
12 changes: 6 additions & 6 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule PdfGenerator.Mixfile do
[
app: :pdf_generator,
name: "PDF Generator",
version: "0.3.4",
version: "0.3.5",
elixir: ">= 1.0.0",
deps: deps,
description: description,
Expand All @@ -19,22 +19,22 @@ defmodule PdfGenerator.Mixfile do
#
# Type `mix help compile.app` for more information
def application do
[
[
applications: [ :logger, :porcelain ],
mod: { PdfGenerator, [] }
]
end

def description do
"""
A wrapper for wkhtmltopdf (HTML to PDF) and PDFTK (adds in encryption) for use in Elixir projects.
"""
A wrapper for wkhtmltopdf (HTML to PDF) and PDFTK (adds in encryption) for
use in Elixir projects.
"""
end


defp package do
[
files: ["lib", "mix.exs", "README.md", "LICENSE", "test"],
files: ["lib", "mix.exs", "README.md", "LICENSE", "test"],
maintainers: ["Martin Gutsch"],
licenses: ["MIT"],
links: %{
Expand Down
11 changes: 9 additions & 2 deletions test/pdf_generator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ defmodule PdfGeneratorTest do

@html "<html><body><h1>Hi</h1><p>Yikes!</p></body></html>"


test "agent startup" do
{:ok, _ } = File.stat PdfGenerator.PathAgent.get.wkhtml_path
end
Expand All @@ -15,7 +14,7 @@ defmodule PdfGeneratorTest do
file_info = File.stat! temp_filename
assert file_info.size > 0
pdf = File.read! temp_filename

# PDF header should be present
assert String.slice( pdf, 0, 6) == "%PDF-1"
end
Expand All @@ -24,4 +23,12 @@ defmodule PdfGeneratorTest do
{:ok, temp_filename } = PdfGenerator.generate @html, [ command_prefix: "env" ]
end

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

test "generate_binary reads file" do
assert {:ok, "%PDF-1" <> _pdf} = PdfGenerator.generate_binary( @html )
end

end

0 comments on commit 9ee1de8

Please sign in to comment.