How do I change font in Prawn

The prawn manual is an excellent reference, and includes sections on font usage. The "External Fonts" section is particularly relevant to your issue.

Here's a basic case that should work, although it doesn't support bold and italic:

Prawn::Document.generate("output.pdf") do
  font Rails.root.join("app/assets/fonts/OpenSans-Regular.ttf")
  text "Euro €"
end

To also use bold and italic, it's best to register a font family that doesn't conflict with one of the standard PDF fonts:

Prawn::Document.generate("output.pdf") do
  font_families.update("OpenSans" => {
    :normal => Rails.root.join("app/assets/fonts/OpenSans-Regular.ttf"),
    :italic => Rails.root.join("app/assets/fonts/OpenSans-Regular.ttf"),
    :bold => Rails.root.join("app/assets/fonts/OpenSans-Regular.ttf"),
    :bold_italic => Rails.root.join("app/assets/fonts/OpenSans-Regular.ttf")
  })
  font "OpenSans"
  text "Euro €"
end

Where do I put the above code?

If you are inheriting from Prawn::Document you can try out the following:

class SpreeInvoicePdf < Prawn::Document
  require 'prawn'

  def initialize(quote, line_items)
    self.font_families.update("OpenSans" => {
                                :normal => Rails.root.join("vendor/assets/fonts/Open_Sans/OpenSans-Regular.ttf"),
                                :italic => Rails.root.join("vendor/assets/fonts/Open_Sans/OpenSans-Italic.ttf"),
                                :bold => Rails.root.join("vendor/assets/fonts/Open_Sans/OpenSans-Bold.ttf"),
                                :bold_italic => Rails.root.join("vendor/assets/fonts/Open_Sans/OpenSans-BoldItalic.ttf")
    })

    font "OpenSans"

  # etc.

You will of course need to go to Google fonts and download the fonts and place it in the vendor/assets/fonts/Open_Sans/ directory.


If you're building your PDF using initialize, you can simply update your font families in the initialize method and then set the desired font.

class InvoicePdf < Prawn::Document

  def initialize()
    super()
    self.font_families.update("DejaVuSans" => {:normal => "#{Rails.root}/public/DejaVuSans.ttf"})
    font "DejaVuSans"
    business_logo
    invoice_items
    footer
  end

  def business_logo
    ##stuff here
  end

end