项目清单
我正进入(状态"the uri prefix is not recognized itextsharp"
例外。遵循我的 web api 控制器的方法。它接收 html 数据并将其转换为 pdf。
[System.Web.Http.AcceptVerbs("GET", "POST")]
public HttpResponseMessage DownloadPDF(long estimationId)
{
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
try
{
//Method fetch the data for estimationId and convert that to HTML
string html = objRepository.GetHTML(estimationId);
//Create PDF
var document = new iTextSharp.text.Document(PageSize.A4);
document.SetMargins(20.0F, 20.0F, document.TopMargin + 20, document.BottomMargin);
var output = new MemoryStream();
var writer = PdfWriter.GetInstance(document, output);
document.Open();
StringReader strRdr = new StringReader(html);
var parsedHtmlElements = HTMLWorker.ParseToList(strRdr, null);
foreach (var htmlElement in parsedHtmlElements)
document.Add(htmlElement as IElement);
writer.CloseStream = false;
writer.Flush();
// document.Close();
output.Seek(0, SeekOrigin.Begin);
result.Content = new ByteArrayContent(output.ToArray());
}
catch (Exception e)
{
ErrorLog.GetDefault(System.Web.HttpContext.Current).Log(new Error(e));
}
return result;
}
行发生异常
var parsedHtmlElements = HTMLWorker.ParseToList(strRdr, null);
请提供任何帮助。
您需要检查您的 HTML。该异常并不是真正的 iTextSharp 异常,而是来自 iTextSharp 要求 .Net 框架通过以下方式下载远程资源:System.Net.WebRequest
。不管怎样,你会得到一个不以以下形式开头的 URLhttp
or https
(or file
or ftp
),通常这是一个 base64 编码的图像。您可以通过执行以下操作来重现此消息:
var req = System.Net.WebRequest.Create("data:image/png;base64");
req.GetResponse();
System.dll 中发生了“System.NotSupportedException”类型的第一次机会异常
System.dll 中发生“System.NotSupportedException”类型的未处理异常
附加信息:无法识别 URI 前缀。
如果您有一个无协议/协议相对 URL,您会收到略有不同的消息:
var req = System.Net.WebRequest.Create("//gerp");
req.GetResponse();
System.dll 中发生了“System.Net.WebException”类型的第一次机会异常
System.dll 中发生“System.Net.WebException”类型的未处理异常
附加信息:UNC 路径的格式应为 \server\share。
您需要更改 HTML 以仅使用支持协议,或者可能在系统中注册您自己的前缀 https://stackoverflow.com/a/14779498/231316.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)