Aspose.Pdf

Sending Pdf to browser over SSL

When developing an ASP.NET application utilizing Aspose.Pdf and you are looking to send the generated Pdf to the browser then you might experience some trouble when the application is hosted on a secure website i.e. when you are trying to send the pdf to the client browser over SSL. On the client side you might experience that instead of downloading the Pdf the browser starts to download the aspx page instead.

 

To overcome this problem, instead of calling the Save function of Pdf class and passing the Response object, you should save the pdf to a memory stream and then send the stream to the browser as specified in the code bellow.

 

Code Snippet

 

[C#]

 

MemoryStream stream = new MemoryStream();

pdf.Save(stream);

Response.Clear();

Response.ClearHeaders();

Response.ClearContent();

Response.Charset = "UTF-8";

Response.AddHeader("Content-Length", stream.Length.ToString());

Response.AddHeader("content-disposition", String.Format("attachment;filename={0}", _SaveAsFileName));

Response.ContentType = "application/pdf";

Response.BinaryWrite(stream.ToArray());

Response.Flush();

Response.End();

 

[VB.NET]

 

Dim stream As New MemoryStream()

pdf.Save(stream)

Response.Clear()

Response.ClearHeaders()

Response.ClearContent()

Response.Charset = "UTF-8"

Response.AddHeader("Content-Length", stream.Length.ToString())

Response.AddHeader("content-disposition", [String].Format("attachment;filename={0}", _SaveAsFileName))

Response.ContentType = "application/pdf"

Response.BinaryWrite(stream.ToArray())

Response.Flush()

Response.End()