I'm trying to use ITextSharp to convert a HTML page to a PDF but running into some problems with the system.net.webrequest.GetResponse as it seems to clear my session states/values.
The code is:
Seems when it executes the following section:
It goes back to to Page_Load but does not have the session information anymore.
Can anyone shed some light on this?
The code is:
VB.NET:
Protected Sub ExportToPDFButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ExportToPDFButton.Click
' Creates variables for the location of the page and the destination of the PDF file
Dim StrURL As Uri = Request.Url
Dim URL As String = StrURL.ToString
Dim FilePath As String = Server.MapPath("TEST/PO_" & Session("PurchaseOrderToPrint") & ".htm")
Dim Writer As New System.IO.StreamWriter(FilePath)
' Converts the page
Dim HTMLText As String = String.Empty
Dim FileName As String = Server.MapPath("TEST/PO_" & Session("PurchaseOrderToPrint") & ".xml")
Try
Dim RequestIP As System.Net.HttpWebRequest = DirectCast(System.Net.WebRequest.Create(URL), System.Net.HttpWebRequest)
RequestIP.Timeout = 10000
Using ResponseIP As System.Net.HttpWebResponse = DirectCast(RequestIP.GetResponse(), System.Net.HttpWebResponse)
Using StreamIP As System.IO.Stream = ResponseIP.GetResponseStream
Using ReaderText As New System.IO.StreamReader(StreamIP)
HTMLText = ReaderText.ReadToEnd
Dim Text As String = HTMLText
Dim TextWriter As New System.IO.StreamWriter(FileName)
TextWriter.Write(Text)
TextWriter.Close()
End Using
End Using
End Using
Catch ex As Exception
End Try
' Creats the PDF
Dim Doc As New Document
pdf.PdfWriter.GetInstance(Doc, New System.IO.FileStream(Server.MapPath("TEST/PO_" & Session("PurchaseOrderToPrint") & ".pdf"), System.IO.FileMode.Create))
html.HtmlParser.Parse(Doc, Server.MapPath("TEST/PO_" & Session("PurchaseOrderToPrint") & ".htm"))
' Erases temporary files
If System.IO.File.Exists(Server.MapPath("TEST/PO_" & Session("PurchaseOrderToPrint") & ".htm")) Then
System.IO.File.Delete(Server.MapPath("TEST/PO_" & Session("PurchaseOrderToPrint") & ".htm"))
End If
If System.IO.File.Exists(Server.MapPath("TEST/PO_" & Session("PurchaseOrderToPrint") & ".xml")) Then
System.IO.File.Delete(Server.MapPath("TEST/PO_" & Session("PurchaseOrderToPrint") & ".xml"))
End If
End Sub
Seems when it executes the following section:
VB.NET:
Using ResponseIP As System.Net.HttpWebResponse = DirectCast(RequestIP.GetResponse(), System.Net.HttpWebResponse)
It goes back to to Page_Load but does not have the session information anymore.
Can anyone shed some light on this?