rails-erd icon indicating copy to clipboard operation
rails-erd copied to clipboard

Generated PDF/PNG are Broken But DOT Format Works

Open pacarvalho opened this issue 4 years ago • 2 comments

Whenever I opt for a format that is not DOT such as PDF and PNG I end up with something that looks like the image below:

erd

I am running my code with the following Dockerfile:

FROM ruby:2.5-alpine AS installer

# Expose port
EXPOSE 3000

# set some rails env vars
ENV RAILS_ENV production
ENV PORT 3000

# set the app directory var
ENV APP_HOME /app
RUN mkdir -p ${APP_HOME}
WORKDIR ${APP_HOME}

# Install necessary packanges
RUN apk add --no-cache \
    build-base curl libressl-dev zlib-dev git \
    mariadb-dev tzdata imagemagick libxslt-dev \
    bash nodejs graphviz

# Copy gemfiles to be able to bundle install
COPY Gemfile* ./

Using the ERD configuration below:

attributes:
  - content
  - foreign_key
  - inheritance
disconnected: true
filename: erd
filetype: png
indirect: true
inheritance: false
markup: true
notation: simple
orientation: horizontal
polymorphism: false
sort: true
warn: true
title: sample title
exclude: null
only: null
only_recursion_depth: null
prepend_primary: false
cluster: false
splines: spline

And running the command bundle exec erd inside the running container. The DOT format works as expected. Any ideas what could be causing this?

pacarvalho avatar Jun 09 '20 19:06 pacarvalho

@pacarvalho I was experiencing the same issue. The core of the problem is that the font specified by the RailsERD gem is "Arial" and this font is not available within the ruby Alpine image being used in your Dockerfile. Additionally, you cannot override this font through the standard RailsERD config options.

There is a way to install Arial into the Ruby Alpine Docker image. However, I have not been able to get this to work using the msttcorefonts-installer Alpine package (suggested by numerous SO questions). Because of this, I opted to install an alternate font into Alpine and then configure RailsERD to use this font instead.

I solved this by monkey patching the RailsERD gem via an initializer. I override the method returning the Arial font names. I opted to installed the NOTO font package (I believe any font would work).

Dockerfile


# Install Graphviz & Fonts
RUN apk add graphviz
RUN apk add font-noto
RUN apk add fontconfig 
RUN fc-cache -f

./config/initalizers/rails_erd.rb


module RailsERD
    class Config
        def self.font_names_based_on_os
            { 
                normal: "Noto Sans",
                bold:   "Noto Sans Bold",
                italic: "Noto Sans Italic",
            }
        end
    end
end

I am running 6.1.3 and was able to successfully generate a PDF diagram using this method. @voormedia It would be great if we could pass in font names when generating diagrams or as a YAML configuration option in .erdconfig

johnsaltarelli avatar Mar 22 '21 03:03 johnsaltarelli

It would be great if we could pass in font names when generating diagrams or as a YAML configuration option in .erdconfig

I don't see why we couldn't add that!

kerrizor avatar Mar 22 '21 17:03 kerrizor