void ProcessAjaxRequest(HttpContext context)
 {
     try
     {
         switch (context.Request.QueryString["type"])
         {
             case "html":
                 context.Response.BufferOutput = true;
                 using (var htmlWriter = new HtmlTextWriter(context.Response.Output))
                 {
                     var htmlGen = new Codaxy.CodeReports.Exporters.Html.HtmlReportWriter(htmlWriter);
                     htmlGen.RegisterCss(DextopUtil.AbsolutePath("client/css/report.css"));
                     htmlGen.Write(Report, new DefaultHtmlReportTheme());
                 }
                 break;
             case "xlsx":
                 context.Response.BufferOutput = true;
                 var xlsxGen = new XlsxReportWriter();
                 context.Response.ContentType = "application/force-download";
                 context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + Guid.NewGuid().ToString() + ".xlsx" + "\"");
                 XlsxReportWriter.WriteToStream(Report, Themes.Default, context.Response.OutputStream);
                 context.Response.Flush();
                 break;
             case "pdf":
                 context.Response.BufferOutput = true;
                 context.Response.ContentType = "application/pdf";
                 //context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + Guid.NewGuid().ToString() + ".pdf" + "\"");
                 PdfConvert.ConvertHtmlToPdf(new PdfDocument
                 {
                     Url = context.Request.Url.ToString().Replace("=pdf", "=html")
                 }, new PdfOutput
                 {
                     OutputStream = context.Response.OutputStream
                 });
                 context.Response.Flush();
                 break;
             default:
                 context.Response.ContentType = "text/plain";
                 context.Response.Write("Invalid options");
                 break;
         }
     }
     catch (Exception ex)
     {
         context.Response.ContentType = "text/plain";
         context.Response.Clear();
         context.Response.Output.WriteLine("Error occured: " + ex);
     }
 }
Example #2
0
        void RenderReport(HttpContext context)
        {
            var type = context.Request.QueryString["type"];

            Report report;
            switch (type)
            {
                case "GdpByYear":
                    report = Reports.GdpByYearReport.Generate();
                    break;
                case "GdpByCountry":
                    report = Reports.GdpByCountryReport.Generate();
                    break;
                case "BiggestCountries":
                    report = Reports.GdpYearColumnReport.BiggestCountries();
                    break;
                case "FastestGrowingCountries":
                    report = Reports.GdpYearColumnReport.FastestGrowingCountries();
                    break;
                case "BestCountries":
                    report = Reports.GdpYearColumnReport.BestCountries();
                    break;
                default:
                    throw new InvalidOperationException("Invalid report type.");
            }

            try
            {

                switch (context.Request.QueryString["format"])
                {
                    case "pdf":
                        var url = context.Request.Url.ToString().Replace("format=pdf", "print=1");
                        PdfConvert.ConvertHtmlToPdf(new PdfDocument { Url = url }, new PdfOutput { OutputStream = context.Response.OutputStream });
                        context.Response.SetFileDownloadHeaders(String.Format("Report{0}.pdf", DateTime.Now.Ticks));
                        break;
                    case "text":
                        context.Response.SetFileDownloadHeaders(String.Format("Report{0}.txt", DateTime.Now.Ticks));
                        TextReportWriter.WriteTo(report, context.Response.Output);
                        break;
                    case "xlsx":
                        context.Response.SetFileDownloadHeaders(String.Format("Report{0}.xlsx", DateTime.Now.Ticks));
                        XlsxReportWriter.WriteToStream(report, Themes.Default, context.Response.OutputStream);
                        break;
                    default:
                        var output = new HtmlTextWriter(context.Response.Output);
                        var htmlWriter = new HtmlReportWriter(output);
                        htmlWriter.RegisterCss(DextopUtil.AbsolutePath("client/css/report.css"));
                        if (context.Request.QueryString["print"] == null)
                            htmlWriter.RegisterCss(DextopUtil.AbsolutePath("client/css/report-preview.css"));
                        htmlWriter.Write(report, new DefaultHtmlReportTheme());
                        break;
                }

            }
            catch (Exception ex)
            {
                context.Response.Write(ex.ToString());
            }
        }