Euro symbol

299 views
Skip to first unread message

gummybears

unread,
Sep 26, 2008, 2:47:50 AM9/26/08
to Prawn
Is it possible to output the euro symbol with prawn ?

In RFPDF I can do something like

formatted_amount = sprintf("%s %6.2f",128.chr ,amount)

but not with prawn.

TIA

James Healy

unread,
Sep 26, 2008, 2:57:47 AM9/26/08
to prawn...@googlegroups.com

I suspect you should be able to use the Euro symbol with a TTF font, but
not the builtin ones. Prawn's support for non-ASCII text with built-in
font's is a little 'wonky'.

You will need to specify the symbol in utf-8, not by using 128.chr:

formatted_amount = sprintf("€ %6.2f", amount)

or if that has issues, specify the raw utf-8 bytes for the euro:

formatted_amount = sprintf("\xE2\x82\xAC %6.2f", amount)

-- James Healy <jimmy-at-deefa-dot-com> Fri, 26 Sep 2008 16:50:22 +1000

gummybears

unread,
Sep 26, 2008, 3:07:05 AM9/26/08
to Prawn


On 26 sep, 08:57, James Healy <ji...@deefa.com> wrote:
> gummybears wrote:
> > Is it possible to output the euro symbol with prawn ?
>
> > In RFPDF I can do something like
>
> > formatted_amount = sprintf("%s %6.2f",128.chr ,amount)
>
> > but not with prawn.
>
> I suspect you should be able to use the Euro symbol with a TTF font, but
> not the builtin ones. Prawn's support for non-ASCII text with built-in
> font's is a little 'wonky'.
>
> You will need to specify the symbol in utf-8, not by using 128.chr:
>
> formatted_amount = sprintf("€ %6.2f", amount)
>
This does not work, I am getting the following error

...../prawn-0.2.0/lib/prawn/font.rb:207:in
`normalize_builtin_encoding': When using a builtin
font, only characters that exist in WinAnsi/ISO-8859-1 are allowed.
(Prawn::Errors::IncompatibleStringEncoding)
> or if that has issues, specify the raw utf-8 bytes for the euro:
>
> formatted_amount = sprintf("\xE2\x82\xAC %6.2f", amount)
>
This just prints EUR ..., instead of € ...
the latter I need

James Healy

unread,
Sep 26, 2008, 3:12:05 AM9/26/08
to prawn...@googlegroups.com
gummybears wrote:
> This does not work, I am getting the following error
>
> ...../prawn-0.2.0/lib/prawn/font.rb:207:in
> `normalize_builtin_encoding': When using a builtin
> font, only characters that exist in WinAnsi/ISO-8859-1 are allowed.
> (Prawn::Errors::IncompatibleStringEncoding)
> > or if that has issues, specify the raw utf-8 bytes for the euro:

You need to load a TTF font first. If you don't load one, prawn will be
using what it calls "built in" fonts, which have trouble rendering
anything other than ASCII.

Prawn::Document.generate("euro.pdf") do
font "#{Prawn::BASEDIR}/data/fonts/DejaVuSans.ttf"
text sprintf("€ %6.2f", amount)
end

You don't have to use one of the TTF fonts distributed with prawn, any
you can find on your system *should* work fine.

-- James Healy <jimmy-at-deefa-dot-com> Fri, 26 Sep 2008 17:08:18 +1000

Gregory Brown

