//Without using JSReportAttibute registered in GlobalActionFilter public async Task <ActionResult> InvoiceDownloadAsync() { try { string filename = "myReport.pdf"; var invoiceModel = InvoiceModel.Example(); var htmlContent = MvcStringHelper.RenderViewToString(this.ControllerContext, "/Views/Home/Invoice.cshtml", invoiceModel); (var contentType, var generatedFile) = await GeneratePDFAsync(htmlContent); Response.Headers["Content-Disposition"] = $"attachment; filename=\"TestingApp.pdf\""; // You may save your file here //using (var fileStream = new FileStream(Path.Combine(folder, fileName), FileMode.Create)) //{ // await generatedFile.CopyToAsync(fileStream); //} // You may need this for re-use of the stream generatedFile.Seek(0, SeekOrigin.Begin); return(File(generatedFile.ToArray(), "application/pdf", filename)); } catch (Exception ex) { var test = ex; return(null); } }
public async Task <FileResult> PDFAsync() { string filename = "myReport.pdf"; var invoiceModel = InvoiceModel.Example(); var htmlContent = MvcStringHelper.RenderViewToString(this.ControllerContext, "/Views/Home/Invoice.cshtml", invoiceModel); (var contentType, var generatedFile) = await GeneratePDFAsync(htmlContent); Response.Headers["Content-Disposition"] = $"attachment; filename=" + filename; generatedFile.Seek(0, SeekOrigin.Begin); return(File(generatedFile.ToArray(), "application/pdf", filename)); }