IronPDF: using ChromePdfRenderer.RenderUrlAsPdf

701 Views Asked by At

I'm using the ChromePdfRenderer.RenderUrlAsPdf method to render a ASP.NET blazor page.

That page is using some internal methods from the model. When everything is good, it works fine. The problem is that when any of those methods fails, the server returns an error page, with HTTP status code = 500 and IronPDF is generating the pdf with that error page in it. (Of course, that's what the url is returning)

My question: Is there a way of getting the HTTP status code that IronPDF is getting internally, so I can decide based on this, if the genrated PDF is valid or not?

Note: I know I could get the html from the url first, and then use the "RenderHtmlAsPdf" methods, but that's not the point.

1

There are 1 best solutions below

2
Ali On

You need to code your method with try{} catch{} conditions with Exception Handler.

public async Task<PdfDocument> GenerateIronPDF(string Url)
{
    try
    {
         var Renderer = new ChromePdfRenderer();
         Renderer.RenderingOptions.RenderDelay = 10000;
         var pdf = Renderer.RenderUrlAsPdf(Url);
         return pdf;
    }
    catch (Exception ex)
    {
        await _exceptionService.HandleException(ex, 
        $"YourSerService/GenerateIronPDF/pdf", string.Empty);
    }

}

Create another Service and call it ExceptionService

namespace Whatever.Services
{
    public class ExceptionService : IExceptionService
    {
        private ILogger<ExceptionService> _logger;

        public ExceptionService()
        {

        }

        public ExceptionService(ILogger<ExceptionService> logger)
        {
            _logger = logger;
        }

        public async Task HandleException(HttpContext httpContext, Exception ex, LogLevel logLevel = LogLevel.Warning)
        {
            try
            {
                string absoluteUri = string.Concat(
                        httpContext.Request.Scheme,
                        "://",
                        httpContext.Request.Host.ToUriComponent(),
                        httpContext.Request.PathBase.ToUriComponent(),
                        httpContext.Request.Path.ToUriComponent(),
                        httpContext.Request.QueryString.ToUriComponent());
                string username = httpContext.User.Identity.Name;
                await HandleException(ex, absoluteUri, username, logLevel);
            }
            finally
            {

            }
        }

        public async Task HandleException(Exception ex, string uri, string username, LogLevel logLevel = LogLevel.Warning)
        {
            try
            {
                string platform = "yourWeb.com";
                var sb = new StringBuilder().Append("<h2>Error Information</h2>");
                sb.Append("<p>Exception type: ").Append(ex.GetType()).Append("</p>");
                sb.Append("<p>Exception message: ").Append(ex.Message).Append("</p>");
                if (ex.InnerException != null && !string.IsNullOrEmpty(ex.InnerException.Message))
                {
                    sb.Append("<p>Inner exception: ").Append(ex.InnerException.Message);
                }
                sb.Append("<p>Request Url: ").Append(uri).Append("</p>");
                sb.Append("<p>User: ").Append(username).Append("</p>");
                sb.Append("<p>Stack trace:</p><p>").Append(ex.StackTrace.Replace(" at", "<br/>at")).Append("</p>");
                if(_logger != null)
                {
                    _logger.Log(logLevel, sb.ToString());
                }
                if (!uri.Contains("localhost"))
                {
                    await RDS2.Shared.EmailService.SendEmailAsync("[email protected]", "Error Handled on " + platform, sb.ToString());
                }
            }
            finally
            {

            }
        }
    }

    public interface IExceptionService
    {
        Task HandleException(HttpContext httpContext, Exception ex, LogLevel logLevel = LogLevel.Warning);
        Task HandleException(Exception ex, string uri, string username, LogLevel logLevel = LogLevel.Warning);
    }
}