2016-11-17 97 views
4

我想要做的是使用蝦生成一個PDF,同時具有一些語言特定的字符。如何更改對蝦的字體

而作爲一個結果,我發現了以下錯誤:

raise Prawn::Errors::IncompatibleStringEncoding, 
    "Your document includes text that's not compatible with the Windows-1252 character set.\n" \ 
    "If you need full UTF-8 support, use TTF fonts instead of PDF's built-in fonts\n." 

所以,我想通過這樣改變字體:

# app/models/prawn/change_font_decorator.rb 

Prawn::Document.generate("output.pdf") do 
    font_families.update("Arial" => { 
    :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 "Arial" 
end 

然而,我得到了同樣的錯誤時,試圖生成一個PDF文件。

關於如何解決這個問題的任何想法?

回答

15

prawn manual是一個很好的參考資料,幷包括字體使​​用的部分。 「外部字體」部分與您的問題特別相關。

下面是應該工作基本情況,雖然它不支持粗體和斜體:

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

要還用粗體和斜體,最好註冊一個字體系列不與一個衝突標準的PDF字體:

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 
+0

如何在表格單元中實現? –

0

如果您使用初始化建立你的PDF,你可以簡單地更新initialize方法您的字體系列,然後設置所需的字體。

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