gettalong / hexapdf

Versatile PDF creation and manipulation for Ruby
https://hexapdf.gettalong.org
Other
1.21k stars 69 forks source link

Strange behaviour with rescue retry #276

Closed MorneL closed 7 months ago

MorneL commented 8 months ago

Hi there,

I'm currently dealing with removing emoji's from strings if the glyphs are not found, I'm aware of font.on_missing_glyph but we want to completely remove the glyph as replacing it with a different glyph is giving us different strange behaviour.

However right now I'm dealing with this issue where I get the following error ONLY when I retry in a rescue. If I run the block inside begin again after the gsub, it's fine, however if I use the retry my document for some reason gets wrapped as a stream.

undefined method `contents=' for #

  hexa_pdf_document.acro_form.each_field do |field|
    begin
      field.field_value = "test 😬"
    rescue HexaPDF::MissingGlyphError => glyph_error
      data[field.field_name].gsub!(glyph_error.glyph.str, "")
      retry
    end
  end
Screenshot 2023-12-07 at 09 26 03

The screenshot shows that further down the code it doesn't pass any stream, however it returns as a stream 🤷

MorneL commented 8 months ago

Ah I just figured out it's because I'm setting field_value more than once. Any reason why I wouldn't be allowed to set that multiple times?

gettalong commented 8 months ago

Using Field#field_value= more than once should definitely be possible, I will take a look at it.

If you have time, could you open another issue for " I'm aware of font.on_missing_glyph but we want to completely remove the glyph as replacing it with a different glyph is giving us different strange behaviour.", i.e. what strange behaviour? - Thanks!

gettalong commented 8 months ago

@MorneL I have created a small, self-contained example:

require 'hexapdf'

doc = HexaPDF::Document.new
form = doc.acro_form(create: true)
field = form.create_text_field('test')
field.create_widget(doc.pages.add, Rect: [10, 10, 100, 30])
data = "test 😬"
begin
        field.field_value = data
rescue HexaPDF::MissingGlyphError => glyph_error
  data = 'test'
        retry
end
doc.write('gh276.pdf')

This example works as is, i.e. no error is thrown.

Could you provide a self-contained example that shows the problem? My guess is that the used PDF has something to do with the problem.

MorneL commented 8 months ago

Using Field#field_value= more than once should definitely be possible, I will take a look at it.

If you have time, could you open another issue for _" I'm aware of font.on_missingglyph but we want to completely remove the glyph as replacing it with a different glyph is giving us different strange behaviour.", i.e. what strange behaviour? - Thanks!

sure here you go: https://github.com/gettalong/hexapdf/issues/277

MorneL commented 8 months ago

@MorneL I have created a small, self-contained example:

require 'hexapdf'

doc = HexaPDF::Document.new
form = doc.acro_form(create: true)
field = form.create_text_field('test')
field.create_widget(doc.pages.add, Rect: [10, 10, 100, 30])
data = "test 😬"
begin
        field.field_value = data
rescue HexaPDF::MissingGlyphError => glyph_error
  data = 'test'
        retry
end
doc.write('gh276.pdf')

This example works as is, i.e. no error is thrown.

Could you provide a self-contained example that shows the problem? My guess is that the used PDF has something to do with the problem.

I will look at this tomorrow again and give you an update

gettalong commented 8 months ago

@MorneL Did you have the chance to look at the problem again and provide a self-contained example?

gettalong commented 7 months ago

@MorneL I will close this issue for now. Feel free to open it again with more information or a self-contained example to reproduce the problem.