【问题标题】:iTextSharp MVC View to PDFiTextSharp MVC 查看到 PDF
【发布时间】:2011-09-05 11:06:27
【问题描述】:

在使用 iTextSharp 时尝试解析要转换为 PDF 的 html 字符串时,我的 TextReader 遇到了一点问题。

Function ViewDeliveryNote(ByVal id As Integer) As FileStreamResult
        'Memory buffer
        Dim ms As MemoryStream = New MemoryStream()

        'the document
        Dim document As Document = New Document(PageSize.A4)

        'the pdf writer
        PdfWriter.GetInstance(document, ms)

        Dim wc As WebClient = New WebClient
        Dim htmlText As String = wc.DownloadString("http://localhost:59800/Warehouse/DeliveryNote/" & id) 'Change to live URL
        Dim worker As html.simpleparser.HTMLWorker = New html.simpleparser.HTMLWorker(document)
        Dim reader As TextReader = New StringReader(htmlText)

        document.Open()

        worker.Open()
        worker.StartDocument()
        worker.Parse(reader)
        worker.EndDocument()
        worker.Close()

        document.Close()

        'ready the file stream
        Response.ContentType = "application/pdf"
        Response.AddHeader("content-disposition", "attachment;filename=DeliveryNote.pdf")
        Response.Buffer = True
        Response.Clear()
        Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer.Length)
        Response.OutputStream.Flush()
        Response.End()

        Return New FileStreamResult(Response.OutputStream, "application/pdf")
 End Function

即使StringReader(htmlText) 已成功读取 HTML 页面,它停止的行是 worker.Parse(reader),错误为 Object reference not set to an instance of an object

我不确定自己做错了什么或目前缺少什么,因此我将不胜感激。

更新我只是尝试Dim reader As New StringReader(htmlText),但无济于事。虽然 htmlText 仍然肯定包含一个值,但对象认为它没有。

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 itextsharp html-to-pdf


    【解决方案1】:

    我肯定会为此编写一个自定义操作结果以避免污染我的控制器。此外,您的代码中所有那些未处理的一次性资源都应该得到照顾:

    Public Class PdfResult
        Inherits ActionResult
    
        Private ReadOnly _id As Integer
    
        Public Sub New(ByVal id As Integer)
            _id = id
        End Sub
    
        Public Overrides Sub ExecuteResult(context As ControllerContext)
            If context Is Nothing Then
                Throw New ArgumentNullException("context")
            End If
    
            Dim response = context.HttpContext.Response
            response.Buffer = True
            response.ContentType = "application/pdf"
            response.AddHeader("Content-Disposition", "attachment; filename=DeliveryNote.pdf")
    
            Using client = New WebClient()
                Dim htmlText As String = client.DownloadString("http://localhost:59800/Warehouse/DeliveryNote/" & _id) 'Change to live URL
                Dim doc = New Document(PageSize.A4)
                PdfWriter.GetInstance(doc, response.OutputStream)
                Dim worker = New HTMLWorker(doc)
                doc.Open()
                worker.Open()
                Using reader = New StringReader(htmlText)
                    worker.Parse(reader)
                End Using
                doc.Close()
            End Using
        End Sub
    End Class
    

    然后简单地说:

    Function ViewDeliveryNote(ByVal id As Integer) As ActionResult
        Return New PdfResult(id)
    End Function
    

    您还应该确保服务器可以访问所需的 url。不要忘记他的请求将在网络帐户的上下文中执行,该帐户可能与普通帐户没有相同的权限。

    【讨论】:

    • 现在,这让我更进一步,但是当我去下载它时,该文件被称为正在传递的 ID,然后 IE 说“无法下载 2”。跨度>
    • @Liam,这是PDF文件命名的问题吗?
    • 我会这么想吗?我假设尝试下载的文件将被称为 DeliveryNote.pdf 而不是 2(没有扩展名)。
    • @Liam,是的,这也是我的假设,而这正是我运行此代码时发生的情况:它有效并且它返回 DeliveryNote.pdf 作为文件名。您是否验证了您尝试使用 WebClient 远程获取的 url 确实有效?调试此代码时会发生什么?你在ExecuteResult方法的执行中达到了多远?
    • 是的,页面存在。尽管通过进入 ExecuteResult 让我遇到了同样的问题。 worker.Parse(reader) 上的 NullReferenceException 并且肯定可以访问 URL。
    猜你喜欢
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-30
    相关资源
    最近更新 更多