The underlying connection was closed: Unable to connect to the remote server

Getting this with aspose.pdf 3.3 on very simple code that instantiates a pdf object, calls BindHTML with a stringbuilder full of HTML text, then saves the pdf to disk. this wasn't happening with the same version when i was calling BindXML. The full .net error is below.

The underlying connection was closed: Unable to connect to the remote server.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The underlying connection was closed: Unable to connect to the remote server.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[WebException: The underlying connection was closed: Unable to connect to the remote server.]
   System.Net.HttpWebRequest.CheckFinalStatus() +677
   System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) +140
   System.Net.HttpWebRequest.GetResponse() +249
   Aspose.Pdf.Xml.ᨉ.ᨊ(Pdf ᖎ, Section ᠯ, Table ส, Row ᙶ, Cell ᕵ, Image ඔ, ᔣ ᔳ, ᛹ ᤥ, Boolean ᤦ) +599
   Aspose.Pdf.Xml.ᘘ.ᘚ(Pdf ᖎ, Section ᖙ, ᔣ ᔳ) +4044
   Aspose.Pdf.Xml.౏.ᗭ(Pdf ᖎ) +1061
   Aspose.Pdf.Xml.ᖈ.ᖍ(ገ ዿ, Pdf ᖎ) +369
   Aspose.Pdf.Pdf.Save(String pdfFile) +441
   CyberRecruiter4.EmailLetterPreview.Page_Load(Object sender, EventArgs e) +414
   System.Web.UI.Control.OnLoad(EventArgs e) +67
   System.Web.UI.Control.LoadRecursive() +35
   System.Web.UI.Page.ProcessRequestMain() +750

Can you please provide your code and let us check it?

I can provide the full HTML text if necessary
***********************************

