本文介绍了Ruby-如何在Prawn中使用不同的字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小的Ruby程序,其中使用Prawn将一些文本打印到PDF中,但是一小部分文本是非英文字符. (其中一些文本是中文,一些是希腊文,等等).当我运行程序时,我当然会收到错误消息Your document includes text that's not compatible with the Windows-1252 character set. (Prawn::Errors::IncompatibleStringEncoding)If you need full UTF-8 support, use TTF fonts instead of PDF's built-in fonts.我知道我需要使用TTF字体,但是我该怎么办?我需要从网上安装吗?如果是这样,我将其保存到哪里?我知道这可能是一个愚蠢的问题,但是我是Ruby和Prawn的新手.谢谢!

I have a small Ruby program where I'm printing some text out to a PDF using Prawn, but a small portion of the text is non-English characters. (Some of that text is Chinese, some is Greek, etc.). When I run my program, I of course get an error saying Your document includes text that's not compatible with the Windows-1252 character set. (Prawn::Errors::IncompatibleStringEncoding)If you need full UTF-8 support, use TTF fonts instead of PDF's built-in fonts. I know that I need to use a TTF font, but how do I even go about that? Do I need to install it from online? If so, where would I save it to? I know that's probably a dumb question but I'm new to Ruby and Prawn. Thanks!

推荐答案

ttf是一种常见格式,您可以从 Google字体,将字体放在项目中的某个目录中,例如/assets/fonts/

ttf is a common format, you can download fonts at Google font for instance, put the font in some directory in your project for instance under /assets/fonts/

然后您可以像这样定义新的字体系列:

You can then define a new font family like so:

Prawn::Document.generate("output.pdf") do
  font_families.update("Arial" => {
    :normal => "/assets/fonts/Arial.ttf",
    :italic => "/assets/fonts/Arial Italic.ttf",
  })
  font "Arial"
end

然后您可以在整个文档中使用该字体.

You can then use the font throughout your document.

这篇关于Ruby-如何在Prawn中使用不同的字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 05:00