We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

ASP.NET Core - Sending a generated PDF directly to browser

Hi

How can I send a generated PDF directly to the browser in ASP.NET Core 1.0 ?



Thanks




2 Replies

AN Andrew Nambudripad July 30, 2016 11:34 AM UTC

Your question isn't clear (e.g. what platform you're deploying to is a big factor) - but you've got two basic ways of doing this. 

1) Just use the standard return a FileResult object through your controller action (if it's a 'one and done' dynamically generated report, e.g.)
2) if you need to retain a local copy (audit reasons, or company policies, etc), I'd generate the PDF, persist the stream to disk, then either a) use IApplicationBuilder.UseStaticFiles() and a FileProvider, or b) place the item statically in front of the application server (e.g. if you're using S3, Azure Blob Storage, or Nginx to host your static files, serve the data from there --- no need to bog down your app servers)


-----
Andrew Nambudripad
Amplete Software
amplete.com


CM Chinnu Muniyappan Syncfusion Team August 1, 2016 11:42 AM UTC

Hi Clovis, 

Thank you for contacting Syncfusion support. 

We have created as simple sample for generating PDF files and downloading via browser in Asp.net Core for your reference, please refer the below code snippet and sample for more details. 
1.       Save the PDF to the local disk. 
            //Save the PDF file in local disk.            
            string path = hostingEnvironment.ContentRootPath; 
 
            document.Save(path + "/output.pdf"); 

2.       Download the PDF: 
//Saving the PDF to the MemoryStream 
MemoryStream ms = new MemoryStream(); 
   document.Save(ms); 
 
   //If the position is not set to '0' then the PDF will be empty. 
   ms.Position = 0; 
 
   //Download the PDF document in the browser. 
   FileStreamResult fileStreamResult = new FileStreamResult(ms, "application/pdf"); 
 
   fileStreamResult.FileDownloadName = "Sample.pdf"; 
 
return fileStreamResult; 

Sample link: 


Regards, 
Chinnu 


Loader.
Up arrow icon