【发布时间】:2011-07-06 13:24:06
【问题描述】:
我在 asp.net mvc 中使用 iTextSharp 像这样返回 pdf:
public class Pdf : IPdf
{
public FileStreamResult Make(string s)
{
using (var ms = new MemoryStream())
{
using (var document = new Document())
{
PdfWriter.GetInstance(document, ms);
document.Open();
using (var str = new StringReader(s))
{
var htmlWorker = new HTMLWorker(document);
htmlWorker.Parse(str);
}
document.Close();
}
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=MyPdfName.pdf");
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
HttpContext.Current.Response.OutputStream.Flush();
HttpContext.Current.Response.End();
return new FileStreamResult(HttpContext.Current.Response.OutputStream, "application/pdf");
}
}
}
问题是没有渲染像:ă ţ ş 这样的字符
【问题讨论】:
-
您是否尝试过使用十六进制值?您是否尝试过编码以支持此类字符?我使用
ÆØ和Å非常适合iTextSharp。 -
这不是答案,而是对上述代码的观察(与您的问题无关,但将来可能会对您有所帮助): ms.GetBuffer() 可能会在某些情况下生成损坏的 PDF 文件情况,您可以在此处阅读更多详细信息:stackoverflow.com/questions/5109384/…
-
@balexandre 你的字符也对我有用,我将如何使用十六进制值?
-
您是否尝试过按照我说的更改编码?
HttpContext.Current.Response.ContentEncoding = "ISO-8859-16";--> en.wikipedia.org/wiki/ISO/IEC_8859-16
标签: pdf pdf-generation itextsharp