我正在使用 ROR 和 Prawn 生成 PDF。 PDF 有英文(表格标签)和日文或其他 FE 语言(用户输入的数据)。
我在建议 ipamp.ttf 的地方找到了与此相关的问题。我安装了该字体,它的日语打印效果很好。问题是,它不支持英文!我两个都需要。
如果重要,这是我安装 ipamp 的方法。我只是在 initialize 方法中调用了 font:
font("lib/assets/ipamp.ttf")
我找到了 Google 的 Noto 字体,但它们是 ttc 格式,Prawn 无法使用。
我正在寻找一种解决方案来同时支持所有欧盟和远东语言,在一个 PDF 文档中,没有一堆逻辑来确定我尝试显示的文本是欧盟/拉丁语还是远东(并切换字体基于那个)。好像那样会很脆。
字体文件的大小不会成为问题,因为 PDF 将在服务器上呈现并作为 PDF 发送到客户端。
谢谢!
最佳答案
这是解决方案,从这篇文章( this post )和一些试验和错误拼凑而成。
关键是 Prawn 支持后备字体。您必须将它们下载到您的项目中,然后更新 Prawn 的字体系列以包含它们,然后包含一个名为“fallback_fonts”的方法,当 Prawn 意识到它有一个它不知道如何渲染的 unicode 字符时,可以使用它。
class ResultsPdf < Prawn::Document
def initialize(device)
super()
@device = device
set_fallback_fonts
page_title #typically English
persons_name #typically Japanese
end
def set_fallback_fonts
ipamp_path = "lib/assets/ipamp.ttf"
ipamp_spec = { file: ipamp_path, font: 'IPAPMincho'}
#don't use a symbol to define the name of the font!
font_families.update("ipamp" => {normal: ipamp_spec,
bold: ipamp_spec,
italic: ipamp_spec,
bold_italic: ipamp_spec})
end
def fallback_fonts
["ipamp"]
end
def page_title
# device name is typically in English
text "#{@device.name}", :size => 15, :style => :bold, :align => :center
end
def persons_name
# owner name can be in any language, including Japanese
# you don't have to specify :fallback_font here. Prawn will use your method.
text "Name: #{@device.owner_last_name}"
end
end
关于ruby-on-rails - Prawn PDF 在同一个 PDF 中同时支持英文和日文/中文/泰文/韩文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36559710/