Tutorial
Generating Open Graph Social Cards Automatically in Lambdapad
When you share a blog post on X, LinkedIn, Slack, or Discord, the preview card comes from Open Graph (og:image, 1200x630) and Twitter (twitter:image, 800x418) metadata. Instead of designing each card by hand or spinning up a headless browser, you can generate them automatically in pure Elixir with resvg.
Why SVG + resvg?
Many static site pipelines reach for Puppeteer or Playwright to screenshot HTML templates into PNGs. That works, but it pulls Chromium into your build environment.
With SVG + resvg (via Mix.install([{:resvg, "~> 0.5"}]) in a standalone og_cards.exs script), you get:
- Fast, deterministic PNG rendering directly from an SVG string in memory.
- No browser dependencies—just a single Elixir script you can run locally or in CI.
-
Consistent branding across every article, plus separate brand cards for
/and/blog.
1. The Standalone og_cards.exs Script
In website/og_cards.exs, we start with Mix.install([{:resvg, "~> 0.5"}]) so the script needs no changes to your main mix.exs or lpad binary:
#!/usr/bin/env elixir
Mix.install([
{:resvg, "~> 0.5"}
])
For each Markdown file in posts/**/*.md, the script:
-
Parses the frontmatter headers (
id,title,date,type). -
Picks a gradient deterministically from the post slug using
:erlang.phash2(slug, length(@gradients)), so each post keeps a stable visual identity across rebuilds. -
Fits the title automatically across a list of
{font_size, line_height}pairs until it wraps cleanly within 4 lines. -
Embeds brand assets as Base64 data URIs (
notepad.pngor a logo) soResvgrenders everything self-contained without external file fetches. -
Writes both sizes into
assets/images/og/<id>.png(1200x630) andassets/images/x/<id>.png(800x418):
og_svg = Lambdapad.OgImage.generate_og(post, images_dir)
{:ok, og_png} = Resvg.svg_string_to_png_buffer(og_svg, resvg_opts)
File.write!(Path.join(og_dir, "#{id}.png"), og_png)
x_svg = Lambdapad.OgImage.generate_x(post, images_dir)
{:ok, x_png} = Resvg.svg_string_to_png_buffer(x_svg, resvg_opts)
File.write!(Path.join(x_dir, "#{id}.png"), x_png)
We also generate dedicated brand cards (default.png and blog.png) so the home page, documentation, and /blog index have their own tailored previews rather than looking like individual articles.
2. Wiring Cards into templates/head.html
Because lambdapad.exs already copies assets/{favicons,images,webfonts}/** to site/, anything under assets/images/og/ and assets/images/x/ is automatically published to /images/og/ and /images/x/.
In templates/head.html, we branch between individual posts, the /blog index, and the rest of the site:
<meta property="og:image" content="{% if post.id %}{{ site_url }}/images/og/{{ post.id }}.png{% elif posts %}{{ site_url }}/images/og/blog.png{% else %}{{ site_url }}/images/og/default.png{% endif %}" />
<meta property="og:image:type" content="image/png" />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:image" content="{% if post.id %}{{ site_url }}/images/x/{{ post.id }}.png{% elif posts %}{{ site_url }}/images/x/blog.png{% else %}{{ site_url }}/images/x/default.png{% endif %}" />
Make sure og:title, og:description, og:url, and <link rel="canonical"> also adapt per page (for example, setting excerpt: true on pages "posts" in lambdapad.exs so post.excerpt is available in head.html).
3. Running and Verifying
Whenever you add a new post:
elixir og_cards.exs
lpad
You can then inspect the generated HTML or check your live URL with Open Graph preview tools like opengraph.xyz to confirm every card renders with the right title, summary, dimensions, and image.