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
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
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
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
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
> 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?)
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
If you think this could fix the problem, try coding it up and we'll
give it a shot.
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.
> 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)
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.
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
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.
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
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
Hello,I have a similar problem with you guys in rails i try to put the euro symbol in a tablepdf = 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"]
^