Git Product home page Git Product logo

elixir2pdf's Introduction

Elixir2pdf

Elixir2pdf is a simple Elixir wrapper for ErlGuten, a Erlang PDF generation application. It is limited to my current needs of ErlGuten's functionality as well as some additional functionality not provided by ErlGuten.

Installation

The package can be installed via GitHub:

def deps do
  [{:elixir2pdf, github: "jsonkenl/elixir2pdf"}]
end

Basic Usage

PDF files have the following basic properties:

  • The origin {0, 0} of a given document is the bottom-left corner. The x-axis extends to the right and the y-axis extends upward.
  • Locations and sizes are stored as a PDF unit. Each PDF unit is equal to 1/72 of an inch. For example, the coordinate point {72, 72} is located 1 inch to the right of the left edge of the page and 1 inch above the bottom edge of the page.
  • PDF documents are sized according to their media box and crop box values. The media box is the total size of the document while the crop box is the viewable area. While most PDF documents have the same media box and crop box values, this is important to note because the origin {0, 0} is in relation to the media box as opposed to the viewable area of the crop box.

Elixir2pdf works by creating a PDF GenServer process and utilizing various functions to add objects to the state of said process and ultimately exporting the data to a PDF document. See below for a list of ErlGuten functionality currently exposed and an example. Follow the links to view details for each function.

Current Exposed Functions:

General

Document

Text

Graphics

Example:

defmodule Example do
  import Elixir2pdf

  @font1 {"OCR-B-Digits", 10}
  @font2 {"Times-Roman", 6}
  @font3 {"Courier-Bold", 12}
  @font4 {"Times-Roman", 5}
  @font5 {"OCR-A-Digits", 10}

  def check_template do
    pid = new

    pid
    |> draw_rectangle({85,675}, {300, 125}, 0.5)
    |> set_font(@font1, :blue)
    |> right_aligned_text(@font1, 375, 785, "1025")
    |> set_font(@font2)
    |> right_aligned_text(@font2, 300, 765, "DATE")
    |> draw_line({305, 765}, {355, 765}, 0.5)
    |> text(90,750, "PAY TO THE")
    |> text(90,745, "ORDER OF")
    |> draw_line({125, 745}, {315, 745}, 0.5)
    |> draw_line({315,745}, {315, 750}, 0.5)
    |> set_font(@font3)
    |> text(320, 745, "$")
    |> draw_rectangle({330, 742}, {45, 13}, 1, :antiquewhite4)
    |> draw_line({95, 727}, {320, 727}, 0.5)
    |> set_font(@font2)
    |> text(325, 727, "DOLLARS")
    |> set_font(@font4)
    |> text(90, 695, "MEMO")
    |> draw_line({110, 695}, {220, 695}, 0.5)
    |> draw_line({240, 695}, {375, 695}, 0.5)
    |> set_font(@font5)
    |> text(110, 680, "000000000  000000000    1025")
    |> export("./example.pdf")
    |> close
  end
end

Contributing

Contributions are encouraged. Feel free to fork the repo, add your code along with appropriate tests and documentation (ensuring all existing tests continue to pass) and submit a pull request.

elixir2pdf's People

Contributors

hdra avatar jsonkenl avatar lostkobrakai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

elixir2pdf's Issues

Update to use the canonical erlguten repository

The canonical erlguten repository seems to be https://github.com/richcarl/erlguten but elixir2pdf uses your own fork, which doesn't have any commit past 2012. The canonical version has commit from 2016.
Would it be possible to make elixir2pdf use that newer version? Thanks

By the way, I realized this version mismatch because the old version outputs an annoying "New pdf" message (an io.format) and the new version apparently doesn't (I only read the code).

Add export to binary function

Currently, the export/2 function requires a file path for the PDF to be written to.

But sometimes, you may not want the PDF to be written to file, and use it in memory directly. Would you consider adding an export/1 function that simply returns the PDF binary so that the consumer can decide what to do with it?

Question about starting multiple PDF instances at once

Hi,

I'm looking at using Elixir2PDF in a phoenix application and I noticed while testing that you can't create two instances within the one run time, e.g:

iex(1)> pdf = Elixir2pdf.new
New pdf
#PID<0.567.0>
iex(2)> pdf = Elixir2pdf.new
New pdf
** (MatchError) no match of right hand side value: {:error, {:already_started, #PID<0.567.0>}}
    /Users/seangeo/Development/sendle-couriers-ex/deps/erlguten/src/eg_pdf.erl:128: :eg_pdf.new/0

Is this something that should be possible? Is it a problem with erlguten library itself?

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.