Prawn:以PDF格式打印Unicode字符串 [英] Prawn: Print unicode string in PDF

查看:222
本文介绍了Prawn:以PDF格式打印Unicode字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Prawn在Rails 3应用程序中生成PDF。



是否可以像HTML视图一样将Unicode字符串打印成PDF? / p>

例如,

 <%= rawunicode_for_& x0F40; %> 

show.html.erb 字形&#x0F40;

  pdf.text rawunicode_for_&#x0F40; 

show.pdf.prawn 字符串unicode_for_&#x0F40;



尝试了 show.pdf.prawn

  pdf.font#{Prawn :: BASEDIR} / data / fonts / TibMachUni- 1.901b.ttfdo 
pdf.text rawunicode_for_&#x0F40;
end

  pdf.font_families.update(TibMachUni=> {:normal =>#{Prawn :: BASEDIR} /data/fonts/TibMachUni-1.901b.ttf})
pdf.font(TibMachUni)do
pdf.text rawunicode_for_&#x0F40;
end

这没有解决问题。

解决方案

您应该使用

  pdf.text rawunicode_for_\\ \\ u0F40

而不是

  pdf.text rawunicode_for_&#x0F40; 

Ruby转义unicode字符的方式是 \uXXXX



&#x0F40; 转义是HTML / XML转义代码,第一个视图生成了一个HTML文件。


I'm using Prawn to generate PDFs in a Rails 3 app.

Is it possible to print a Unicode string into a PDF like in a HTML view?

For example,

<%= raw "unicode_for_&#x0F40;" %>

in show.html.erb results in the glyph ཀ while

pdf.text raw "unicode_for_&#x0F40;"

in show.pdf.prawn results in the string "unicode_for_&#x0F40;"

Tried in show.pdf.prawn:

    pdf.font "#{Prawn::BASEDIR}/data/fonts/TibMachUni-1.901b.ttf" do
      pdf.text raw "unicode_for_&#x0F40;"
    end

and

    pdf.font_families.update("TibMachUni" => {:normal => "#{Prawn::BASEDIR}/data/fonts/TibMachUni-1.901b.ttf" })
    pdf.font("TibMachUni") do 
      pdf.text raw "unicode_for_&#x0F40;"
    end

This did not solve the problem.

解决方案

You should use

pdf.text raw "unicode_for_\u0F40"

instead of

pdf.text raw "unicode_for_&#x0F40;"

The Ruby way to escape unicode characters is \uXXXX.

The &#x0F40; escape is an HTML/XML escape code and works only because the first view generated an HTML file.

这篇关于Prawn:以PDF格式打印Unicode字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