Dim str2 As New StringReader("

" + "Bunch of HTML text here" + "

")

Dim license2 As Aspose.pdf.License = New Aspose.pdf.License
license2.SetLicense(Request.ServerVariables("APPL_PHYSICAL_PATH") + "bin\Aspose.PDF.lic")

Dim pdf As New Aspose.Pdf.Pdf
pdf.IsTruetypeFontMapCached = False
pdf.BindHTML(str2)

Dim pdfMS As MemoryStream = New MemoryStream
pdf.Save(Request.ServerVariables("APPL_PHYSICAL_PATH") + "temp\" + TempFileName)

pdf = Nothing

Dim ReadStream As System.IO.FileStream = New System.IO.FileStream(Request.ServerVariables("APPL_PHYSICAL_PATH") + "temp\" + TempFileName, FileMode.Open, FileAccess.Read)

Dim FileLen As Integer
Dim arrFile(ReadStream.Length) As Byte
ReadStream.Read(arrFile, 0, ReadStream.Length)

Response.ContentType = "application/pdf"
Response.BinaryWrite(arrFile)
Response.Flush()
ReadStream.Close()

You should not bind the HTML string directly using BindHTML method. You can save the string into a file or memory stream and the use BindHTML.

still no good, same error, here's my new code
**********************************

Dim buffer() As Byte
Dim encoder As New System.Text.ASCIIEncoding
Dim str As String = "

" +"HTML Text Here" + "

"

ReDim buffer(str.Length - 1)
encoder.GetBytes(str, 0, str.Length, buffer, 0)

Dim license2 As Aspose.pdf.License = New Aspose.pdf.License
license2.SetLicense(Request.ServerVariables("APPL_PHYSICAL_PATH") + "bin\Aspose.PDF.lic")

Dim ms2 As MemoryStream = New MemoryStream(buffer, False)
Dim pdf As New Aspose.Pdf.Pdf
pdf.IsTruetypeFontMapCached = False
pdf.BindHTML(ms2)
Dim pdfMS As MemoryStream = New MemoryStream
pdf.Save(Request.ServerVariables("APPL_PHYSICAL_PATH") + "temp\" + TempFileName)

pdf = Nothing

Dim ReadStream As System.IO.FileStream = New System.IO.FileStream(Request.ServerVariables("APPL_PHYSICAL_PATH") + "temp\" + TempFileName, FileMode.Open, FileAccess.Read)

Dim FileLen As Integer
Dim arrFile(ReadStream.Length) As Byte
ReadStream.Read(arrFile, 0, ReadStream.Length)

Response.ContentType = "application/pdf"
Response.BinaryWrite(arrFile)
Response.Flush()
ReadStream.Close()

I tested with the following code and it worked:

Dim buffer() As Byte
Dim encoder As New System.Text.ASCIIEncoding
Dim str As String = "

" + "HTML Text Here" + "

"

ReDim buffer(str.Length - 1)
encoder.GetBytes(str, 0, str.Length, buffer, 0)

Dim ms2 As MemoryStream = New MemoryStream(buffer, False)
Dim pdf As New Aspose.Pdf.Pdf
pdf.IsTruetypeFontMapCached = False
pdf.BindHTML(ms2)
Dim pdfMS As MemoryStream = New MemoryStream
pdf.Save("d:/test/test.pdf")

Please try if it work for you. If it work you should check the Request.ServerVariables in your code.

same thing, even if i hard-code the path like you did. it ends up making a .pdf file of 0KB in that path.

I wonder why I can't reproduce this error. Please:

1) makre sure you are using the latest version of Aspose.Pdf.

2) try adding the following code:

ms2.Position = 0
pdf.BindHTML(ms2)

If it still won't work, please send a runable project to us and let us check it.

let me try it from another angle, here was my original approach, write my HTML string to an aspose.words word doc, then translate that to a pdf, here is my code. this was actually working except the text on the resulting pdf was all jumbled and overlapping each other. however, now i can't seem to even get that to happen, i'm getting "Memory stream is not expandable. " error on the WDoc.Save(ms2, Aspose.Words.SaveFormat.AsposePdf)

line of code

**************

Dim buffer() As Byte
Dim encoder As New System.Text.ASCIIEncoding
Dim str As String = "

" + FN(dat, "ctext") + "

"

ReDim buffer(str.Length - 1)
encoder.GetBytes(str, 0, str.Length, buffer, 0)

Dim ms As MemoryStream = New MemoryStream(buffer, False)

Dim license As Aspose.Words.License = New Aspose.Words.License
license.SetLicense(TempPath + "bin\Aspose.Words.lic")
Dim license2 As Aspose.pdf.License = New Aspose.pdf.License
license2.SetLicense(TempPath + "bin\Aspose.PDF.lic")

Dim WDoc As Aspose.Words.Document = New Aspose.Words.Document(ms)
WDoc.Save(TempPath + "temp\" + TempFileName + ".doc", Aspose.Words.SaveFormat.Html)

Dim ms2 As MemoryStream = New MemoryStream(buffer)
Dim xml As XmlDocument = New XmlDocument
WDoc.Save(ms2, Aspose.Words.SaveFormat.AsposePdf)
ms2.Position = 0
xml.Load(ms2)
Dim pdf As New Aspose.Pdf.Pdf
pdf.IsTruetypeFontMapCached = False
pdf.BindXML(xml, Nothing)
ms2.Close()
Dim pdfMS As MemoryStream = New MemoryStream
pdf.Save(TempPath + "Temp\" + TempFileName)

WDoc = Nothing
pdf = Nothing

Dim ReadStream As System.IO.FileStream = New System.IO.FileStream(TempPath + "Temp\" + TempFileName, FileMode.Open, FileAccess.Read)

Dim FileLen As Integer
Dim arrFile(ReadStream.Length) As Byte
ReadStream.Read(arrFile, 0, ReadStream.Length)

Response.ContentType = "application/pdf"
Response.BinaryWrite(arrFile)
Response.Flush()
ReadStream.Close()

I think there is some error in your code to use Aspose.Words but I am not familiar with that. Please contact the Aspose.Words team if necessary.

I tested your previous code again with a web app. Only the path is changed. I get no error. I also let our support developer Adeel tested this issue and he has not get any error either.

I was able to get past that, now i'm back to my very original problem. Code below, i'm taking my html string, converting it to a word doc, then to a pdf. it does it, but the text ends up smushed together. it looks ok in the word doc, just not in the pdf after the conversion.

***** CODE ******

Dim buffer() As Byte
Dim encoder As New System.Text.ASCIIEncoding
Dim str As String = "

" + FN(dat, "ctext") + "

"
ReDim buffer(str.Length - 1)
encoder.GetBytes(str, 0, str.Length, buffer, 0)

Dim ms As MemoryStream = New MemoryStream(buffer, False)

Dim license As Aspose.Words.License = New Aspose.Words.License
license.SetLicense(Request.ServerVariables("APPL_PHYSICAL_PATH") + "bin\Aspose.Words.lic")
Dim license2 As Aspose.pdf.License = New Aspose.pdf.License
license2.SetLicense(Request.ServerVariables("APPL_PHYSICAL_PATH") + "bin\Aspose.PDF.lic")

Dim WDoc As Aspose.Words.Document = New Aspose.Words.Document(ms)
WDoc.Save(Request.ServerVariables("APPL_PHYSICAL_PATH") + "temp\" + TempFileName + ".doc",SaveFormat.Html)

Dim ms2 As MemoryStream = New MemoryStream
Dim xml As XmlDocument = New XmlDocument
WDoc.Save(ms2, Aspose.Words.SaveFormat.FormatAsposePdf)
ms2.Position = 0
xml.Load(ms2)
Dim pdf As New Aspose.Pdf.Pdf
pdf.IsTruetypeFontMapCached = False
pdf.BindXML(xml, Nothing)
ms2.Close()
Dim pdfMS As MemoryStream = New MemoryStream
pdf.Save(Request.ServerVariables("APPL_PHYSICAL_PATH") + "temp\" + TempFileName)

WDoc = Nothing
pdf = Nothing

Dim ReadStream As System.IO.FileStream = New System.IO.FileStream(Request.ServerVariables("APPL_PHYSICAL_PATH") + "temp\" + TempFileName, FileMode.Open, FileAccess.Read)

Dim FileLen As Integer
Dim arrFile(ReadStream.Length) As Byte
ReadStream.Read(arrFile, 0, ReadStream.Length)

Response.ContentType = "application/pdf"
Response.BinaryWrite(arrFile)
Response.Flush()
ReadStream.Close()

**** END CODE ******

Hi,

We have tried several times with the same code you have posted, but we dont find any problems. Please tell us which version of Aspose.Pdf and Aspose.Words you are using. Secondly, please send us the full HTML Text string instead of FN(dat, “ctext”) statement. So we can recheck again.

Thanks.

Adeel Ahmad
Support Developer
Aspose Changsha Team
http://www.aspose.com/Wiki/default.aspx/Aspose.Corporate/ContactChangsha.html

just responded to your last email with attachments

Hi,

I didn’t get it .Please send your file attachments again.

Thanks.

Adeel Ahmad
Support Developer
Aspose Changsha Team
http://www.aspose.com/Wiki/default.aspx/Aspose.Corporate/ContactChangsha.html

Hello,

When they replied to the automated email, it came to me. I have just forwarded them to you. You should receive in a few minutes. Have a good day.

Hi,

I have checked in detail. And I wont be able to find any errors. You have mentoned Aspose.Words version 4.2 and Aspose.PDF 3.3. Please check again your version, I need to check the whole version means, I have with me Aspose.PDF 3.3.5.0. Please tell full version. If the version is same, then I will send you the demo project I am using.

Thanks.

Adeel Ahmad
Support Developer
Aspose Changsha Team
http://www.aspose.com/Wiki/default.aspx/Aspose.Corporate/ContactChangsha.html

it looks like the latest version of pdf was what i needed, thanks.