简体   繁体   English

在ASP.NET MVC 5的客户端PC中从Crystal Report打印

[英]Print From Crystal Report In client PC in ASP.NET MVC 5

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: 我正在asp.net mvc 5中创建一个pos应用程序,并使用水晶报表创建了发票,现在当销售按钮将单击时,我要打印该发票,我的代码:

$.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"); 我将PrintToPrinter更改为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... 此代码以pdf格式下载报告,如何在客户端打印机中打印而不是下载...

thanks in advance.... 提前致谢....

Friend, You can use this method to print crystal reports in clients PCs in ASP.NET MVC 5. 朋友,您可以使用此方法在ASP.NET MVC 5的客户端PC中打印水晶报表。

I am using this method already. 我已经在使用这种方法。

You just call ActionResult method from on your button click from View like this : 您只需从View上单击按钮就可以调用ActionResult方法,如下所示:

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

Then create a ActionResult method in your controller like this : 然后像这样在您的控制器中创建一个ActionResult方法:

    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. 希望这段代码对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM