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