代码之家  ›  专栏  ›  技术社区  ›  immutabl

princexml:“输入不正确的utf-8”

  •  1
  • immutabl  · 技术社区  · 14 年前

    我正在从数据库生成HTML,然后将其发送到PrinceXML以转换为PDF。我使用的代码是:

    string _htmlTemplate = @"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd""><html lang=""en-GB"" xml:lang=""en-GB"" xmlns=""http://www.w3.org/1999/xhtml""><head><meta http-equiv=""Content-type"" content=""text/html;charset=UTF-8"" /><title>Generated PDF Contract</title></head><body>{0}</body></html>";
    
    string _pgeContent = string.Format(_htmlTemplate, sb.ToString());
    writer.Write(sb.ToString());
    Byte[] arrBytes = UTF8Encoding.Default.GetBytes(_pgeContent);
    Stream s = new MemoryStream(arrBytes);
    
    Prince princeConverter = new Prince(ConfigurationManager.AppSettings["PrinceXMLInstallLoc"].ToString());
    princeConverter.SetLog(ConfigurationManager.AppSettings["PrinceXMLLogLoc"]);
    princeConverter.AddStyleSheet(Server.MapPath(ConfigurationManager.AppSettings["FormsDocGenCssLocl"]));
    Response.ClearContent();
    Response.ClearHeaders();
    Response.ContentType = "application/pdf";
    Response.BufferOutput = true;
    

    但是,转换失败,错误为:

    输入不正确的UTF-8,表示编码!字节:0xa0 0x77 0x65 0x62

    我已经获取生成的HTML并将其上载到W3C验证器。它验证标记为UTF-8编码的XHTML 1.0过渡,没有错误或警告。

    我还仔细检查了文件中的无效字符。到目前为止还没有。

    有人能推荐我可以尝试的其他东西吗?

    1 回复  |  直到 14 年前
        1
  •  2
  •   immutabl    14 年前

    一个下午,我咕哝着咒骂,撕掉了我头发上剩下的部分,然后我想出了一个解决我特殊问题的方法。

    默认情况下,system.text.utf 8编码不会输出utf-8标识符字节。所以在我的例子中,我需要使用一个接受布尔参数的构造函数来控制这个参数的输出。

    UTF8Encoding u8enc = new UTF8Encoding(true);//Ensures a UTF8 identifier is emitted.
    

    之后一切都很好。希望这能帮助别人:-)