jessedoyle / prawn-icon

Easy icons for Prawn.
Other
28 stars 15 forks source link

Getting Prawn::Errors::IncompatibleStringEncoding #44

Closed alliedarmour closed 3 years ago

alliedarmour commented 3 years ago

Hey, first it's a really nice gem as it's exactly what I'm looking for, but I can only partially get it to work.

I'm using prawn-rails to generate PDFs in my Rails 6 application, which is working fine. Now I want to include some icons and I imported the Font Awesome font to the font_families:

 pdf.font_families.update(
        "Font Awesome" => { :normal => Rails.root.join("app", "assets", "fonts", "fa-solid-900.ttf") }
    )

Now, if I usepdf.font "Font Awesome" the icons in my table are working, but I don't want the whole document to have this font family.

The problem is if I don't use this change, I always get the error you see in the title:

Your document includes text that's not compatible with the Windows-1252 character set.
If you need full UTF-8 support, use external fonts instead of PDF's built-in fonts.

I searched the repository here and some stackoverflow threads but I can't seem to fix the issue.

I tried to make the cell before the table and set the font family, but also no luck:

    cell_1 = pdf.make_cell(pdf.table_icon('fas-birthday-cake'))
    cell_1.font = "Font Awesome"

Any ideas how to get this to work? Is it maybe a prawn-rails issue?

jessedoyle commented 3 years ago

Hey @alliedarmour, thanks for using prawn-icon!

I'm pretty sure I've encountered this, and I'm pretty sure the root cause lives in prawn-table.

I'll do some investigation, but you could help me if you could confirm that the removal of prawn-table from your Gemfile resolves the issue (i.e. can you replicate the same exception without prawn-table).

Thanks!

jessedoyle commented 3 years ago

Ahh - I think we've seen this before: https://github.com/jessedoyle/prawn-icon/issues/39

Yes, it does appear to be an issue that stems either from prawn or prawn-table. In particular it appears to occur with font-awesome fonts.

@alliedarmour - I hate locking gems to a particular version, but can you please try locking prawn to 2.1.0 for your app? If so, does the exception still occur?

Thanks, maybe we can get to the bottom of this!

alliedarmour commented 3 years ago

Ahh - I think we've seen this before: #39

Yes, it does appear to be an issue that stems either from prawn or prawn-table. In particular it appears to occur with font-awesome fonts.

@alliedarmour - I hate locking gems to a particular version, but can you please try locking prawn to 2.1.0 for your app? If so, does the exception still occur?

Thanks, maybe we can get to the bottom of this!

I'm using the prawn-rails gem where prawn is installed as dependency :/. I looked into the prawn-rails issue with the creation of an initializer, but this didn't work out, it was another problem anyway.

jessedoyle commented 3 years ago

I'm using the prawn-rails gem where prawn is installed as dependency

For sure, but it looks prawn-rails doesn't lock the version of prawn (see: https://github.com/cortiz/prawn-rails/blob/master/prawn-rails.gemspec#L21), so the app should be able to define the version prawn.

Can you try adding this line to your app's Gemfile and running bundle install?

gem 'prawn', '2.1.0'

I can replicate the issue locally when trying to render a table, but only with prawn >= 2.2.2. So a change to font handling must have been made between versions 2.1.0 and 2.2.2 that introduced the bug.

jessedoyle commented 3 years ago

@alliedarmour - Okay so I think I've figured it out!

It's a complex issue, and it turns out it's fixed in prawn-table master from a PR I submitted almost 5 years ago now (https://github.com/prawnpdf/prawn-table/pull/60).

Unfortunately, there hasn't been a release of prawn-table pushed to Rubygems that includes the fix.

Here's what seems to be happening:

TLDR: There was a defect in prawn-table causing the issue that's long been fixed. Unfortunately there hasn't been a release of prawn-table to Rubygems that includes the fix.

The error should go away if you point prawn-table to the GitHub master branch:

# in Gemfile
gem 'prawn-table', git: 'https://github.com/prawnpdf/prawn-table.git'

I'm going to close the issue as it's not a defect in prawn-icon. Thanks!

alliedarmour commented 3 years ago

@alliedarmour - Okay so I think I've figured it out!

It's a complex issue, and it turns out it's fixed in prawn-table master from a PR I submitted almost 5 years ago now (prawnpdf/prawn-table#60).

Unfortunately, there hasn't been a release of prawn-table pushed to Rubygems that includes the fix.

Here's what seems to be happening:

  • Prawn >= 2.2.2 introduced Better Handling for Windows-1252 Encoded Strings.
  • This change fixed broken font-handling logic in prawn, but also caused the exception to be raised due to broken logic in prawn-table (prawn-table wasn't using the text's current font when calculating width).
  • This PR resolves the issue in prawn-table: prawnpdf/prawn-table#60, but a release has not been pushed to Rubygems that includes the fix.

TLDR: There was a defect in prawn-table causing the issue that's long been fixed. Unfortunately there hasn't been a release of prawn-table to Rubygems that includes the fix.

The error should go away if you point prawn-table to the GitHub master branch:

# in Gemfile
gem 'prawn-table', git: 'https://github.com/prawnpdf/prawn-table.git'

I'm going to close the issue as it's not a defect in prawn-icon. Thanks!

You're my hero, it's working! Thanks for your research and your quick help, you saved a bit of my thesis :D. Hopefully they will merge in the PR and publish a new release.

alliedarmour commented 3 years ago

Working like a charm :)

Bildschirmfoto von 2020-08-26 22-20-57

jessedoyle commented 3 years ago

Glad to hear @alliedarmour! Looks great!