Beispiel #1
0
    protected override void Render(HtmlTextWriter writer)
    {
        if (OverrideRender)
        {
            StringBuilder sbOut = new StringBuilder();

            using (StringWriter swOut = new StringWriter(sbOut, CultureInfo.InvariantCulture))
            {
                UTF8Encoding enc = new UTF8Encoding(true);
                swOut.Write(Encoding.UTF8.GetString(enc.GetPreamble()));
                using (HtmlTextWriter htwOut = new HtmlTextWriter(swOut))
                {
                    base.Render(htwOut);
                }
            }

            PDFRenderer.RenderFile(sbOut.ToString(),
                                   PageOptions,
                                   () => // onError
            {
                util.NotifyAdminEvent("Error saving PDF", String.Format(CultureInfo.CurrentCulture, "User: {0}\r\nOptions:\r\n{1}\r\n\r\nQuery:{2}\r\n",
                                                                        CurrentUser.UserName,
                                                                        JsonConvert.SerializeObject(PageOptions),
                                                                        JsonConvert.SerializeObject(PrintOptions1.Options)), ProfileRoles.maskSiteAdminOnly);

                Response.Redirect(Request.Url.PathAndQuery + (Request.Url.PathAndQuery.Contains("?") ? "&" : "?") + "pdfErr=1");
            },
                                   (szOutputPDF) => // onSuccess
            {
                Page.Response.Clear();
                Page.Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", String.Format(CultureInfo.CurrentCulture, @"attachment;filename=""{0}.pdf""", CurrentUser.UserFullName));
                Response.WriteFile(szOutputPDF);
                Page.Response.Flush();

                // See http://stackoverflow.com/questions/20988445/how-to-avoid-response-end-thread-was-being-aborted-exception-during-the-exce for the reason for the next two lines.
                Page.Response.SuppressContent = true;                      // Gets or sets a value indicating whether to send HTTP content to the client.
                HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of execution and directly execute the EndRequest event.
            });
        }
        else
        {
            base.Render(writer);
        }
    }
        protected override void Render(HtmlTextWriter writer)
        {
            if (OverrideRender)
            {
                PDFRenderer.RenderFile(
                    PrintOptions.PDFSettings,
                    (htwOut) => { base.Render(htwOut); },
                    (szErr) => // onError
                {
                    util.NotifyAdminEvent("Error saving PDF", String.Format(CultureInfo.CurrentCulture, "{0}\r\nUser: {1}\r\nOptions:\r\n{2}\r\n",
                                                                            szErr,
                                                                            CurrentUser.UserName,
                                                                            JsonConvert.SerializeObject(PrintOptions)
                                                                            ), ProfileRoles.maskSiteAdminOnly);

                    // put the error into the session
                    PDFError = szErr;
                    Response.Redirect(Request.Url.PathAndQuery);
                },
                    (szOutputPDF) => // onSuccess
                {
                    try
                    {
                        Page.Response.Clear();
                        Page.Response.ContentType = "application/pdf";
                        Response.AddHeader("content-disposition", String.Format(CultureInfo.CurrentCulture, @"attachment;filename=""{0}.pdf""", CurrentUser.UserFullName));
                        Response.WriteFile(szOutputPDF);
                        Page.Response.Flush();

                        // See http://stackoverflow.com/questions/20988445/how-to-avoid-response-end-thread-was-being-aborted-exception-during-the-exce for the reason for the next two lines.
                        Page.Response.SuppressContent = true;                      // Gets or sets a value indicating whether to send HTTP content to the client.
                        HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of execution and directly execute the EndRequest event.
                    }
                    catch (HttpUnhandledException) { }                             // sometimes the remote host has closed the connection - allow cleanup to proceed.
                    catch (HttpException) { }
                });
            }
            else
            {
                base.Render(writer);
            }
        }