# Dealing with multiple encodings in the same app

**URL:** <https://www.ruby-forum.com/t/dealing-with-multiple-encodings-in-the-same-app/88131>\
**Category:** Ruby\
**Created:** [March 8, 2007, 12:00pm UTC](https://www.ruby-forum.com/t/dealing-with-multiple-encodings-in-the-same-app/88131 "2007-03-08T12:00:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Isak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ac91a4/32.png) [@Isak](https://www.ruby-forum.com/u/Isak)\
**Post date:** [March 8, 2007, 12:00pm UTC](https://www.ruby-forum.com/t/dealing-with-multiple-encodings-in-the-same-app/88131/1 "2007-03-08T12:00:31Z")

</div>

Our (rails) app is mainly utf-8, so we set kcode to u.

However, we need to generate pdfs using the cp1252 encoding, as the  
FPDF library doesn’t have unicode support.  
Translating strings work fine using iconv, but the library is using  
sprintf internally, which blows up on non-utf8 characters.

E.g.:

s = ("" \<\< 73 \<\< 241 \<\< 116 \<\< 234 \<\< 114 \<\< 110 \<\< 228 \<\< 116 \<\< 105  
\<\< 111 \<\< 110 \<\< 97 \<\< 108 \<\< 105 \<\< 122 \<\< 230 \<\< 116 \<\< 105 \<\< 248  
\<\< 110) # valid cp1252-string  
puts sprintf(“aa %s bb”, s)

…which discards the last two characters of s.

Suggestions appreciated,  
Isak

---

<div class="post-metadata">

**Author:** ![Isak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ac91a4/32.png) [@Isak](https://www.ruby-forum.com/u/Isak)\
**Post date:** [March 8, 2007, 1:15pm UTC](https://www.ruby-forum.com/t/dealing-with-multiple-encodings-in-the-same-app/88131/2 "2007-03-08T13:15:46Z")

</div>

On Mar 8, 11:58 am, “Isak” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Our (rails) app is mainly utf-8, so we set kcode to u.

> From Rails 1.2 on KCODE is always set the ‘u’, so you might not have  
> to set it anymore.

> However, we need to generate pdfs using the cp1252 encoding, as the  
> FPDF library doesn’t have unicode support.

Ok, so FPDF doesn’t support Unicode.

> Translating strings work fine using iconv, but the library is using  
> sprintf internally, which blows up on non-utf8 characters.

But you can only feed it utf-8 characters?

---

<div class="post-metadata">

**Author:** ![Isak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ac91a4/32.png) [@Isak](https://www.ruby-forum.com/u/Isak)\
**Post date:** [March 8, 2007, 3:26pm UTC](https://www.ruby-forum.com/t/dealing-with-multiple-encodings-in-the-same-app/88131/3 "2007-03-08T15:26:08Z")

</div>

On Mar 8, 1:13 pm, “Manfred Stienstra” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ok, so FPDF doesn’t support Unicode.  
> But you can only feed it utf-8 characters?

That’s the gist of it, at least when running ruby with kcode = ‘u’.

I replaced the 4 occurances of sprintf(“%X %s bbb”, X, txt) with  
sprintf("%X “, X) + txt + " bbb”, which fixed our issues.  
Reasonable approach, or is there a better way?

Isak
