【问题标题】:Asp.Net MVC filecontentresult error generating pdfAsp.Net MVC filecontentresult错误生成pdf
【发布时间】:2018-08-08 19:26:53
【问题描述】:

控制器:

    <HttpGet>
Function downloadpdf() As FileContentResult

    Dim data As MyData = Me.Getdata()
    Dim sb As StringBuilder = New StringBuilder()

    If data IsNot Nothing Then
        If data.HasItems Then
            sb.AppendLine()
            sb.AppendLine("ID,Description)
            For Each item In data.Items
                sb.AppendLine(String.Format("{0},{1}", item.Id, item.Description))
            Next
        End If
    End If
    Dim bytes as Byte() = New UTF8Encoding().GetBytes(sb.ToString())
    Response.Clear()
    Response.AddHeader("Content-Disposition", "inline; filename=mypdf.pdf")
    Response.AddHeader("Content-Type", "application/pdf")
    Response.ClearHeaders()
    Response.AddHeader("Content-Length", bytes.Length.ToString())
    Return File(bytes, "application/pdf", "mypdf.pdf")

End Function

查看:

 <a href="@Url.Action("downloadpdf", "home")">PDF</a>

点击链接时,提示下载文件,然后显示如下:

Adobe Acrobat Reader 无法打开“mypdf.pdf”,因为它不是受支持的文件类型或文件已损坏(例如,它作为电子邮件附件发送且未正确解码)。

而且 pdf 没有打开。 有什么建议或任何其他方式吗?

干杯

【问题讨论】:

    标签: asp.net-mvc vb.net pdf


    【解决方案1】:

    这不是您创建 PDF 的方式。您只是返回一个带有 PDF 扩展名的文本文件。

    您将需要第三方工具来创建 PDF。我使用ABCPdf(付费,我不隶属于他们),但有几个opensourcetools可供选择。

    相关:.NET server based PDF generation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-22
      • 1970-01-01
      • 2010-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-17
      • 2014-10-15
      相关资源
      最近更新 更多