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仍然包含一個值,但是該對象認爲它沒有。
現在,這已經讓我更進了一步,但是當我確實去下載它時,文件被稱爲ID被傳遞,然後IE說「2無法下載」。 – LiamGu
@Liam,這是PDF文件的命名問題嗎? –
我會這麼想的?我假設試圖下載的文件將被稱爲DeliveryNote.pdf而不是2(沒有擴展名)。 – LiamGu