๐Ÿ–ผ๏ธ Carta

Hex.pmHex DocsCILicense: MIT

Generate images from HTML using a headless browser.

Carta renders HTML content in a headless browser and captures it as a JPEG image. It is backend-agnostic: any Browse-compatible browser works (e.g. BrowseChrome, BrowseServo).

Use cases: ๐ŸŒ Open Graph images ยท ๐Ÿ“ฑ Social media cards ยท ๐Ÿ“ง Email banners ยท ๐Ÿ“œ Certificates ยท ๐Ÿงพ Invoices ยท ๐Ÿท๏ธ Badges ยท and anything you can build with HTML and CSS.

๐Ÿ“ฆ Installation

Add carta and a browser backend to your dependencies:

def deps do
  [
    {:carta, "~> 0.2.0"},
    {:browse_chrome, "~> 0.2"}
  ]
end

๐Ÿ”ง Setup

Start a browser pool in your application's supervision tree:

# config/config.exs
config :browse_chrome,
  default_pool: MyApp.BrowserPool,
  pools: [
    {MyApp.BrowserPool, pool_size: 4}
  ]

# lib/my_app/application.ex
children = BrowseChrome.children()

๐Ÿš€ Usage

html = """
<html>
  <style>
    body { background: linear-gradient(135deg, #667eea, #764ba2); display: flex;
           align-items: center; justify-content: center; width: 1200px; height: 630px; }
    h1 { color: white; font-size: 48px; font-family: sans-serif; }
  </style>
  <body>
    <h1>Hello, Carta!</h1>
  </body>
</html>
"""

{:ok, jpeg_binary} = Carta.render(MyApp.BrowserPool, html)

Since it's a full browser, everything works: Google Fonts via <link>, flexbox, grid, images, SVG, etc. ๐ŸŽจ

โš™๏ธ Options

Option Default Description
:width1200 Viewport width in pixels
:height630 Viewport height in pixels
:quality90 JPEG quality (1-100)

๐Ÿ’พ Caching

Rendering is expensive. Use Carta.cache_key/2 to derive a stable hash from the input and options, then cache the result in your own store:

key = Carta.cache_key(html)

case MyCache.get(key) do
  nil ->
    {:ok, jpg} = Carta.render(MyApp.BrowserPool, html)
    MyCache.put(key, jpg)
    jpg

  cached ->
    cached
end

๐Ÿค” Why not Wallaby?

Wallaby is a browser testing framework built on WebDriver. Carta takes a different approach:

๐Ÿ“„ License

MIT License. See LICENSE for details.