0
votes

I am creating a pos application in asp.net mvc 5 and I create Invoice using crystal report, now when sale button will click I want to print that invoice, my code:

$.ajax({
            type: 'POST',
            contentType: 'application/json',
            dataType: 'json',
            url: '@Url.Action("SaveData", "PointOfSale")',
            data: dataList,
            success: function (data) {
                if (data.isRedirect) {
                    window.setTimeout(function() {
                        window.location = data.redirectUrl;
                    },700);
                    toastr.success("Save Successfully.");
                }
            }
        });

my controller:

public void GenerateInvoice(string invoice)
    {
        var invoiceData = _reportManager.InvoicePrint(invoice);
        var strPath = Path.Combine(Server.MapPath("~/Reports/Invoice.rpt"));
        DataSet objDataSet = invoiceData;
        DataTable dataTable = objDataSet.Tables[0];

        using (ReportDocument report = new ReportDocument())
        {
            report.Load(strPath);
            report.Database.Tables["VEW_RPT_INVOICE_PRINT"].SetDataSource((DataTable)dataTable);
            report.SetDatabaseLogon("saraecom", "saraecom");
            report.PrintOptions.PrinterName = "EPSON TM-T82 Receipt";
            report.PrintToPrinter(1, false, 0, 0);
        }
    }

but this code just print invoice only in server printer, how can I print it in client computer?

I change PrintToPrinter to report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "crReport");

this code download report as pdf, how can i print it in client Printer instead of downloading...

thanks in advance....

1

1 Answers

0
votes

Friend, You can use this method to print crystal reports in clients PCs in ASP.NET MVC 5.

I am using this method already.

You just call ActionResult method from on your button click from View like this :

<a href="/VisitDetails/PrintVisitorPass/@Model.id" class="btn btn-default ">Print Userpass</a>

Then create a ActionResult method in your controller like this :

    public ActionResult PrintVisitorPass(string id)
    {

        var visitList = (from visitor in db.VisitorDetails
                             where visitor.invoiceid == id
                            select visitor).ToList();

        ReportDocument rd = new ReportDocument();
        rd.Load(Path.Combine(Server.MapPath("~/CrystalReports"), "VisitorPass2.rpt"));
        rd.SetDataSource(visitList);

        try
        {
            rd.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "crReport");
            return RedirectToAction("ActionName", "ControllerName", new { id = id });
        }
        catch (Exception ex) { return RedirectToAction(ex.ToString()); }
    }

I hope this code will help you.