Cloud Render using Elixer

This example shows how to generate a PDF from a DOC template using Elixir.  It calls the Docmosis REST API to merge the data with the template and stream the result back.

The sample code includes the instructions to get started. You will need a Free Trial and then plug your Docmosis Cloud access key into the code below, then run.

defmodule DocmosisTest.Example do
@moduledoc """
This elixir module calls the Docmosis Web Service in order to render a document
from the default template (samples/WelcomeTemplate.docx) into a PDF.

Thank you to Ali Tahbaz for creating this example to share with everyone.

Keywords: Elixir REST post generate pdf template


**In order to execute:**
1. Create a new mix project and navigate into the new directory with the following
`mix new docmosis_test && cd docmosis_test`
2. Add the following application configuration entry into /config/config.exs
```
config :docmosis_test,
  docmosis_access_key: "enter your key here",
  docmosis_render_api_uri: "https://dws2.docmosis.com/services/rs/render"
```
3. Open mix.exs and replace your application and deps functions with the following
```
  def application do
    [applications: [:httpoison]]
  end

  defp deps do
    [{:poison, "~> 3.0", override: true},
     {:httpoison, "~> 0.11.1"}]
  end
```
3. Drop this file defining DocmosisTest.Example into /lib ("DocmosisTest.Example.ex")
4. Get dependencies and execute
```
mix do deps.get, compile
iex -S mix
iex> DocmosisTest.Example.render()
```
"""

@doc """
Renders an example document named `output_filename` into `output_root` using the Docmosis cloud service.

Returns either `:ok` or `:error`

  ## Examples

      iex> DocmosisTest.Example.render()
      :ok
"""
  def render(output_root \\ "", output_filename \\ "myWelcome.pdf") do
    case do_render("samples/WelcomeTemplate.docx", output_filename) do
      {:ok, rendered_content} ->
        if(output_root == "", do: Path.join(__DIR__, ".."), else: output_root)
        |> Path.join(output_filename)
        |> IO.inspect(label: "Rendering output to ")
        |> File.write!(rendered_content)
        :ok
      {:error, reason} ->
        IO.puts "Something went wrong: #{reason}"
        :error
    end
  end

  defp do_render(template_name, output_name, format \\ "pdf", access_key \\ nil)
  defp do_render(template_name, output_name, format, nil) do
    # ensure the access key has been set
    access_key = Application.get_env(:docmosis_test, :docmosis_access_key)
    if is_nil(access_key) || (access_key == "") || (access_key == "enter your key here") do
      {:error, "Please set your access key prior to running this example"}
    else
      do_render(template_name, output_name, format, access_key)
    end
  end
  defp do_render(template_name, output_name, format, access_key) do
    docmosis_attributes = docmosis_settings(template_name, output_name, format)
    request_json = example_data()
    |> Map.merge(%{accessKey: access_key})
    |> Map.merge(docmosis_attributes)
    |> Poison.encode!()

    # the default timeout of 5s will work for the example, increase the recv_timeout 
    # to something higher like this if necessary for more significant rendering requests (30s here)
    case HTTPoison.post(docmosis_render_uri(), request_json, [{"Content-Type", "application/json"}], recv_timeout: 30_000) do
      {:ok, %HTTPoison.Response{body: document}} ->
        case document do
          "{\"succeeded\":false,\"shortMsg\":\"" <> error_message ->
            {:error, String.replace_trailing(error_message, "\"}", "")}
          _ ->
            {:ok, document}
        end
      {:error, error} ->
        {:error, "Error retrieving document from docmosis: #{inspect error}"}
      unexpected_response ->
        {:error, "An unexpected response was returned from docmosis: #{inspect unexpected_response}"}
    end
  end

  defp docmosis_render_uri() do
    Application.get_env(:docmosis_test, :docmosis_render_api_uri) || "https://dws2.docmosis.com/services/rs/render"
  end

  defp docmosis_settings(template_name, output_name, format) do
    %{
      templateName: template_name,
      outputName: output_name,
      outputFormat: format
    }
  end

  defp example_data() do
    %{
      data: %{
        title: "Welcome to Docmosis in the Cloud",
        messages: [
          %{msg: "This cloud experience is better than I thought."},
          %{msg: "The sun is shining."},
          %{msg: "Right, now back to work."}
        ]
      }
    }
  end
end

Feedback

Invalid Input

Sorry, this field will only accept letters and numbers, and not special characters, to limit spam.

Sorry, this field will only accept letters and numbers, and not special characters, to limit spam.

Invalid Input

Applies To

Docmosis-Java Tornado Cloud
Version(s) - - DWS2