大虾PDF在同一个PDF中同时支持英文和Japenese/Chinese/Thai/Korean

Prawn PDF support both English and Japenese/Chinese/Thai/Korean in same PDF

我正在使用 ROR 和 Prawn 生成 PDF。 PDF 有英文(表格标签)和日文或其他 FE 语言(用户输入的数据)。

我在建议 ipamp.ttf 的地方找到了与此相关的问题。我安装了那个字体,它打印的日语很漂亮。问题是,它不支持英文!我两个都需要。

以防万一,以下是我安装 ipamp 的方式。我刚刚在初始化方法中调用了字体:

font("lib/assets/ipamp.ttf")

我找到了Google的Noto字体,但是是ttc格式,大虾不能用

我正在寻找一种在一个 PDF 文档中同时支持所有欧盟和远东语言的解决方案,没有一堆逻辑来确定我要显示的文本是否是 EU/Latin或远东(并根据它切换字体)。好像那会很脆。

字体文件的大小不是问题,因为 PDF 将在服务器上呈现并作为 PDF 发送到客户端。

谢谢!

这是根据 post (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