14

I'm trying to use ruby to generate a PDF using Prawn on OS X. I have the following:

font 'Arial'

Arial is installed on my Mac. But when I try to generate the PDF, I get the following error:

Prawn::Errors::UnknownFont in ProjectsController#show
Arial is not a known font.

How can I get this common font to work in Prawn? In fact, almost anything other than Helvetica or Times New Roman throws this same error. This is part of a Rails 3.2 app.

If I try to load the font ttf file directly, per Ashish's suggestion below, I get a Bad font family message:

RuntimeError (Bad font family):
  app/pdfs/quote_sheet_pdf.rb:29:in `page_top'
  app/pdfs/quote_sheet_pdf.rb:12:in `initialize'
  app/controllers/projects_controller.rb:9:in `new'
  app/controllers/projects_controller.rb:9:in `block (2 levels) in show'
  app/controllers/projects_controller.rb:7:in `show'
  config/initializers/quiet_assets.rb:7:in `call_with_quiet_assets'
croceldon
  • 4,511
  • 11
  • 57
  • 92
  • Prawn has a small set of fonts, and they are embedded in the gem. Here's a [list of the available fonts](https://github.com/prawnpdf/prawn/tree/master/data/fonts). I don't think prawn can use fonts installed in the OS. – Augusto Aug 14 '12 at 15:49

3 Answers3

30

If you’re using the :style option to calls to text, e.g.

text "Hello World", :style => :italic

Then the font you’re using at the time needs to have an associated font family, otherwise you’ll get the “Bad font family” error you’re seeing, e.g. this:

Prawn::Document.generate("output.pdf") do
  font "/Library/Fonts/Arial.ttf"
  text "Hello World", :style => :italic
end

produces: Bad font family (RuntimeError).

One way round this would be to always specify the exact font file you want every time you want to change style, e.g.

font "/Library/Fonts/Arial Italic.ttf"
text "Hello World"

A better option would be to create a font family with the styles you want:

Prawn::Document.generate("output.pdf") do

  font_families.update("Arial" => {
    :normal => "/Library/Fonts/Arial.ttf",
    :italic => "/Library/Fonts/Arial Italic.ttf",
    :bold => "/Library/Fonts/Arial Bold.ttf",
    :bold_italic => "/Library/Fonts/Arial Bold Italic.ttf"
  })

  font "Arial"
  text "Hello World"
  text "Hello World", :style => :italic
  text "Hello World", :style => :bold
  text "Hello World", :style => :bold_italic
end

After you’ve set up the font family you can just use Arial as the font name, and you can use the different styles easily.

matt
  • 78,533
  • 8
  • 163
  • 197
5

I had the same problem trying to load fonts like this.

@pdf.font_families.update(
    'Arial' => { :normal => Rails.root.join('public/arial.ttf'),
                 :bold   => Rails.root.join('public/arialbd.ttf') }
)

It turn's out that Rails.root.join doesn't return explicit String object. The solution is to add to_s at the end of the expression.

@pdf.font_families.update(
  'Arial' => { :normal => Rails.root.join('public/arial.ttf').to_s,
               :bold   => Rails.root.join('public/arialbd.ttf').to_s }
)

by TheR

Damjan Rems
  • 101
  • 1
  • 3
4

try passing full path of arial.ttf to the font function as below -

Prawn::Document.generate("custom_font_usage.pdf") do
  font "/path/to/fonts/arial.ttf"
  text "this is a test " * 20 
end

Also see some links that are relevant -

  1. How many fonts are available in Prawn?
  2. Prawn documentation - http://rubydoc.info/gems/prawn/0.12.0/frames
Community
  • 1
  • 1
saihgala
  • 5,724
  • 3
  • 34
  • 31