Add document-wide fallback_font to a Prawn PDF

The Prawn PDF manual says that you can set a document-wide fallback font in case you are dealing with special characters (in my case, Chinese characters), that may not be included in the default font.

But it doesn’t give an exact example, so here is that example:

This code goes at the top of your Prawn PDF code, and it assumes there is a font file (in my case, fireflysung.ttf) in a directory called vendor/fonts/.

    font_families["Firefly"] = {
      :normal => { :file => "#{Rails.root}/vendor/fonts/fireflysung.ttf" }
    }

    def fallback_fonts
      ["Firefly"]
    end

You can also pass multiple font family strings into the array defined by fallback_fonts if you want multiple fallback fonts.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s