2

I have a script that uses pdf-reader among other gems to parse and split up a large pdf file into multiple pdfs. I have been setting it up to run on a new machine, and am now getting this error when I try to run tests:

Failure/Error: SrnProcessor.process_main_pdf(main_pdf_path)
ArgumentError:
Unknown glyph width for 9 Helvetica

I saw that a couple other people are having this issue over here: https://github.com/yob/pdf-reader/issues/102 But no workarounds yet. It seems to me that this must be an issue with my setup and not the gem or the pdf, since on other machines I don't have any problems. I'm running ruby 1.9.3 on linux (Mageia 3 64bit), and have tried switching to multiple versions of the gem with no luck. Any suggestions on what I'm missing? Thanks!

1 Answers1

0

I am having the same problem with ruby 1.9.3. I have narrowed it down for me at least to having the trademark R in the pdf. I got it down to this code in the pdf gem:

 def glyph_width(code_point)
    return 0 if code_point.nil? || code_point <= 0

    m = @metrics.char_metrics_by_code[code_point]
    if m.nil?
      names = @font.encoding.int_to_name(code_point)
      m = names.map { |name|
        @metrics.char_metrics[name.to_s]
      }.compact.first
    end

    if m
      m[:wx]
    elsif @font.widths[code_point - 1]
      @font.widths[code_point - 1]
    else
      raise ArgumentError, "Unknown glyph width for #{code_point} #{@font.basefont}"
    end
  end

and i changed it to this which will just ignore characters that it cant recognize. I'm not sure if ruby 2.0 has fixed this issue or not.

  def glyph_width(code_point)
    return 0 if code_point.nil? || code_point <= 0

    m = @metrics.char_metrics_by_code[code_point]
    if m.nil?
      names = @font.encoding.int_to_name(code_point)
      m = names.map { |name|
        @metrics.char_metrics[name.to_s]
      }.compact.first
    end

    if m
      m[:wx]
    elsif @font.widths[code_point - 1]
      @font.widths[code_point - 1]
    elsif(m == nil)
      return 0
    else
      p(m)
      raise ArgumentError, "Unknown glyph width for #{code_point} #{@font.basefont}"
    end
  end

A better solution might be to catch the code_point which in this case is 9 in the char_metrics_by_code

moloch101
  • 30
  • 4