<input type="button" value="Open Report in PDF viewer" onclick="loadPdf();" />
function loadPdf() {
var jsonData = new Object();
jsonData["Id"] = "abc";
var jsonResult = JSON.stringify(jsonData);
$.ajax({
url: '@Url.Action("OpenOrderReport")',
type: 'POST',
dataType: 'json',
crossDomain: true,
traditional: true,
contentType: 'application/json; charset=utf-8',
data: jsonResult,
success: function (data) {
var _filename = data["data"];
var ws = window.open("", '_blank', "width=800,height=600,location=no,menubar=no,status=no,titilebar=no,resizable=no")
//Adding script and CSS files
ws.document.write('<!DOCTYPE html><html><head><title>PdfViewer</title><link rel='nofollow' href = "https://cdn.syncfusion.com/16.3.0.29/js/web/flat-azure/ej.web.all.min.css" rel = "stylesheet"><script src="https://cdn.syncfusion.com/js/assets/external/jquery-3.1.1.min.js"><\/script><script src="https://cdn.syncfusion.com/16.3.0.29/js/web/ej.web.all.min.js"><\/script><\/head><body>');
//div to render PDF Viewer
ws.document.write('<div style="width:100%;min-height:570px"><div id="container"><\/div><\/div>')
//Initializes the PDF Viewer
ws.document.write("<script>$(function(){ $('#container').ejPdfViewer({ serviceUrl: '../api/PdfViewer', documentPath: '" + _filename + "', })})<\/script>")
ws.document.write('<\/body><\/html>');
ws.document.close();
},
error: function (msg, textStatus, errorThrown) {
alert('Exception' + msg.responseText);
}
});
} |
public object OpenOrderReport(ReportId jsonResult)
{
var id = jsonResult.Id;
ReportWriter reportWriter = new ReportWriter();
reportWriter.ReportPath = Server.MapPath("~/Data/GroupingAgg.rdl");
reportWriter.ReportProcessingMode = ProcessingMode.Remote;
List<ReportParameter> parameters = new List<ReportParameter>();
parameters.Add(new ReportParameter() { Name = "OrderId", Labels = new List<string>() { id }, Values = new List<string>() { id } });
reportWriter.SetParameters(parameters);
var format = WriterFormat.PDF;
MemoryStream ms = new MemoryStream();
//To save the report as memory stream
reportWriter.Save(ms, format);
//Converts Memory stream into base64 string
string base64 = "data:application/pdf;base64," + Convert.ToBase64String(ms.ToArray());
var json = new { data = base64 };
return Json(json);
} |