Asp.net mvc iTextSharp MVC视图转换为PDF
当我试图解析使用iTextSharp时要转换为PDF的html字符串时,我的文本阅读器有点问题Asp.net mvc iTextSharp MVC视图转换为PDF,asp.net-mvc,asp.net-mvc-3,itextsharp,html-to-pdf,Asp.net Mvc,Asp.net Mvc 3,Itextsharp,Html To Pdf,当我试图解析使用iTextSharp时要转换为PDF的html字符串时,我的文本阅读器有点问题 Function ViewDeliveryNote(ByVal id As Integer) As FileStreamResult 'Memory buffer Dim ms As MemoryStream = New MemoryStream() 'the document Dim document As Document = Ne
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.Parsereader,错误对象引用未设置为对象实例,即使StringReaderhtmlText已成功读取HTML页面
我不知道我做错了什么,也不知道我现在错过了什么,所以如果有任何帮助,我将不胜感激
更新我只是尝试了Dim reader作为新的StringReaderhtmlText,但没有用。虽然htmlText仍然肯定包含一个值,但对象认为它不包含。我肯定会为此编写一个自定义操作结果,以避免污染我的控制器。此外,还应注意代码中所有未分散的可支配资源:
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。别忘了他的请求将在网络帐户的上下文中执行,而网络帐户可能没有与普通帐户相同的权限。现在,这让我更进一步了,但当我下载它时,文件名为正在传递的ID,然后IE说2无法下载。@Liam,这是PDF文件命名的问题吗?我会这么想的?我假设尝试下载的文件名为DeliveryNote.pdf not,而不是2,没有扩展名。@利亚姆,是的,我也会这样假设,这正是我运行此代码时发生的情况:它工作了,并返回DeliveryNote.pdf作为文件名。您是否已验证您尝试使用WebClient远程获取的url是否有效?调试此代码时会发生什么情况?在ExecuteSult方法的执行过程中,您达到了什么程度?是的,页面存在。虽然通过进入执行器,结果让我遇到了同样的问题。worker.Parsereader上存在NullReferenceException,并且肯定可以访问URL。