我正在尝试使用Prawn for Rails应用程序生成多种语言的pdf。我尝试使用Google的Noto Fonts但我使用Noto Serif生成的PDF没有渲染所有字符(大多数都是空白的)。但是当我使用Droid Serif时,PDF正确渲染了所有角色(见下图)。
如果我没有明确指定Prawn的字体(即使用默认的Prawn字体,可能是Helvetica),PDF再次正常,没有丢失的字符。使用Noto Serif时发生了什么?
最后,Noto Serif支持一系列语言,这就是为什么我打算将它用于我的应用程序。是否还有其他unicode字体(替代品),我可以在PDF中使用,涵盖Noto Serif所使用的同一组语言?
更新(生成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
<{1}} get_font(language)
为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
}