DEV Community

Matias Carpintini
Matias Carpintini

Posted on

How to generate OG links preview like (for free) with Rails 7

Hey hey heyyyyyy
Quick article on how to have beauty link previews, just like does

You're gonna get something like this:

Final result

Yup, was being literal with the "like" 😛
I've build something similar for my new project, here's how i've build it...

Almost every guide that i found was using, in fact, uses it. It's pretty popular, and efficient, BUT, i don't want to pay $14 bucks for each 1k images 😂🐁

So, i'm going to use Grover: A Ruby gem to transform HTML into PDFs, PNGs or JPEGs using Google Puppeteer and Chromium.

If you want to build this with, let's say, Node.js, you can also follow me. Puppeteer is popular and easy to use.

Long story short: We're going to build a HTML view and take an screenshot of it.

You can create a blank Rails project, or just add additional functionality to your current app.


Install Grover gem with
bundle add grover

And the dependency of Grover, Puppeteer, with:
yarn add puppeteer

Then create an empty controller where you can respond with different link preview templates:
rails g controller og-imager dev_to

In the dev_to action, let's call Grover and let him do the magic

class OgImagerController < ApplicationController
  # GET /og_imager/dev_to
  # @param {string} title
  # @param {string} avatar
  # @param {string} username
  # @param {string} timestamp
  # @param {array} logos
  # @returns image/png
  def dev_to
    # Get params and set to variable
    # TODO: Retrieve your object instead :p
    @title = params[:title]
    @avatar = params[:avatar]
    @username = params[:username]
    @timestamp = params[:timestamp]
    @logos = params[:logos]

    # accepts a URL or inline HTML and optional parameters for Puppeteer
    grover =

    # Get a screenshot
    png = grover.to_png

    # Render image
    send_data(png, type: 'image/png', disposition: 'inline')
Enter fullscreen mode Exit fullscreen mode

Here's our view, just simple HTML/CSS

<div class="container">
  <div class="card">
    <h1 class="title">
      <%= @title %>
    <div class="details">
      <div class="user">
        <img src="<%= @avatar %>" class="avatar">
        <p class="username">
          <%= @username %> - <%= @timestamp %>
      <div class="logos">
        <% @logos.each do |logo| %>
          <img src="<%= logo %>" class="logo">
        <% end %>

  @import url(';500&display=swap');

    font-family: 'Roboto', sans-serif;
    color: #cd685f;

    width: 1128px;
    height: 600px;
    display: flex;

    display: flex;
    justify-content: space-between;
    flex-direction: column;
    margin: 2.8rem 4rem 4rem 2.8rem;
    border: 3px solid #c56b61;
    border-radius: 25px 25px 0px 0px;
    box-shadow: 7px 10px 0px 1px #c56b61;
    padding: 3.8rem 1.8rem 1.8rem 1.8rem;

    margin: 0px;
    font-weight: 500;
    font-size: 5rem;
    line-height: 80px;

    display: flex;
    justify-content: space-between;

    display: flex;
    align-items: center;

    border: 3px solid #c56b61;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    margin-right: 10px;

    margin: 0;
    font-size: 2.5rem;

    display: flex;
    justify-content: space-between;

    width: 60px;
    height: 60px;
    margin-left: 20px;
    transform: rotate(5deg);
Enter fullscreen mode Exit fullscreen mode

And finally, create an initializer to set our Browser viewport (final image size).


Grover.configure do |config|
  config.options = {
    viewport: {
      width: 1128,
      height: 600
Enter fullscreen mode Exit fullscreen mode

If you have created a blank project, and want to use this as a micro-service, here's a guide on how to deploy this thing to Heroku.

  1. Create the app on your Heroku account (T'm assuming you already have the cli installed): heroku create your_app_name
  2. Warn Heroku of our JS library (Puppeteer): heroku buildpacks:add heroku/nodejs --index=1
  3. And also help him to setup Puppeteer dependencies 😛: heroku buildpacks:add jontewks/puppeteer --index=2
  4. Tell Grover to run Puppeteer in the "no-sandbox": heroku config:set GROVER_NO_SANDBOX=true

To make use of this image on the link preview, you will need The Open Graph protocol. Just simple meta tags that browsers will look for.

Meta tags are always on

<%= yield(:head) %>
Enter fullscreen mode Exit fullscreen mode

And then ask ERB to fill previous block with

<%= content_for :head do %>
  <meta property="og:title" content="<%= @article.title %>">
  <meta property="og:image" content="<%= article_link_preview(@article) %>">
  <meta property="og:image:type" content="image/png" />
  <meta property="og:image:width" content="1128">
  <meta property="og:image:height" content="600">
  <meta name="twitter:card" content="summary_large_image" />
<% end %>
Enter fullscreen mode Exit fullscreen mode

at your details view. Like: /articles/:id

The twitter:card thing it's because they are more fancy on link previews, you can read more about that here

The previous article_link_preview method it's a simple helper that generates the URL that we are looking for.


def article_link_preview article
  uri = URI.parse('')
  uri.query = URI.encode_www_form(
    title: article.title,
    'images[]': article.tags.collect(&:image),
    timestamp: article.created_at.to_formatted_s(:short),
Enter fullscreen mode Exit fullscreen mode

You will get something like this:,%20in%20Rails%207&avatar=[]=[]=

Here's the repo of the whole thing.

Top comments (0)