【问题标题】:iTextsharp pdf cannot open when hosting in IIS在 IIS 中托管时,iTextsharp pdf 无法打开
【发布时间】:2013-10-24 07:27:31
【问题描述】:

我正在使用 iTextSharp 为 ASP.Net (c#) 中的报告创建 PDF。 Pdf 已成功创建并且可以在运行应用程序时打开,但是当 iam 托管在 IIS 中时,它无法以 PDF 格式打开并给出错误:

“adobe reader 无法打开 pdf,因为它不是受支持的文件类型或文件已损坏”

我的代码如下:

Document pdfDoc = new Document();
pdfDoc.SetPageSize(PageSize.A4.Rotate());

string filepath = "Demo" + ".pdf";

string attachment = "attachment; filename=" + filepath;
Response.Charset = String.Empty;
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/PDF";

PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

pdfDoc.Open();

//Adding some paragraph in to the pdfdoc.            

pdfDoc.Close();

Response.Write(pdfDoc);
Response.End();

我在等你的好主意

【问题讨论】:

  • 什么是“Response.Write(pdfDoc);”行好? PDF 本身通过“PdfWriter.GetInstance(pdfDoc, Response.OutputStream);”导出。因此,该 Response.Write 调用很可能会将垃圾添加到响应中。
  • 有没有更好的方法来做到这一点?
  • 您为什么要这样做(即返回除 PDF 之外的其他内容)?如果您不想这样做,只需删除“Response.Write(pdfDoc);”行。

标签: itextsharp


【解决方案1】:

我知道这是一篇旧帖子,但我遇到了 itextsharp 问题,在我的情况下是关于 iis 用户的文件夹权限。

我只是右键单击我的主文件夹,转到安全选项卡并选择 .Net 用户(对我来说是 2 个)并完全控制。那是我的问题,并且解决了。希望这可以帮助其他人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    • 2019-05-30
    相关资源
    最近更新 更多