How to print direct report (.rdlc) using Web Client Print in ASP.NET MVC?
by Abdul Rehman • November 5, 2015 • Microsoft .NET Framework, Microsoft ASP.NET, Microsoft ASP.NET MVC • 0 Comments
WebClientPrint for ASP.NET is a solution for Client-side printing scenarios for Windows, Linux and MAC OS X clients, designed for ASP.NET Web Forms and MVC website projects.
We can directly print any file without showing or displaying any print dialog box with WebClientPrint. With the WebClientPrint , you can easily send any data to printers.
WebClientPrint is a client-server solution:
The Server-side component:
It is a .NET managed-code assembly (Neodynamic.SDK.WebClientPrint.dll) which is used in your ASP.NET website to generate “Client Print Jobs”. A Client Print Job allows you to specify what client printer to use and the commands you want to print or send to the client printer.
Use “the Default printer” of the client machine. Printing will be performed without displaying any dialog!
Use “a specific installed printer’s name” on the client machine. Printing will be performed without displaying any dialog! This also applies for Shared Printers on the client machine network with a UNC name/path!
Display a “printer dialog” to let the user to select the printer
Explicitly specify the client printer settings for parallel (LPT) Centronics or serial RS-232 ports or IP/Ethernet Network printers.
The Client-side component:
It is the WebClientPrint Processor (WCPP) utility that needs to be installed at the client machine just once. WCPP is a lightweight native app (without any dependencies) that processes all the “Client Print Jobs” generated by the server-side component.
Follow up these steps:
- Download & install WebClientPrint for ASP.NET
- Open Visual Studioand create a new NET MVC Website naming it PrintPdfSample
- Add a reference to SDK.WebClientPrint.dllto your project
- Open your configfile and add the following entries:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<system.web> <httpHandlers> <add verb="*" path="wcp.axd" type="Neodynamic.SDK.Web.WebClientPrint, Neodynamic.SDK.WebClientPrint"/> </httpHandlers> </system.web> <system.webServer> <handlers> <add name="WCP" verb="*" path="wcp.axd" type="Neodynamic.SDK.Web.WebClientPrint, Neodynamic.SDK.WebClientPrint"/> </handlers> </system.webServer> |
Controller.cs:
Since there is no server-side processing, the controller class is also fairly simple and it practically does nothing. It is only receiving parameter of printer name and then print report directly go to printer without show dialog. The controller class used in the example is shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
public class HomeController : Controller { // // GET: /Home/ public ActionResult Index() { return View(); } public ActionResult GetReport(string printerName) { try { LocalReport lr = new LocalReport(); string reportType = null; string path = null; try { reportType = "PDF"; path = Path.Combine(Server.MapPath("~/"), "Report.rdlc"); } catch (Exception ex) { recordId = null; } if (System.IO.File.Exists(path)) { lr.ReportPath = path; } else { return RedirectToAction("Index"); } if (System.IO.File.Exists(path)) { lr.ReportPath = path; } else { return RedirectToAction("Index"); } string mimeType; string encoding; string fileNameExtension; string deviceInfo = "<DeviceInfo>" + " <OutputFormat>" + reportType + "</OutputFormat>" + " <PageWidth>4in</PageWidth>" + " <PageHeight>2.25in</PageHeight>" + " <MarginTop>0in</MarginTop>" + " <MarginLeft>0in</MarginLeft>" + " <MarginRight>0in</MarginRight>" + " <MarginBottom>0in</MarginBottom>" + "</DeviceInfo>"; Warning[] warnings; string[] streams; byte[] pdfContent = lr.Render("PDF", deviceInfo, out mimeType, out encoding, out fileNameExtension, out streams, out warnings); string fileName = Guid.NewGuid().ToString("N") + ".pdf"; //Create a PrintFile object with the pdf report PrintFile file = new PrintFile(pdfContent, fileName); //Create a ClientPrintJob and send it back to the client! ClientPrintJob cpj = new ClientPrintJob(); cpj.PrintFile = file; if (printerName == "null") cpj.ClientPrinter = new DefaultPrinter(); else cpj.ClientPrinter = new InstalledPrinter(System.Web.HttpUtility.UrlDecode(printerName)); cpj.SendToClient(System.Web.HttpContext.Current.Response); return View(); } catch { return RedirectToAction("Index"); } } } |
View.cshtml:
The View used in the example is shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
@using Webclientprint; @{ ViewBag.Title = "Index"; } <h2>Direct Printing</h2> <script src="~/Scripts/jquery-2.1.4.min.js"></script> <script src="~/Scripts/jquery-2.1.4.js"></script> <div> <div id="loadPrinters" style="float:left; margin-left:113px; margin-top:4px"> <a onclick="javascript:jsWebClientPrint.getPrinters();" class="readmore">Click To Load installed printers</a> <br /><br /> </div> </div> <div id="installedPrinters" style="visibility: hidden; width: 200px; float: left; margin-left: 113px; margin-top: 4px"> <select name="installedPrinterName" id="installedPrinterName"></select> </div> <div> <input type="button" value="Print" id="btnPrint" /> </div> @Html.Raw(Neodynamic.SDK.Web.WebClientPrint.CreateScript(Url.Action("GetReport", "Home", null, Request.Url.Scheme))) <script type="text/javascript"> var wcppGetPrintersDelay_ms = 5000; //5 sec function wcpGetPrintersOnSuccess(){ if(arguments[0].length > 0){ var p=arguments[0].split("|"); var options = ''; for (var i = 0; i < p.length; i++) { options += '<option>' + p[i] + '</option>'; } $('#installedPrinters').css('visibility','visible'); $('#installedPrinterName').html(options); $('#installedPrinterName').focus(); $('#loadPrinters').hide(); }else{ alert("No printers are installed in your system."); } } function wcpGetPrintersOnFailure() { alert("No printers are installed in your system."); } $(document).ready(function () { $('#btnPrint').click(function () { javascript: jsWebClientPrint.print('&printerName=' + $('#installedPrinterName').val()) }) }) </script> |
But WebClientPrint occurs problem when you are enable authentication. In next article I will explain how to print directly report while window and form authentication has enabled.
