2011-09-05 89 views
0

当我试图解析使用iTextSharp时想要转换为PDF的html字符串时,我遇到了一些问题。iTextSharp MVC查看PDF

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 

它停止在该生产线是worker.Parse(reader)与错误Object reference not set to an instance of an object即使StringReader(htmlText)已经成功地读取HTML页面。

我不确定我做错了什么,或者我现在错过了什么,所以我会很感激任何帮助。

UPDATE我只是试过Dim reader As New StringReader(htmlText)而不是无济于事。尽管htmlText仍然包含一个值,但是该对象认为它没有。

回答

0

我肯定会为此写一个自定义操作结果以避免污染我的控制器。另外,在你的代码中所有这些未予处置一次性资源应该被照顾:

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。不要忘记,他的请求将在网络帐户的环境中执行,该帐户可能与正常帐户的权限不同。

+0

现在,这已经让我更进了一步,但是当我确实去下载它时,文件被称为ID被传递,然后IE说“2无法下载”。 – LiamGu

+0

@Liam,这是PDF文件的命名问题吗? –

+0

我会这么想的?我假设试图下载的文件将被称为DeliveryNote.pdf而不是2(没有扩展名)。 – LiamGu