2011-09-05 116 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