c# Rotativa issue in production server using user built template pdf generation

1.1k views Asked by At

i am developing a enterprise app in c# .net. There are many pdf documents generated in the system. I use Rotativa for pdf handling. There is one pdf which uses user created template in the system. System fetches data from the system and replaces the template variables with the dynamic values from server and downloads the pdf. This particular pdf is working fine in my local and development server, but fails in the production server. Will be glad if somebody could help. i have attached the error and the code. please have a look.

public ActionResult GenerateRedemptionLetterGift(int id, int templateId)
    {
        try
        {
            int RedTempId = templateId; //Convert.ToInt32(Request.QueryString["templateId"]); 
            int type = 2;
            RedemptionCode RedemptionObj = new RedemptionCode();
            RedemptionObj = BlObj.GetRedemptionDetail(id);
            return new Rotativa.MVC.ActionAsPdf("ReturnTemplate", new { id, RedTempId, type }) { FileName = "Redemption_Letter_" + RedemptionObj.Id.ToString() + ".pdf" };
        }
        catch (Exception ex)
        {
            throw new Exception("Main Method", ex);
        }
    }

here i call a function ReturnTemplate as ActionAsPdf where all the data is fetched and replaced in the user created template.

public ActionResult ReturnTemplate(int id, int RedTempId, int type)
    {
        try
        {
            RedemptionTemplateBO RedTemp = new RedemptionTemplateBO();
            RedTemp = BlObj.GetRedemptionTemplateForEdit(RedTempId);
            Hashtable TempStrings = new Hashtable();

            if (type == 1)
            {
                TempStrings = GenerateRedemptionHashTable(id);
            }
            else if (type == 2)
            {
                TempStrings = GenerateRedemptionHashTableGift(id);
            }

            StringBuilder builder = new StringBuilder();
            builder.Append("<html><head><title>Redemption Letter</title></head><body><style> @font-face {font-family: myFirstFont;src: url(~/fonts/Roboto-Regular.ttf);} p{font-family: 'Roboto', sans-serif;color: #3e3e3e;font-size: 15px;font-weight: 400;margin-bottom: 10px}</style>");
            builder.Append(RedTemp.TemplateContent);
            builder.Append("</body></html>");
            foreach (string key in TempStrings.Keys)
            {
                builder.Replace("[" + key + "]", (string)TempStrings[key]);
            }
            return Content(builder.ToString());
        }
        catch( Exception ex)
        {
            throw new Exception("Return Template", ex);

        }
     }

I have checked in the local using a break point, if i am getting the correct data in the string for returning in the second method. Its coming fine.

Its running fine in both local and development server. I am getting the expected pdf.

But when i run it in production. i am running into an error, and it doesnt seem to be hitting the try catch block also.

Server Error in '/' Application. Error: Failed loading page http://app.com/Redemption/ReturnTemplate/185?RedTempId=3&type=2 (sometimes it will work just to ignore this error with --load-error-handling ignore) Exit with code 1 due to network error: RemoteHostClosedError Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Exception: Error: Failed loading page http://app.com/Redemption/ReturnTemplate/185?RedTempId=3&type=2 (sometimes it will work just to ignore this error with --load-error-handling ignore) Exit with code 1 due to network error: RemoteHostClosedError

This is the last few stack trace

 Rotativa.Core.WkhtmltopdfDriver.Convert(DriverOptions options, String html) +793
 Rotativa.MVC.AsPdfResultBase.CallTheDriver(ControllerContext context) +27
 Rotativa.MVC.AsPdfResultBase.BuildPdf(ControllerContext context) +203
 Rotativa.MVC.AsPdfResultBase.ExecuteResult(ControllerContext context) +27

WkhtmltopdfDriver takes too long to respond from production server.

Could it be due to some outgoing calls in Rotativa server. But still my other pdf generations work fine with rotativa in the production server.

1

There are 1 answers

0
JayKayOf4 On

We had a similar but different error: Failed loading page:...HostNotFoundError

Basically, rotativa was trying to resolve the domain name from within the intranet, but this particular network does not allow you to access their own public facing sites using its bound domain name. Switching to .UrlAsAPdf and using the intranet IP address, fixed this.

Also test the PDF view/ page locally first, to see that it is doing what you want.