I had been struggling for a few days to try and figure out how to do this. I found a few solutions that were partial answers that finally lead me to this. This will print directly to the default printer without stopping for directions. It doesn't require a special view or partial view. It's just clean. I have some cleanup to do on it, but so far it works quite well. Hopefully someone else finds it useful!
I'm using a custom Knockoutjs grid to start the code
I'm using a custom Knockoutjs grid to start the code
@Html.Hidden("Report", Url.Action("Reports"))
<
script
id
=
"OHSTemplate"
type
=
"text/html"
>
<
tr
>
<
td
class
=
"formno"
>${formNo}</
td
>
<
td
class
=
"name"
>${ name }</
td
>
<
td
class
=
"online"
>-</
td
>
<
td
data-bind
=
"click: $parent.PrintCurrent.bind($data, fileName)"
class
=
"print"
></
td
>
</
tr
>
</
script
>
//This is Javascript
PrintCurrent: function (file) {
var Url, d;
Url = $('#Report').val();
d = { fileName: file };
$.ajaxSettings.traditional = true;
$.ajax({
type: "GET",
url: Url,
data: d,
contentType: "application/json;",
dataType: "json",
success: function (response) {
//response will give success or failure text (response.Result)
}
});
}
//This is c# in my controller
public class Results
{
string result;
public Results()
{
}
public string Result
{
get
{
return this.result;
}
set
{
this.result = value;
}
}
}
public JsonResult Reports(string fileName)
{
if(fileName != null)
{
Results results = new Results();
List<
Report
> reports = new List<
Report
>();
//Add each Telerik report you have that you'll be searching through
Report r = new Report();
r = (from c in reports
where c.Name == fileName
select c).FirstOrDefault();
//Configure Printer Settings
System.Drawing.Printing.PrinterSettings printerSettings = new System.Drawing.Printing.PrinterSettings();
if (printerSettings.CanDuplex)
{
printerSettings.Duplex = System.Drawing.Printing.Duplex.Default;
}
printerSettings.DefaultPageSettings.Color = false;
//Initialize ReportProcessor
Telerik.Reporting.Processing.ReportProcessor rp = new Telerik.Reporting.Processing.ReportProcessor();
//Print Report
try
{
rp.PrintReport(r, printerSettings);
results.Result = "Print Successful";
}
catch (Exception e)
{
results.Result = "Failed to print report because: " + e.InnerException;
}
return Json(results, JsonRequestBehavior.AllowGet);
}
return null;
}