Does Prawn gem support Arabic Language?

与世无争的帅哥 提交于 2019-12-11 04:36:11

问题


I'll create a pdf report in Arabic Language, but the letters is separated and in reverse order, I use the following code to generate a pdf file

class InstitutesPdf < Prawn::Document
  def initialize(institute)
    super()
    font(Rails.root.join("app/assets/fonts/arial.ttf"))
    @institute = institute
    text "Institute ID : #{@institute.id}"
    move_down(30)
    text "Institute name : #{@institute.name.mb_chars.to_s}"
  end
end

when I change the last line to

text "Institute name : #{@institute.name.mb_chars.reverse.to_s}"

the word letters shows in proper order but still separated

I use rails 4 and prawn version '1.1.0'


回答1:


I had developed a gem to fix this problem particularly to be used with prawn that was refactored by Sinan into this project:

https://github.com/staii/arabic-letter-connector

So in console:

gem install arabic-letter-connector

Then in code:

require 'prawn'
require 'arabic-letter-connector'
Prawn::Document.generate("arabic.pdf") do
  text_direction :rtl
  font("/path/to/arabic/font.ttf") do # For example: http://www.amirifont.org/
    text "مرحبا يا العالم".connect_arabic_letters
  end
end

As you have mixed text and might not want to use :rtl, a workaround would be to reverse the string yourself before printing into the pdf.




回答2:


I had the same issue, I was trying to print Arabic words via RMagick

I did it by using connect_arabic_letters to connect them, and then used .reverse to reverse the characters (You might not have to do it)

FYI: This doesn't work well if the words are decorated (Tashkeel/Harakat)



来源:https://stackoverflow.com/questions/24651908/does-prawn-gem-support-arabic-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!