Utf8 string in ruby 1.8.7?

1,181 views
Skip to first unread message

Brian Jensen

unread,
Aug 11, 2008, 7:52:07 AM8/11/08
to Prawn
So Prawn requires a string to be a string to be utf8 encoded, how do I
do convert a non utf8 string to utf8 in ruby 1.8.7? I found the gem
character-encodings however it wants to compile from scratch which I
cant do on my windows server.

Ideas?

Gregory Brown

unread,
Aug 11, 2008, 8:53:25 AM8/11/08
to prawn...@googlegroups.com

What encoding are you using? ASCII is a subset of UTF8 so it does not
need to be converted on Ruby 1.8, and is automatically converted on
1.9.

Also... please note that Ruby 1.8.7 is not (and will not be) support.
1.8.6 and 1.9.0_3 are the currently supported versions of Ruby for
Prawn.

-greg

--
Technical Blaag at: http://blog.majesticseacreature.com | Non-tech
stuff at: http://metametta.blogspot.com

Brian Jensen

unread,
Aug 11, 2008, 9:06:14 AM8/11/08
to Prawn
Latin1 in the rails database:

foo = "Hello Scandinavians: Ø Å Æ ø æ å" (retrieved from database)

pdf.text foo

This will cause the utf invalid encoding error.

This is with 1.8.6 now btw.

Gregory Brown

unread,
Aug 11, 2008, 9:13:31 AM8/11/08
to prawn...@googlegroups.com
On Mon, Aug 11, 2008 at 9:06 AM, Brian Jensen <bri...@daimi.au.dk> wrote:
>
> Latin1 in the rails database:
>
> foo = "Hello Scandinavians: Ø Å Æ ø æ å" (retrieved from database)
>
> pdf.text foo
>
> This will cause the utf invalid encoding error.

Oh, right. Are you using the builtin fonts? If so, the support for
ISO 8859-1 encoding[0] is broken. We have this fixed in master on
sandal/prawn, but I'll cut a 0.1.2 release with the changes in a
couple hours.

If you get a chance, please try the code in edge and see if it works
for you. If not, please try the release when it is ready and see if
it fixes your problem. I'll drop a note here when it is ready

-greg

[0] http://www.ramsch.org/martin/uni/fmi-hp/iso8859-1.html

Brian Jensen

unread,
Aug 11, 2008, 9:17:47 AM8/11/08
to Prawn
Its the builtin font yes. Ill try with the 0.1.2 release.

James Healy

unread,
Aug 11, 2008, 8:44:16 AM8/11/08
to prawn...@googlegroups.com
Gregory Brown wrote:
> Also... please note that Ruby 1.8.7 is not (and will not be) support.
> 1.8.6 and 1.9.0_3 are the currently supported versions of Ruby for
> Prawn.

That said, I run 1.8.7 on one of my boxes, and I haven't run into any
issues when running the prawn specs on it.

-- James Healy <jimmy-at-deefa-dot-com> Mon, 11 Aug 2008 22:43:35 +1000

James Healy

unread,
Aug 11, 2008, 8:48:31 AM8/11/08
to prawn...@googlegroups.com
Brian Jensen wrote:
> Its the builtin font yes. Ill try with the 0.1.2 release.

In the meantime, you can try converting the string to utf-8 and loading
a truetype font. Almost any truetype font you can find on your system
should support the basic iso-8859-1 character set.

require 'iconv'
require 'prawn'

@iconv = Iconv.new('ISO-8859-1', 'utf-8')

Prawn::Document.generate("text.pdf") do |pdf|
pdf.font "some_font.ttf"
pdf.text @iconv.iconv("a latin1 string from your db")
end

-- James Healy <jimmy-at-deefa-dot-com> Mon, 11 Aug 2008 22:44:22 +1000

Brian Jensen

unread,
Aug 11, 2008, 9:48:25 AM8/11/08
to Prawn
Only had 2 minuttes so I had to wrap this up in a hurry, dunno if this
helps or not:

require 'rubygems'
require 'iconv'
require 'Prawn'

@iconv = Iconv.new('ISO-8859-1', 'utf-8')

Prawn::Document.generate("text.pdf") do |pdf|
pdf.font "c:/windows/fonts/times.ttf"
pdf.text @iconv.iconv("hello:æøå ÅØÆ")
end


OUTPUT:

C:\Projects>ruby test.rb
test.rb:9:in `iconv': "\346\370\345
\305\330\306" (Iconv::IllegalSequence)
from test.rb:9
from c:/ruby/lib/ruby/gems/1.8/gems/prawn-0.1.1/lib/prawn/
document.rb:47
:in `generate'
from test.rb:7

C:\Projects>ruby --version
ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]

James Healy

unread,
Aug 11, 2008, 9:23:15 AM8/11/08
to prawn...@googlegroups.com
Brian Jensen wrote:
> Only had 2 minuttes so I had to wrap this up in a hurry, dunno if this
> helps or not:

My bad, I got the order of arguments to the iconv constructor wrong. It
should be iconv = Iconv.new(to_encoding, from_encoding). In your case
you want to go from iso-8859-1 to utf-8.

The API docs for iconv are available at ruby-doc:
http://ruby-doc.org/stdlib/

-- James Healy <jimmy-at-deefa-dot-com> Mon, 11 Aug 2008 23:20:24 +1000

Brian Jensen

unread,
Aug 12, 2008, 3:31:53 AM8/12/08
to Prawn
Hello James.


The code works.
Reply all
Reply to author
Forward
0 new messages