如何解决CrystalReport PrintToPrinter方法的问题

时间:2019-04-01 12:15:28

标签: c# asp.net-mvc iis printing crystal-reports

我已经在Windows 10上设置了iis服务器,并且已经在其上部署了mvc-5 Web应用程序,但除Crystal Reports PrintToPrinter之外,其他所有功能都可以正常工作,当我尝试调用此方法时,不会打印任何内容,而Web应用程序则无任何作用并且只是挂在那里。但是当我在同一台机器上使用vs17开发此应用程序时,printToPrinter方法工作正常。它只有在iis上发布后才卡住。

我尝试过

popt.PrinterName = printerSettings.PrinterName;             rd.ReportClientDocument.PrintOutputController.PrintReport(popt);

此方法也是如此。 这是我的实际代码:

        DataTable dt = new DataTable();
        string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(sqlQuery))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    sda.Fill(dt);
                }
            }
        }

        ReportDocument rd = new ReportDocument();
        rd.Load(Path.Combine(Server.MapPath("~/Reporting/Crystals/rptKitchenCopy.rpt")));


        rd.SetDataSource(dt);
        Response.Buffer = false;
        Response.ClearContent();
        Response.ClearHeaders();
        Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
        stream.Seek(0, SeekOrigin.Begin);
        PrinterSettings settings = new PrinterSettings();

        System.Drawing.Printing.PrintDocument pDoc = new System.Drawing.Printing.PrintDocument();

        CrystalDecisions.Shared.PrintLayoutSettings PrintLayout = new CrystalDecisions.Shared.PrintLayoutSettings();

        System.Drawing.Printing.PrinterSettings printerSettings = new System.Drawing.Printing.PrinterSettings();
        var dpn = settings.PrinterName;
        printerSettings.PrinterName = dpn;

        System.Drawing.Printing.PageSettings pSettings = new System.Drawing.Printing.PageSettings(printerSettings);

        CrystalDecisions.ReportAppServer.Controllers.PrintReportOptions popt = new CrystalDecisions.ReportAppServer.Controllers.PrintReportOptions();
        popt.PrinterName = printerSettings.PrinterName;
        rd.ReportClientDocument.PrintOutputController.PrintReport(popt);



        // rd.PrintOptions.PrinterName = "\\ ";
       // rd.PrintToPrinter(printerSettings, pSettings, false, PrintLayout);
       // rd.PrintToPrinter(1, true, 1, 1);

        return File(stream, "application/pdf", "KitchenCopy.pdf");

我希望它也可以使用我在同一台计算机上托管的默认打印机在iis服务器上打印。

1 个答案:

答案 0 :(得分:0)

我发现了导致此问题的原因,因为我在Windows 10(都在同一台机器上)上托管和开发了Web应用程序,该问题的原因是默认情况下,我的Windows 10 iis正在检测“ Microsoft打印为pdf”作为默认打印机,因此我已经从Windows功能中关闭了此功能,并添加了

PrinterSettings设置=新的PrinterSettings(); rd.PrintOptions.PrinterName = settings.PrinterName;

在我的代码中。希望它会遇到同样的问题。谢谢