Storing Pdf in session?


#1

It takes a while for a large pdf to be generated. The time is spent within this call:

pdf.Save("PdfReport.pdf",SaveType.OpenInBrowser,Response);

I wonder if I could do the work ahead of the time where I need to display it in the browser. Do it while the user is busy doing other things. Keep the result in Session. When the user finally clicks [show me my PDF], I would retrieve it from the session and display it right away.

I know I could do it using a file. But this file is only useful one time so instead of saving it, serving it, then deleting it, I thought it is better to not use a physical file at all.

Any suggestions?

Thanks


#2

Hi,

Thank you for considering Aspose.

I think you can save the pdf into a memory stream and write the stream into the Response when you want to display the pdf.


#3

Can you post a few lines of sample code, if possible?

Thanks


#4

Here is a small example:

MemoryStream ms = new MemoryStream();

private void Page_Load(object sender, System.EventArgs e)
{
Pdf pdf = new Pdf();

pdf.Sections.Add().Paragraphs.Add(new Text("hello world"));

pdf.Save(ms);
Response.Write("pdf prepared.");
}

private void Button1_Click(object sender, System.EventArgs e)
{
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType="application/pdf";
ms.Position = 0;
byte[] buf = ms.GetBuffer();
Response.OutputStream.Write(buf,0,(int)buf.Length);
Response.End();
}