Prawn & Prawnto Rails PDF generation - UTF-8?

允我心安 提交于 2019-12-21 04:21:14

问题


I'm using ruby, prawn, and prawnto to dynamically generate pdf's containing text in other languages. I can't seem to get any text in languages with non-english characters to show up. It doesn't throw any errors...just shows a bunch of dashes instead of characters. Prawn brags on its homepage about UTF-8 support so I don't see why this is a problem. I'm using ruby 1.8.6 (engineyard).


回答1:


For Unicode to work you need to load a TTF font that has the characters you require.

The default Helvetica font only supports ASCII (plus a few extras).




回答2:


Here is a example of prawn with unicode. Download the font from here. http://www.siyabas.lk/files/iskpota.ttf

#!/bin/env ruby
# encoding: utf-8

require 'prawn'

pdf = Prawn::Document.new

pdf.font_families.update("Iskoola Potha Unicode"=>{:normal =>"fonts/iskpota.ttf"})
pdf.font "Iskoola Potha Unicode"

pdf.text "යුනිකෝඩ් වනාහි …"

pdf.move_down 10
pdf.text "
පරිගණක මූලිකව අංක මගින් එහි කටයුතු සිදු කරයි. 
ඒවා වචන හා අක්‍ෂරවලට නොයෙකුත් අංක නියම කොට ඒ අංක ගබඩා කිරීම මගින් වචන 
හා අකුරුද ගබඩා කර ගනී. යුනිකෝඩ් ක්‍රමය සොයා ගැනීමට පෙර මෙසේ අකුරුවලට අංක 
නියම කිරීමට කේතන පද්ධති ඉතා විශාල ගණනක් පැවතුනි. උදාහරණයක් ලෙස යුරෝපීය 
සංගමයට පමණක් ඔවුන්ගේ භාෂා කේතන ක්‍රම රාශියක් අවශ්‍ය විය. එසේම එක් භාෂාවක් වන 
ඉංග්‍රීසි භාෂාව සඳහා එහි සියලු අක්‍ෂර, විරාම ලකුණු සහ තාක්‍ෂණික සංකේත සඳහා එක් 
කේතන ක්‍රමයක් නොසෑහුනි. තවද මෙම කේනත ක්‍රම එකිනෙක හා ගැටුනි. එනම් වෙනස් 
කේතන ක්‍රම 2කට එකම අංකය වෙනස් අක්‍ෂර දෙකකට හෝ වෙනස් අංක දෙකක් එකම 
අක්‍ෂරයකට යෙදිය හැක. වෙනස් කේතන ක්‍රම භාවිතයේදී ඕනෑම පරිගණකයක් 
(විශේෂයෙන්ම සර්වර් පරිගණක) වෙනස් කේතන ක්‍රම කීපයක් සමග ක්‍රියා කළ යුතු වේ; 
එහෙයින් දත්ත වෙනස් කේතන ක්‍රම හෝ පරිගණක පද්ධති හරහා ගමන් ගැනීමේදී කේතන 
ක්‍රමවල වෙනස හේතුවෙන් එමදත්ත විනාශ වීමට හෝ අපවිත්‍ර වීමට ඉඩ ඇත."

pdf.stroke_horizontal_rule

pdf.render_file "sinhala.pdf"


来源:https://stackoverflow.com/questions/2104814/prawn-prawnto-rails-pdf-generation-utf-8

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