【问题标题】:PrinceXML: "Input is not proper UTF-8"PrinceXML:“输入不是正确的 UTF-8”
【发布时间】:2010-11-17 11:59:23
【问题描述】:

我正在从数据库生成 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 过渡,没有错误或警告。

我还用细齿梳检查了文件,寻找无效字符。到目前为止什么都没有。

谁能建议我可以尝试的其他方法?

【问题讨论】:

  • 是的,按照错误消息的建议将流转换为 UTF-8。
  • @DarinDimitrov W3c 验证器将其解析为有效的 UTF-8 编码的 XHTML 是否意味着它 UTF-8 ?还是我错过了什么……?

标签: c# asp.net encoding html-entities princexml


【解决方案1】:

嗯,经过一个下午的低语诅咒和扯掉我剩下的头发,我找到了解决我的特殊问题的方法。

默认情况下,System.Text.UTF8Encoding 似乎不输出 UTF-8 标识符字节。所以就我而言,我需要使用带有布尔参数的构造函数来控制 this 的输出。

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

在这之后一切都很好。希望这可以帮助某人:-)

【讨论】:

  • 有点无关紧要,但是Prince 是否支持您之前使用的XHTML 过渡文档类型?
猜你喜欢
  • 2013-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-29
  • 2019-01-08
  • 2021-10-09
  • 2019-05-23
相关资源
最近更新 更多