# File lib/pdf/reader/encoding.rb, line 103
    def to_utf8(str, tounicode = nil)

      # unpack the single bytes
      array_orig = str.unpack(@unpack)

      # replace any relevant bytes with a glyph name
      array_orig = process_differences(array_orig)

      # replace any remaining bytes with a unicode codepoint
      array_enc = []
      array_orig.each do |num|
        if tounicode && (code = tounicode.decode(num))
          array_enc << code
        elsif tounicode || (tounicode.nil? && @to_unicode_required)
          array_enc << PDF::Reader::Encoding::UNKNOWN_CHAR
        elsif @mapping && @mapping[num]
          array_enc << @mapping[num]
        else
          array_enc << num
        end
      end

      # convert any glyph names to unicode codepoints
      array_enc = process_glyphnames(array_enc)

      # replace charcters that didn't convert to unicode nicely with something valid
      array_enc.collect! { |c| c ? c : PDF::Reader::Encoding::UNKNOWN_CHAR }

      # pack all our Unicode codepoints into a UTF-8 string
      ret = array_enc.pack("U*")

      # set the strings encoding correctly under ruby 1.9+
      ret.force_encoding("UTF-8") if ret.respond_to?(:force_encoding)

      return ret
    end