View Source Mudbrick (mudbrick v0.3.0)
Top-level API for creating documents.
Example
Compression, OTF font with special characters and image placement:
iex> import Mudbrick.TestHelper # import some example fonts and images
...> import Mudbrick
...> alias Mudbrick.Page
...> new(
...> compress: true, # flate compression for fonts, text etc.
...> fonts: %{bodoni: [file: bodoni_regular()]}, # register an OTF font
...> images: %{flower: [file: flower()]} # register a JPEG
...> )
...> |> page(size: Page.size(:letter))
...> |> image( # place preregistered JPEG
...> :flower,
...> scale: {100, 100},
...> position: {50, 600} # in points (1/72 inch), starts at bottom left
...> )
...> |> text( # write red, right-aligned text in Bodoni 14, with
...> {"CO₂", colour: {1, 0, 0}}, # right side anchored 200 points from left of page
...> align: :right,
...> font: :bodoni,
...> font_size: 14,
...> position: {200, 700}
...> )
...> |> render() # produces iodata, can go straight to File.write/2
...> |> IO.iodata_to_binary() # or turned into a (non-String) binary
Summary
Functions
Compress data with the same method that PDF generation does. Useful for testing.
Decompress data with the same method that PDF generation does. Useful for testing.
Insert image previously registered in new/1
at the given coordinates.
Start a new document.
Start a new page upon which future operators should apply.
Produce iodata
from the current document.
Write text at the given coordinates.
Types
Functions
Compress data with the same method that PDF generation does. Useful for testing.
Example
iex> Mudbrick.compress(["hi", "there", ["you"]])
[<<120, 156, 203, 200, 44, 201, 72, 45, 74, 173, 204, 47, 5, 0, 23, 45, 4, 71>>]
Decompress data with the same method that PDF generation does. Useful for testing.
Example
iex> Mudbrick.decompress([<<120, 156, 203, 200, 44, 201, 72, 45, 74, 173, 204, 47, 5, 0, 23, 45, 4, 71>>])
["hithereyou"]
Insert image previously registered in new/1
at the given coordinates.
Options
:position
-{x, y}
in points, relative to bottom-left corner.:scale
-{w, h}
in points.:skew
-{x, y}
, passed through to PDFcm
operator.
All options default to {0, 0}
.
Examples
iex> Mudbrick.new(images: %{lovely_flower: [file: Mudbrick.TestHelper.flower()]})
...> |> Mudbrick.page()
...> |> Mudbrick.image(:lovely_flower, position: {100, 100}, scale: {100, 100})
iex> Mudbrick.new()
...> |> Mudbrick.page()
...> |> Mudbrick.image(:my_face, position: {100, 100}, scale: {100, 100})
** (Mudbrick.Image.Unregistered) Unregistered image: my_face
Tip: to make the image fit the page, pass e.g. Page.size(:a4)
as the
scale
and {0, 0}
as the position
.
@spec new(opts :: Mudbrick.Document.options()) :: Mudbrick.Document.t()
Start a new document.
Options
:compress
- when set totrue
, apply deflate compression to streams (if compression saves space). Default:false
:fonts
- register OTF or built-in fonts for later use.:images
- register images for later use.
The following options define metadata for the document:
:producer
- software used to create the document, default:"Mudbrick"
:creator_tool
- tool used to create the document, default:"Mudbrick"
:create_date
-DateTime
representing the document's creation time:modify_date
-DateTime
representing the document's last update time:title
- title (can change e.g. browser window title), default:nil
:creators
- list of names of the creators of the document, default:[]
Examples
Register an OTF font. Pass the file's raw data to the :file
option.
iex> Mudbrick.new(fonts: %{bodoni: [file: Mudbrick.TestHelper.bodoni_regular()]})
Or a built-in font. Note that these don't support right-alignment or special characters.
iex> Mudbrick.new(fonts: %{helvetica: [name: :Helvetica, type: :TrueType, encoding: :PDFDocEncoding]})
Register an image.
iex> Mudbrick.new(images: %{flower: [file: Mudbrick.TestHelper.flower()]})
Set document metadata.
iex> Mudbrick.new(title: "The best PDF", producer: "My cool software")
Start a new page upon which future operators should apply.
Options
:size
- a tuple of{width, height}
. Some standard sizes available inMudbrick.Page.size/1
.
Produce iodata
from the current document.
Write text at the given coordinates.
Top-level options
:font
- Required. Name of a font previously registered withnew/1
.:position
- Coordinates from bottom-left of page in points. Default:{0, 0}
.:font_size
- Size in points. Default:12
.:leading
- Leading in points. Default is 120% of:font_size
.:align
- Either:left
or:right
. Default::left
. Note that the rightmost point of right-aligned text is the horizontal offset provided to:position
.
Individual write options
When passing a list to this function, each element can be tuple of {text, opts}
,
where opts
are:
:colour
-{r, g, b}
tuple. Each element is a number between 0 and 1. Default:{0, 0, 0}
.
Examples
Write "CO₂" in the bottom-left corner of a default-sized page.
iex> import Mudbrick.TestHelper
...> import Mudbrick
...> new(fonts: %{bodoni: [file: bodoni_regular()]})
...> |> page()
...> |> text("CO₂", font: :bodoni)
Write "I am red" at 200, 200, where "red" is in red.
iex> import Mudbrick.TestHelper
...> import Mudbrick
...> new(fonts: %{bodoni: [file: bodoni_regular()]})
...> |> page()
...> |> text(["I am ", {"red", colour: {1, 0, 0}}], font: :bodoni, position: {200, 200})
Write "I am bold" at 200, 200, where "bold" is in bold.
iex> import Mudbrick.TestHelper
...> import Mudbrick
...> new(fonts: %{regular: [file: bodoni_regular()], bold: [file: bodoni_bold()]})
...> |> page()
...> |> text(["I am ", {"bold", font: :bold}], font: :regular, position: {200, 200})