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
HTMLページを正常に読み取ったにもかかわらず、停止した行にworker.Parse(reader)
エラーがあります。Object reference not set to an instance of an object
StringReader(htmlText)
現時点で何が間違っているのか、何が欠けているのかわからないので、何か助けていただければ幸いです。
更新Dim reader As New StringReader(htmlText)
代わりに試してみましたが、役に立ちませんでした。htmlText には確かに値が含まれていますが、オブジェクトは値が含まれていないと認識しています。