2010-11-17 55 views
1

我从数据库生成HTML,然后将它发送到PrinceXML以转换为PDF。我用它来做到这一点的代码是:PrinceXML:“Input is not proper UTF-8”

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; 

然而,转换失败,出现错误:

Input is not proper UTF-8, indicate encoding ! Bytes: 0xA0 0x77 0x65 0x62

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

我也通过一个精细的牙梳寻找无效字符的文件。到目前为止没有。

任何人都可以推荐别的东西我可以试试吗?

+1

是的,按照错误消息的建议将流转换为UTF-8。 – 2010-11-17 12:06:03

+0

@DarinDimitrov W3c验证程序将它解析为有效的UTF-8编码的XHTML意味着它*是* UTF-8吗?或者我错过了什么......? – 5arx 2010-11-17 12:18:18

回答

2

经过一天下午嘟嘟curs te地扯出什么是我的头发,我想出了一个解决我的特定问题的方法。

这会显示System.Text.UTF8Encoding默认情况下不会输出UTF-8标识符字节。所以在我的情况下,我需要使用带布尔参数的构造函数来控制输出。

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

+0

有点不相干,但Prince是否支持你在那里使用的XHTML过渡文档类型? – user961627 2013-01-28 15:25:47

+0

是的,唯一的问题是通过此修复解决的。 – 5arx 2013-01-29 11:00:56