【问题标题】:Why do PDFs generated with Prawn using the Noto Serif font have missing characters?为什么使用 Prawn 使用 Noto Serif 字体生成的 PDF 会缺少字符?
【发布时间】:2016-11-05 16:12:04
【问题描述】:

我正在尝试使用 Prawn 为 Rails 应用程序生成多种语言的 pdf。我尝试使用 Google 的 Noto Fonts,但我使用 Noto Serif 生成的 PDF 没有呈现所有字符(其中大部分是空白的)。但是当我使用 Droid Serif 时,PDF 正确呈现了所有字符(见下图)。

使用 Noto Serif 的最终 PDF

使用 Droid Serif 的最终 PDF

如果我没有为 Prawn 明确指定字体(即使用默认的 Prawn 字体,可能是 Helvetica),则 PDF 再次正常,没有丢失字符。使用 Noto Serif 时会发生什么?

最后,Noto Serif 支持多种语言,这就是我打算在我的应用程序中使用它的原因。我可以在 PDF 中使用任何其他 unicode 字体(替代品)来涵盖与 Noto Serif 相同的语言集吗?

UPDATE(生成PDF的相关代码):

# ... part of a big function
pdf = Prawn::Document.new

# set the correct font for the language
font = get_font(language)
pdf.font_families.update({font['name'] => {
  :normal => font['regular'],
  :italic => font['italic'],
  :bold => font['bold'],
  :bold_italic => font['bold_italic']
}})
pdf.font font['name'] rescue pdf.font('Helvetica')

# leave the first page empty for the table of contents
pdf.start_new_page

# get content and write to pdf
source_url = 'http://someurl/' + language
book.sections.each do |section|
  url = source_url + section.url + '?raw'
  doc = Nokogiri::HTML(open(url).read, nil, 'utf-8')
  doc.xpath('//body/*').each do |node|
    if node.name == 'h1'
      pdf.font_size = 20
    elsif node.name == 'h3'
      pdf.font_size = 16
      pdf.move_down 10
    else
      pdf.font_size = 12
    end
    pdf.move_down 10
    pdf.text node.content
  end
  pdf.start_new_page
end

# ... some more code that generates table of contents and references

return pdf.render

get_font(language) for language = 'en' 返回一个映射(对于 Noto Serif)如下:

{
    'name' => 'NotoSerif',
    'path' => Rails.root.join('fonts', 'NotoSerif-unhinted'),
    'regular' => Rails.root.join('fonts', 'NotoSerif-unhinted', 'NotoSerif-Regular.ttf').to_s,
    'bold' => Rails.root.join('fonts', 'NotoSerif-unhinted', 'NotoSerif-Bold.ttf').to_s,
    'italic' => Rails.root.join('fonts', 'NotoSerif-unhinted', 'NotoSerif-Italic.ttf').to_s,
    'bold_italic' => Rails.root.join('fonts', 'NotoSerif-unhinted', 'NotoSerif-BoldItalic.ttf').to_s
}

【问题讨论】:

  • 您可以发布您要创建的文档的代码吗?
  • 我认为您应该检查 Noto 字体在 PDF 中嵌入(和子集)方面的版权和权限。并非所有字体都允许这些东西。
  • 你解决了这个问题吗?我在使用 Roboto 时遇到了同样的问题
  • @Maragues 不,我没有。我正在使用不同的字体。
  • 谢谢,我最终使用了 DejaVu

标签: ruby-on-rails pdf unicode fonts prawn


【解决方案1】:

如果有人偶然发现它。

出现此错误是因为某些字体使用了高级 TTF 功能,而旧版本的 prawn 不支持它。

解决方案是升级到更新的 Prawn 版本,或者从这个错误报告中向后移植修复:https://github.com/prawnpdf/prawn/issues/972

【讨论】:

    猜你喜欢
    • 2015-09-26
    • 2021-05-14
    • 2017-01-11
    • 1970-01-01
    • 2010-11-18
    • 2010-12-27
    • 1970-01-01
    • 2015-01-18
    • 1970-01-01
    相关资源
    最近更新 更多