iTextSharp的MVC视图PDF [英] iTextSharp MVC View to PDF
问题描述
我试图解析HTML字符串我想用iTextSharp的时候转换为PDF时,有我的TextReader有点麻烦了。
I'm having a little trouble with my TextReader when trying to parse the html string I want to convert to PDF when using 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
它停止在该生产线是 worker.Parse(阅读器)
与对象引用不设置到对象的实例错误
即使 StringReader(的htmlText)
已成功地读取HTML页面。
The line it stops on is worker.Parse(reader)
with the error Object reference not set to an instance of an object
even though StringReader(htmlText)
has successfully read the HTML page.
我不知道我做错了还是什么我失踪的时刻,所以我会的任何援助表示感谢。
I'm not sure what I'm doing wrong or what I'm missing at the moment so I would be grateful for any assistance.
更新我只是想昏暗阅读器作为新StringReader(的htmlText)
来代替,但无济于事。虽然仍的htmlText肯定包含一个值,但对象认为它没有。
UPDATE I just tried Dim reader As New StringReader(htmlText)
instead but to no avail. Although htmlText still definitely contains a value, but the object thinks that it doesn't.
推荐答案
我肯定会写这样的自定义操作结果避免污染我的控制器。另外,在你的code所有这些一次性未予资源应该照顾:
I would definitely write a custom action result for this to avoid polluting my controller. Also all those undisposed disposable resources in your code should be taken care of:
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。不要忘了,他的要求将在可能不具有相同的权限正常账户的网络帐户的上下文中执行。
You should also make sure that the server has access to the desired url. Don't forget that his request will execute in the context of the Network Account which might not have the same privileges as normal accounts.
这篇关于iTextSharp的MVC视图PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!