Skip to content

WingDings font gives "iconv(): Detected an incomplete multibyte character in input string" #96

Open

Description

@MircoBabin

Reproduction

Convert file tekst2.zip using:

        $rtf = file_get_contents('tekst2.rtf');

        $document = new Document(rtrim($rtf, "\0"));  // remove ASCII 0, the NULL-byte.
        $formatter = new HtmlFormatter('UTF-8');
        $html = $formatter->Format($document);

This will throw an exception:

iconv(): Detected an incomplete multibyte character in input string at /.../public_html/vendor/henck/rtf-to-html/src/Html/HtmlFormatter.php:375

Analysis

  • On the first line "{\f1\fswiss\fcharset128 Wingdings;}" the WingDings font is defined with fcharset 128.
  • fcharset 128 is the Japanese Shift-JIS multibyte font. When the first character has a code of 128 or higher, the total character is compromised of two bytes.
  • HtmlFormatter.php is unware of the multibyte CP932 codepage. And calls DecodeUnicode() with only one character. iconv() then throws an exception, because only the half of the multibyte character was provided.

Activity

  1. MircoBabin commented on Mar 30, 2023

    @MircoBabin
    Author

    Bypass

    For now I have implemented a bypass in HtmlFormatter.php by wrapping the iconv() call inside a try catch block:

      protected function DecodeUnicode($code, $srcEnc = 'UTF-8')
      {
        $utf8 = '';
    
        if ($srcEnc != 'UTF-8') { // convert character to Unicode
          //MircoBabin wrapped iconv in a try catch block to prevent exception "iconv(): Detected an incomplete multibyte character in input string"
          try {
            $utf8 = iconv($srcEnc, 'UTF-8', chr($code));
          } catch(\Exception $ex) {
            $utf8 = false;
          }
          //MircoBabin - end
        }
    
        if ($this->encoding == 'HTML-ENTITIES') {
          return $utf8 ? "&#{$this->ord_utf8($utf8)};" : "&#{$code};";
    
        } elseif ($this->encoding == 'UTF-8') {
          return $utf8 ? $utf8 : mb_convert_encoding("&#{$code};", $this->encoding, 'HTML-ENTITIES');
    
        } else {
          return $utf8 ? mb_convert_encoding($utf8, $this->encoding, 'UTF-8') :
            mb_convert_encoding("&#{$code};", $this->encoding, 'HTML-ENTITIES');
        }
      }

    But it would be nice if the CP 932 Shift-JIS codepage was correctly handled by this package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions