简体   繁体   English

在OS X上如何将Arial字体与Prawn一起使用?

[英]How do I use the Arial font with Prawn on OS X?

I'm trying to use ruby to generate a PDF using Prawn on OS X. I have the following: 我正在尝试使用ruby在OS X上使用Prawn生成PDF。我具有以下内容:

font 'Arial'

Arial is installed on my Mac. Arial已安装在我的Mac上。 But when I try to generate the PDF, I get the following error: 但是,当我尝试生成PDF时,出现以下错误:

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

How can I get this common font to work in Prawn? 我怎样才能使这种通用字体在Prawn中工作? In fact, almost anything other than Helvetica or Times New Roman throws this same error. 实际上,除了Helvetica或Times New Roman以外,几乎所有其他东西都会引发此错误。 This is part of a Rails 3.2 app. 这是Rails 3.2应用程序的一部分。

If I try to load the font ttf file directly, per Ashish's suggestion below, I get a Bad font family message: 如果我尝试按照以下Ashish的建议直接加载字体ttf文件,则会收到Bad font family消息:

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'

If you're using the :style option to calls to text , eg 如果您使用:style选项来调用text ,例如

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, eg this: 然后,您当时使用的字体需要具有关联的字体系列,否则,您将看到“ Bad font family”错误,例如:

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

produces: Bad font family (RuntimeError) . 产生: 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, eg 一种解决方法是每次更改样式时始终指定确切的字体文件,例如

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. 设置字体系列后,您可以仅使用Arial作为字体名称,并且可以轻松使用不同的样式。

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. 事实证明,Rails.root.join不会返回显式的String对象。 The solution is to add to_s at the end of the expression. 解决方案是在表达式的末尾添加to_s。

@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 由TheR

try passing full path of arial.ttf to the font function as below - 尝试将arial.ttf的完整路径传递给字体函数,如下所示-

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 大虾文档-http: //rubydoc.info/gems/prawn/0.12.0/frames

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM