Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
MircoBabin opened this issue Mar 30, 2023 · 1 comment

Comments

@MircoBabin
Copy link

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.
@MircoBabin
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant