prawnpdf / prawn

Fast, Nimble PDF Writer for Ruby
https://prawnpdf.org
Other
4.65k stars 687 forks source link

Using external TTF fonts to generate PDF with language specific text #996

Closed catalin88 closed 7 months ago

catalin88 commented 7 years ago

Prawn doesn't work with language specific characters. I think this is a known and addressed problem. But I simply can't solve this, I keep getting the same error over and over again.

This is what I have tried:

/models/prawn/change_font_decorator.rb

Prawn::Document.generate("output.pdf") do
  font_families.update("MyFont" => {
    :normal => Rails.root.join("app/assets/fonts/SpecificFont.ttf").to_s
  })
  font "MyFont"
end

Any ideas why it doesn't work ?

pointlessone commented 7 years ago

Hello @catalin88,

What is the issue you're facing? What is the expected result and how actual is different?

catalin88 commented 7 years ago

I keep getting this error message:

raise Prawn::Errors::IncompatibleStringEncoding,
     "Your document includes text that's not compatible with the Windows-1252 character set.\n" \
     "If you need full UTF-8 support, use TTF fonts instead of PDF's built-in fonts\n."

So I have tried changing the font to a ttf font as adviced. Did it, as I described in my initial message, but I still get the same error. I simply don't know where the problem is and how to solve it...

pointlessone commented 7 years ago

@catalin88 Could you please provide a minimal (and complete) snippet of code that demonstrates your issue?

The one in the first message is incomplete.

catalin88 commented 7 years ago

@pointlessone well, actually Prawn is part of a gem I'm using for an e-commerce app to print Invoices. And this is the code that does it:

https://github.com/spree-contrib/spree_print_invoice/blob/master/app/models/spree/bookkeeping_document.rb

But all I'm looking to do is change the font for Prawn, so I'm not sure why the code provided by me is not sufficient...

catalin88 commented 7 years ago

@pointlessone And this is the configuration for the pdf file: https://github.com/spree-contrib/spree_print_invoice/blob/fa7502f6af6e315d788d4ecd9ba3ad6d3e5ad441/lib/spree/print_invoice_setting.rb

preference :font_face, :string, default: 'Helvetica'

def font_faces
    ::Prawn::Font::AFM::BUILT_INS.reject do |font|
        font =~ /zapf|symbol|bold|italic|oblique/i
    end
end
yob commented 7 years ago

I've attempted to answer this on stack overflow

mail2sanand commented 6 years ago

I am trying the same thing on Prawn Tables.

The issue is explained further in this ticket - https://github.com/prawnpdf/prawn/issues/1054

Can somebody please suggest a way out for using ttf fonts in Prawn Tables

Anand

fbukevin commented 6 years ago

With collecting some information, here is a workaround works for me:

  1. Check #{Prawn::DATADIR}/fonts/ if the TrueType font file you want exists. If not, download it and put it into the folder. I have no idea why gkai00mp.ttf (font for simplified Hant letters) was not installed when I use bundle to install prawn. But you can find it here.

  2. In the prawn pdf file

    prawn_document do |pdf| 
    pdf.font "#{Prawn::DATADIR}/fonts/gkai00mp.ttf"
    pdf.text "你好,世界!"
    end

Should works fine.

pointlessone commented 6 years ago

@fbukevin Your issue doesn't seem to be related to the one described in the top message.

We do not package fonts for a couple of reasons. The fonts in the repository are only used for testing.

You should not put any files into Prawn::DATADIR as the dir will be gone when you upgrade gem or do a new install. You should distribute fonts with your application.

theasteve commented 5 years ago

I get the same error. PDF's built-in fonts have very limited support for internationalized text. If you need full UTF-8 support, consider using a TTF font instead. However, I didn't configures any fonts.

pdf_prawn = Prawn::Document.new(page_size: 'LETTER', margin: 0)
          pdf_document_package(pdf_prawn, format_image)
          data = pdf_prawn.render

How can I go about providing default fonts in instantiation or in other way to fix this issue?

gem 'prawn', '~> 2.2', '>= 2.2.2'

gettalong commented 5 years ago

@theasteve Have a look at the http://prawnpdf.org/manual.pdf, pages 57 and 58.

pointlessone commented 7 months ago

I can not reproduce the issue and I don't know where to go with this with the limited information provided. I'm closing this issue. Feel free to reopen with a minimal script that demonstrates the issue.

qisantanu commented 6 months ago

So if you have a string: "Com�e", we are getting this error: Prawn::Errors::IncompatibleStringEncoding (Your document includes text that's not compatible with the Windows-1252 character set.)

It is asking for to use the external font. Is there a solution or a workaround? We are using 'prawn', '2.4.0' and 'prawn-table', '0.2.2'

pointlessone commented 6 months ago

@qisantanu Yes, there's a solution! Use an external font. Also read the manual.

qisantanu commented 6 months ago

It is working now as expected. Thanks @pointlessone , @gettalong