unread,
Sep 26, 2008, 12:20:37 PM9/26/08
to prawn...@googlegroups.com
On Fri, Sep 26, 2008 at 3:12 AM, James Healy <ji...@deefa.com> wrote:
>
> gummybears wrote:
>> This does not work, I am getting the following error
>>
>> ...../prawn-0.2.0/lib/prawn/font.rb:207:in
>> `normalize_builtin_encoding': When using a builtin
>> font, only characters that exist in WinAnsi/ISO-8859-1 are allowed.
>> (Prawn::Errors::IncompatibleStringEncoding)
>> > or if that has issues, specify the raw utf-8 bytes for the euro:
>
> You need to load a TTF font first. If you don't load one, prawn will be
> using what it calls "built in" fonts, which have trouble rendering
> anything other than ASCII.

Is there a way to support Euro in built-in fonts?

-greg


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

James Healy

unread,
Sep 28, 2008, 4:27:10 AM9/28/08
to prawn...@googlegroups.com
Gregory Brown wrote:
> > You need to load a TTF font first. If you don't load one, prawn will be
> > using what it calls "built in" fonts, which have trouble rendering
> > anything other than ASCII.
>
> Is there a way to support Euro in built-in fonts?

From the PDF spec POV, definitely. However I think the API we have in
prawn doesn't currently allow it.

The builtin encoding we use in prawn (WinAnsiEncoding) has a euro symbol
at byte 0x80. In theory if we pass a utf-8 Euro into Document#text(), it
should do the conversion to 0x80 automatically.

I'm not sure why it gets rendered to the PDF as EUR. The euro didn't
exist when ISO-8859-1 first appeared, so maybe Adobe placing it at 0x80
is non-standard, and iconv handles it differently.

Maybe this is an argument for doing the utf-8 -> WinAnsiEncoding
onversion ourselves instead of loading iconv?

-- James Healy <jimmy-at-deefa-dot-com> Sun, 28 Sep 2008 18:18:37 +1000

James Healy

unread,
Sep 28, 2008, 6:38:32 AM9/28/08
to prawn...@googlegroups.com
James Healy wrote:
> The builtin encoding we use in prawn (WinAnsiEncoding) has a euro symbol
> at byte 0x80. In theory if we pass a utf-8 Euro into Document#text(), it
> should do the conversion to 0x80 automatically.
>
> I'm not sure why it gets rendered to the PDF as EUR. The euro didn't
> exist when ISO-8859-1 first appeared, so maybe Adobe placing it at 0x80
> is non-standard, and iconv handles it differently.

Yup, it's the TRANSLIT option we pass to iconv that's converting € to
EUR, presumably because there's no euro sign in ISO-8859-1.

> Maybe this is an argument for doing the utf-8 -> WinAnsiEncoding
> onversion ourselves instead of loading iconv?

I had a crack at getting this working:
http://github.com/yob/prawn/tree/winansi

No spec changes, just more accurate conversion from UTF-8 to
WinAnsiEncoding.

One outstanding issue is that we're using ISO-8859-1 AFM files to
calculate glyph metrics, but it seems there are a few characters that
are different between the 2 encodings. For those characters (like the
euro), the glyph metrics we pull from the AFM may be incorrect.

-- James Healy <jimmy-at-deefa-dot-com> Sun, 28 Sep 2008 20:08:22 +1000

Gregory Brown

unread,
Sep 28, 2008, 1:42:37 PM9/28/08
to prawn...@googlegroups.com
On Sun, Sep 28, 2008 at 6:38 AM, James Healy <ji...@deefa.com> wrote:

> I had a crack at getting this working:
> http://github.com/yob/prawn/tree/winansi

Cool, I'll give this a try soon, still on vacation...

> No spec changes, just more accurate conversion from UTF-8 to
> WinAnsiEncoding.
>
> One outstanding issue is that we're using ISO-8859-1 AFM files to
> calculate glyph metrics, but it seems there are a few characters that
> are different between the 2 encodings. For those characters (like the
> euro), the glyph metrics we pull from the AFM may be incorrect.

Have you hunted down more appropriate AFM files (do they exist?)

James Healy

unread,
Sep 28, 2008, 7:54:22 PM9/28/08
to prawn...@googlegroups.com
Gregory Brown wrote:
> > One outstanding issue is that we're using ISO-8859-1 AFM files to
> > calculate glyph metrics, but it seems there are a few characters that
> > are different between the 2 encodings. For those characters (like the
> > euro), the glyph metrics we pull from the AFM may be incorrect.
>
> Have you hunted down more appropriate AFM files (do they exist?)

The AFM files we have already contain glyph names ('A','a','Euro', etc).
Maybe it would be better to lookup glyph metrics using those?

-- James Healy <jimmy-at-deefa-dot-com> Mon, 29 Sep 2008 09:52:16 +1000

Gregory Brown

unread,
Sep 30, 2008, 2:27:48 PM9/30/08
to prawn...@googlegroups.com
On Sun, Sep 28, 2008 at 7:54 PM, James Healy <ji...@deefa.com> wrote:
>
> Gregory Brown wrote:
>> > One outstanding issue is that we're using ISO-8859-1 AFM files to
>> > calculate glyph metrics, but it seems there are a few characters that
>> > are different between the 2 encodings. For those characters (like the
>> > euro), the glyph metrics we pull from the AFM may be incorrect.
>>
>> Have you hunted down more appropriate AFM files (do they exist?)
>
> The AFM files we have already contain glyph names ('A','a','Euro', etc).
> Maybe it would be better to lookup glyph metrics using those?

If you think this could fix the problem, try coding it up and we'll
give it a shot.

Edwin V.

unread,
Nov 5, 2008, 7:37:31 AM11/5/08
to Prawn
Has anyone had a look at this problems yet? We need to render Euro
signs on bills and using TTF fonts isn't really an option because of
the enormous file size when used in the PDF. A PDF with the fonts
attached is 800Kb, a PDF using internal fonts only 27Kb. We now take
the EUR text for granted, but we would like the real € sign.

Thanks,

Edwin

Damian Janowski

unread,
Nov 5, 2008, 9:08:43 AM11/5/08
to prawn...@googlegroups.com
On Wed, Nov 5, 2008 at 10:37 AM, Edwin V. <flydes...@gmail.com> wrote:
> Has anyone had a look at this problems yet? We need to render Euro
> signs on bills and using TTF fonts isn't really an option because of
> the enormous file size when used in the PDF. A PDF with the fonts
> attached is 800Kb, a PDF using internal fonts only 27Kb. We now take
> the EUR text for granted, but we would like the real € sign.

Why is the TTF version 800 kb? Have you tried it lately?

There was a bug that caused TTF fonts to be embedded *on each page* so
if your PDF had many pages the file size was growing linearly with the
amount of pages (like size of font file * pages). This bug was fixed
by Gregory some time ago, so maybe you could give it a try.

Gregory Brown

unread,
Nov 5, 2008, 9:18:16 AM11/5/08
to prawn...@googlegroups.com
On Wed, Nov 5, 2008 at 9:08 AM, Damian Janowski
<damian....@gmail.com> wrote:

> Why is the TTF version 800 kb? Have you tried it lately?
>
> There was a bug that caused TTF fonts to be embedded *on each page* so
> if your PDF had many pages the file size was growing linearly with the
> amount of pages (like size of font file * pages). This bug was fixed
> by Gregory some time ago, so maybe you could give it a try.

But we still embed the entire TTF. So if the TTF file is large, it
will result in a large PDF (for now)

Gregory Brown

unread,
Nov 5, 2008, 9:20:59 AM11/5/08
to prawn...@googlegroups.com
On Wed, Nov 5, 2008 at 7:37 AM, Edwin V. <flydes...@gmail.com> wrote:
>
> Has anyone had a look at this problems yet? We need to render Euro
> signs on bills and using TTF fonts isn't really an option because of
> the enormous file size when used in the PDF. A PDF with the fonts
> attached is 800Kb, a PDF using internal fonts only 27Kb. We now take
> the EUR text for granted, but we would like the real € sign.

Please try cherry-picking this patch against sandal/master and see if
it works as expected.
http://github.com/yob/prawn/commit/699993b390ebf2b1d301612c08f3dbc1eced4449

If it does, report back, and I'll merge it.

thk....@gmail.com

unread,
Nov 20, 2008, 4:19:20 PM11/20/08
to Prawn
i tried to load a ttf font (downloaded here):
http://www.karmenweb.com/Tools/PDF/lib/fonts/

but it doesn't seem to work.

Prawn::Document.generate("euro.pdf") do
font "#{Prawn::BASEDIR}/data/fonts/verdanab.ttf"
font "#{Prawn::BASEDIR}/data/fonts/Verdana.ttf"
text sprintf("€ %6.2f", 100.10)
text "test", :style => :bold
end

at the line with :style i get an exception saying: Bad font family.

how do i solve that?

thomas

Gregory Brown

unread,
Nov 20, 2008, 4:22:58 PM11/20/08
to prawn...@googlegroups.com

If verndanab.ttf is your Bold style, and Verdana.ttf is your normal
font, you will need to register your font family before you can use
:style => :bold.

See the API documentation for Prawn::Document#font_families at:
http://prawn.majesticseacreature.com/docs

thk....@gmail.com

unread,
Nov 20, 2008, 4:56:30 PM11/20/08
to Prawn
> See the API documentation for Prawn::Document#font_families at:http://prawn.majesticseacreature.com/docs

thank you very much, working now

thomas

Gregory Brown

unread,
Nov 20, 2008, 4:59:50 PM11/20/08
to prawn...@googlegroups.com
On Thu, Nov 20, 2008 at 4:56 PM, thk....@gmail.com <thk....@gmail.com> wrote:
>
>> See the API documentation for Prawn::Document#font_families at:http://prawn.majesticseacreature.com/docs
>
> thank you very much, working now

Just as a note, you should be able to use Euro symbols with the adobe
fonts on edge Prawn at sandal/master.
Not sure if you were using the TTF as a workaround or if you actually
needed them.

Edwin V.

unread,
Dec 2, 2008, 6:07:37 AM12/2/08
to Prawn
Hi Gregory,

Sorry for the late reply, I didn't have the time to have a look at the
problem due to the release of some other large features of our
product.

The fix works perfectly for me, without any changes to my original
code. Looking forward seeing this in master (and a gem) soon.

Thanks,

Edwin

On 5 nov, 15:20, "Gregory Brown" <gregory.t.br...@gmail.com> wrote:
> On Wed, Nov 5, 2008 at 7:37 AM, Edwin V. <flydesign...@gmail.com> wrote:
>
> > Has anyone had a look at this problems yet? We need to render Euro
> > signs on bills and using TTF fonts isn't really an option because of
> > the enormous file size when used in the PDF. A PDF with the fonts
> > attached is 800Kb, a PDF using internal fonts only 27Kb. We now take
> > the EUR text for granted, but we would like the real € sign.
>
> Please try cherry-picking this patch against sandal/master and see if
> it works as expected.http://github.com/yob/prawn/commit/699993b390ebf2b1d301612c08f3dbc1ec...

Gregory Brown

unread,
Dec 2, 2008, 11:22:19 AM12/2/08
to prawn...@googlegroups.com
On Tue, Dec 2, 2008 at 6:07 AM, Edwin V. <flydes...@gmail.com> wrote:
>
> Hi Gregory,
>
> Sorry for the late reply, I didn't have the time to have a look at the
> problem due to the release of some other large features of our
> product.
>
> The fix works perfectly for me, without any changes to my original
> code. Looking forward seeing this in master (and a gem) soon.

Merged into sandal/prawn at master.
You'll see it in Prawn 0.3.0, due out as a gem on December 15th


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

"Ruby Best Practices" Book now in O'Reilly Roughcuts:
http://rubybestpractices.com

James Healy

unread,
Oct 15, 2012, 12:40:06 PM10/15/12
to prawn...@googlegroups.com

To include non-ascii characters in a ruby 1.9 file you must add the following to the first line of the file:

    # coding: utf-8

Cheers

James

On 15/10/2012 6:35 PM, "Razvan Ciocanel" <cho...@gmail.com> wrote:
Hello,

I have a similar problem with you guys in rails i try to put the euro symbol in a table

pdf = Prawn::Document.new
[...]

invoice_data
= [["Description", "Price", "Qty", "Total"]]

invoice_items
.map do |item|
  pdf
.font "#{Prawn::BASEDIR}/data/fonts/verdanab.ttf"
  pdf
.font "#{Prawn::BASEDIR}/data/fonts/Verdana.ttf"
  invoice_data
<< ["","€ 10",1,"€ 10"]
end


pdf
.table(invoice_data, :width => pdf.bounds.width,:cell_style => { :inline_format => true },:row_colors => ["FFFFFF", "DDDDDD"])

The problem is after i try to run this code i get this error and i couldn't get it to work otherwise...

invoices_pdf.rb:64: invalid multibyte char (US-ASCII)
invoices_pdf
.rb:64: invalid multibyte char (US-ASCII)
invoices_pdf
.rb:64: syntax error, unexpected $end, expecting ']'
  invoice_data
<< ["","€ 10",1,"€ 10"]
                         
^

Razvan Ciocanel

unread,
Oct 17, 2012, 6:16:11 AM10/17/12
to prawn...@googlegroups.com
Youhooo thanks a lot man. :D
That solved the issue i had.
Reply all
Reply to author
Forward
0 new messages