週刊Elixirライブラリ2015-13

Elixirのライブラリの解説を週ごとにお届けする記事です。
解説が間違っていた場合には、コメントか@hayabusa333にご連絡くださると嬉しいです。

今回はGutenexについて

# Gutenex is 何?
Gutenexは、ElixirでPDFを生成するためのライブラリとなります。

# 実行環境
OS:OS X Yosemite
Erlang:Eshell V6.5, OTP-Version 18
Elixir:v1.0.4

# Gutenexの実行を行うための新規プロジェクトの作成

$ mix new my_gutenex
$ cd new my_gutenex

# HexにてGutenexをインストールするために設定ファイルの記載を行う

$ vim mix.exs

mix.exsの内容は下記となります。

defmodule MyGutenex.Mixfile do
  use Mix.Project

  def project do
    [app: :my_gutenex,
     version: "0.0.1",
     elixir: "~> 1.0",
     build_embedded: Mix.env == :prod,
     start_permanent: Mix.env == :prod,
     deps: deps]
  end

  # Configuration for the OTP application
  #
  # Type `mix help compile.app` for more information
  def application do
    [applications: [:logger, :gutenex]]
  end

  # Dependencies can be Hex packages:
  #
  #   {:mydep, "~> 0.3.0"}
  #
  # Or git/path repositories:
  #
  #   {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"}
  #
  # Type `mix help deps` for more examples and options
  defp deps do
    [{:gutenex, git: "https://github.com/SenecaSystems/gutenex"}]
  end
end

# 依存関係の解決を行います。

$ mix deps.get
$ mix deps.compile

# Gutenexを使ってみる

$ iex -S mix
iex> {:ok, pid} = Gutenex.start_link
iex> Gutenex.begin_text(pid) |> Gutenex.set_font("Helvetica", 21) |> Gutenex.text_position(110, 320) |> Gutenex.write_text("Elixir") |> Gutenex.end_text |> Gutenex.export("./test.pdf")

まず、Gutenex.start_link にて pid を作成し、その後に作成したpidを引数にPDFを作成していくだけです。
同一のPIDを使用してpdfに書き出しを行うと、前に作成したPDFの上に本当に上書きしてしまい、前に作成したpdfのテキスト文章は残ったままなので注意が必要です。
Gutenex.text_position(110, 320) はテキストの位置になりますが、左側がX軸で右側がY軸です。Y軸はPDFの下側が0なので注意が必要です。

# まとめ
今回はElixirにてPDFを出力できるライブラリのGutenexを紹介させていただきました。
PDFファイルを出力したい時もありますので使用してみてはいかがでしょうか。
また上記のライブラリは、まだ開発中でありHex上には上がっていないため注意が必要です。

それでは皆さま、良いElixirライフを