iTextSharp MVC View to PDF
-
26-10-2019 - |
Domanda
Sto avendo un po 'di problemi con la mia TextReader quando si cerca di analizzare la stringa html voglio convertire in PDF quando si utilizza iTextSharp.
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
La linea si ferma su è worker.Parse(reader)
con la Object reference not set to an instance of an object
errore anche se StringReader(htmlText)
ha letto correttamente la pagina HTML.
Non sono sicuro di quello che sto facendo male o che cosa mi manca in questo momento così sarei grato per qualsiasi tipo di assistenza.
Aggiorna Ho appena provato Dim reader As New StringReader(htmlText)
invece ma senza alcun risultato. Anche se ancora htmlText sicuramente contiene un valore, ma l'oggetto pensa che non è così.
Soluzione
mi sarebbe sicuramente scrivere un risultato un'azione personalizzata per questo per evitare di inquinare il mio controller. Inoltre tutte quelle risorse monouso undisposed nel codice dovrebbero essere presi cura di:
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
e poi semplicemente:
Function ViewDeliveryNote(ByVal id As Integer) As ActionResult
Return New PdfResult(id)
End Function
Si dovrebbe anche fare in modo che il server ha accesso a l'URL desiderato. Non dimenticate che la sua richiesta verrà eseguito nel contesto del account di rete che potrebbe non avere gli stessi privilegi come i conti normali.