Download generated PDF without saving as a physical file

Hi,

Is it possible to generate a PDF from scratch and send to the browser for download without saving as a physical file first?

Thanks

@vardanho

Thanks for contacting support.

You can save PDF document into MemoryStream after generating it and pass bytes from that MemoryStream to Response object so that it can be downloaded from browser. Please check following code snippet where I have implemented your requirement.

Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
doc.Pages.Add().Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("Hello World"));
MemoryStream ms = new MemoryStream();
doc.Save(ms);
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.Charset = "UTF-8";
Response.AddHeader("content-length", ms.Length.ToString());
Response.AddHeader("content-disposition", String.Format("attachment;filename=TestDocument.pdf", "FileName"));
Response.ContentType = "application/pdf";
Response.BinaryWrite(ms.ToArray());
Response.Flush();
Response.End();

In case of any further assistance, please feel free to contact us.


Best Regards,
Asad Ali

1 Like

Thanks @asad.ali !
This is exactly what I was looking for and couldn’t find in documentation and code examples !

@vardanho,

Thanks for the acknowledgement.

We are glad to hear that your requirement is accomplished. Furthermore, for your and others convenience, we have also added an article in our API documentation as “Sending Pdf to Browser for Download”. Please continue using our API and in the event of any further query, please feel free to contact.

1 Like