When I try to create a pdf in Arabic Language, I'm getting the letters in reverse order.
I use the following code to generate a pdf file.
note_section = {content: "#{order_item.try(:notes).try(:connect_arabic_letters)}" ,
size: 8,
font: "app/assets/fonts/ufonts.com_arial-unicode-ms.ttf",
text_color: "313131"}
Expected text is:
In prawn it looks like:
Gem version => Prawn version:2.2.2 Rails version:5.1.6
Any help is highly appreciated, thanks!
Related
(Rails 6.0.2.2, ruby 2.7.1, combine_pdf 1.0.18)
I'm currently trying to write some text to an existing PDF with the CombinePDF gem. Unfortunately I'm running in some encoding problems.
I'm loading the existing PDF:
pdf = CombinePDF.load "#{Rails.root}/public/pdf/base.pdf"
Then I'm adding text to it:
pdf.pages[0].textbox "Straße", height: 20, width: 160, y: 527, x: 215, font_size: 12, box_color: nil, text_align: :left, text_padding: 0
When generating a new pdf out of it:
send_data pdf.to_pdf, filename: "output.pdf", type: "application/pdf"
the string gets displayed as StraˆŸe, so the ß isn't displayed correctly.
I also tried to replace it with unicode literals (\xc3\x9f) without any effect.
Anybody has an idea what else to try?
If you use HexaPDF you could do something like this:
require 'hexapdf'
require 'stringio'
doc = HexaPDF::Document.open("#{Rails.root}/public/pdf/base.pdf")
doc.pages[0].canvas.font("Helvetica", size: 12).text("Straße", at: [215, 527])
outio = StringIO.new(''.b)
doc.write(outio)
Just make sure that you use a font that contains glyphs for all characters you want to use. So better use a TrueType font instead of the builtin Helvetica (you can just provide the path to a TrueType font to the #font method).
Below is my Prawn PDF file to generate a name on the PDF -
def initialize(opportunity_application)
pdf = Prawn::Document.new(:page_size => [1536, 2048], :page_layout => :landscape)
cell_1 = pdf.make_cell(content: "Eylül Çamcı".force_encoding('iso-8859-1').encode('utf-8'), borders: [], size: 66, :text_color => "000000", padding: [0,0,0,700], font: "app/assets/fonts/opensans.ttf")
t = pdf.make_table [[cell_1]]
t.draw
pdf.render_file "tmp/mos_certificates/application_test.pdf"
end
When rendering the name Eylül Çamcı which is Turkish, I get the following error -
Prawn::Errors::IncompatibleStringEncoding: Your document includes text that's not compatible with the Windows-1252 character set.
If you need full UTF-8 support, use TTF fonts instead of PDF's built-in fonts.
I'm already using a TTF font that supports the characters in that name, what can I do to print the name correctly?
It seams Turkish is missing in iso-8859-1.
On the other hand iso-8859-9 should work.
So you may try to change your code like (check the iso number that I changed):
...
cell_1 = pdf.make_cell(content: "Eylül Çamcı".force_encoding('iso-8859-9').encode('utf-8'), borders: [], size: 66, :text_color => "000000", padding: [0,0,0,700], font: "app/assets/fonts/opensans.ttf")
...
And a fun link which is not only related with character set but also other internalisation differences for Turkey.
Edit 1: I made a basic check, it seems the text is already in UTF-8. So why need to change to iso-8859 and come back to UTF-8?
Can you please try "Eylül Çamcı".force_encoding('utf-8') alone?
irb(main):013:0> "Eylül Çamcı".encoding
=> #<Encoding:UTF-8>
irb(main):014:0> "Eylül Çamcı".force_encoding('UTF-8')
=> "Eylül Çamcı"
irb(main):015:0>
Edit 2: Also can you check your font path? Both font exists and the path is proper?
#Rails.root.join('app/assets/fonts/opensans.ttf')
cell_1 = pdf.make_cell(content: "Eylül Çamcı".force_encoding('utf-8'), borders: [], size: 66, :text_color => "000000", padding: [0,0,0,700], font: Rails.root.join('app/assets/fonts/opensans.ttf'))
I'm not sure I remember how Prawn works, but PDF files don't support UTF-8, which is the default Ruby encoding for String objects.
In fact, PDF files only support ASCII encoding using internal fonts - any other encoding requires that you bring your own font (which is also recommended for portability).
The workaround is to either use character maps (CMaps) - either custom CMaps or pre-defined ones (BYO font).
Generally, PDF files include an embedded font (or a subset of a font), and a CMap, mapping the value of a byte (or, a number of bytes) to a desired font glyph. i.e. mapping 97, which is 'a' in ASCII, to the å glyph when using the specified font.
Last time I used Prawn, I think it supported TTF fonts and created font maps automatically using UTF-8 Strings for the text input - but you have to load an appropriate font into Prawn and remember to use it!.
You can see an example in this answer.
Good Luck!
EDIT
I updated the answer to reflect #mkl's comments.
#mkl pointed out that other encodings are supported or possible (BYO font), including predefined some multibyte encoding (which use pre-defined CMaps).
From this anwser about Force strings to UTF-8 from any encoding :
"Forcing" an encoding is easy, however it won't convert the characters
just change the encoding:
str = str.force_encoding("UTF-8")
str.encoding.name # => 'UTF-8'
If you want to perform a conversion,
use encode
Indeed, as #MehmetKaplan said:
It seams Turkish is missing in iso-8859-1.
On the other hand iso-8859-9 should work.
Therefore, you won't need the force_encodinganymore but just encode
[37] pry(main)> "Eylül Çamcı".encode('iso-8859-1')
Encoding::UndefinedConversionError: U+0131 from UTF-8 to ISO-8859-1
from (pry):39:in `encode'
[38] pry(main)> "Eylül Çamcı".encode('iso-8859-9')
=> "Eyl\xFCl \xC7amc\xFD"
This mean you have to drop the UTF-8 entirely in your code.
content: "Eylül Çamcı".encode('iso-8859-9'),
Since the Arabic letters appeared as unknown characters while using prawn gem alone , I installed the Arabic-Prawn gem 0.0.1 to print data in arabic .
I used as a test the below code
Prawn::Document.generate('hello.pdf') do
font "#{Prawn::BASEDIR}/data/fonts/artro.ttf"
font_size 16
self.text_direction = :rtl
long_text = "مرحبا يا العالم"
text long_text.fix_arabic_glyphs
end
First it displays the below error:
undefined method `fix_arabic_glyphs'
so I included the prawn in the class , the error disappeared but the Arabic letters are still not shown (represnted as _).
Someone can help me?
I had the same problem, and this solved it for me:
https://github.com/prawnpdf/prawn/issues/921
I have solved the problem by using another font only.
Some characters are not defined for some fonts .
So using a font that defines all characters for arabic language is the best solution
I'm generating a pdf with prawn. Basically, I generate the document and I fill it with some images.
The problem comes when I download the file and I try to print it. The dimensions are not set to the ones I previously specified.
pdf = Prawn::Document.new(page_size: "A3", margin: PAGE_MARGIN, page_layout: :landscape)
When I try to print it, the default page size is "A4" instead of "A3"
How can I solve this?
I tried to attach some metadata but it didn't work correctly.
Thanks in advance!
In the case where you're generating the document within its own class, this also works to declare the paper size:
class EnvelopePdf < Prawn::Document
def initialize(_item_array, _type_of_item)
super(:page_size => [324, 684], :page_layout => :landscape) # 4.5" by 9.5", which is No 10 envelopes
... application-specific initialization code here ...
print_the_envelopes
end
Using prawn 1.3.0:
require "prawn"
pdf = Prawn::Document.new(:page_size => 'A3')
pdf.text "Hello World!"
pdf.render_file("export.pdf")
in terminal:
pdfinfo export.pdf
outputs:
Creator: Prawn
Producer: Prawn
Tagged: no
Form: none
Pages: 1
Encrypted: no
Page size: 841.89 x 1190.55 pts
Page rot: 0
File size: 842 bytes
Optimized: no
PDF version: 1.3
Hi i'm getting error in this line of code:
pdf.font.size = 13
The name of the file is "show.pdf.prawn"
I'm trying to generate a page into pdf using the prawn gem.
But changing the font size isn't working.
Any alternatives or workaround? Thanks.
BTW: I'm following a simple tutorial from railstips.org : http://railstips.org/blog/archives/2008/10/13/how-to-generate-pdfs-in-rails-with-prawn/
Your tutorial seems to be a little bit old. You should have a look at this manual : http://prawn.majesticseacreature.com/manual.pdf and/or check the railscasts episode : http://railscasts.com/episodes/153-pdfs-with-prawn-revised.
You can use the font_size property to define the size of your text like this :
font_size(25) { text "Even bigger!" }
You can also do it like this :
text "Single line on 20 using the :size option.", :size => 20
Or you can use a block to apply different font properties to your text :
font("Courier", :size => 10) do
text "Yeah, using Courier 10 courtesy of the font method."
end