locked
Rotativa.AspNetCore not working after deployment in smarterasp RRS feed

  • Question

  • User493825449 posted

    In my latest NetCore(2.0) project I am using Rotativa.AspNetCore, everything worked fine(locally) since I published it to SmarterAsp.

    The main problem is that when I'm trying to generate PDF file through my api, the action is not working, is returning an error:

    Requested page not found. [404]

    XML Parsing Error: no root element found

    Location: https://app.oik-ks.org/api/file/MembershipRegistrationApplication

    Line Number 1, Column 1:

    [HttpPost]
    [AllowAnonymous]
    [Route("/api/file/MembershipRegistrationApplication")]
    public async Task<byte[]> MembershipRegistrationApplication([FromBody] RegisterViewModel model)
    {
        var education = new List<Education>();
    
        education.Add(_educationService.IncludeEducation(model.Education));
    
        var user = new ApplicationUser
        {
            Email = model.Email,
            PhoneNumber = model.PhoneNumber,
            BirthDate = model.BirthDate,
            Address = model.Address,
            PlaceOfBirth = model.PlaceOfBirth,
            MunicipalityOfBirthId = model.MunicipalityId,
            IsMale = model.IsMale,
            FathersName = model.FathersName,
            LastName = model.LastName,
            MaritalStatusId = model.MaritalStatusId,
            CitizenshipId = model.CitizenshipId,
            MunicipalityId = model.MunicipalityId,
            FirstName = model.FirstName,
            NationalityId = model.NationalityId,
            PersonalNumber = model.PersonalNumber,
            ForeignMunicipalityOfBirth = model.ForeignMunicipalityOfBirth,
            Educations = education,
            MaidenName = model.MaidenName
        };
    
        user = _accountService.IncludeApplicationUser(user);
        return await _pdfGenerator.GenerateMembershipApplication(user);
    }

    GenerateMembershipApplication method code

    public async Task<byte[]> GenerateMembershipApplication(ApplicationUser user)
    {
        var httpContext = new DefaultHttpContext { RequestServices = _serviceProvider };
        var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
    
        var bytePDF = new Rotativa.AspNetCore.ViewAsPdf("File/MembershipApplication", user)
        {
            PageMargins = { Top = 10, Right = 1, Bottom = 1, Left = 20 },
            FileName = string.Concat("MembershipApplication", ".pdf")
        }.BuildFile(actionContext);
    
       return await bytePDF;
    }

    and ajax call

    $.ajax({
        url: "/api/file/MembershipRegistrationApplication",
        type: 'post',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: JSON.stringify(model),
        success: function (data) {
            var pdfAsDataUri = "data:application/pdf;base64,"+data;
            window.open(pdfAsDataUri);
        },
        error: function (jqXHR, exception) {
            var msg = '';
            if (jqXHR.status === 0) {
                msg = 'Not connect.\n Verify Network.';
            } else if (jqXHR.status == 404) {
                msg = 'Requested page not found. [404]';
            } else if (jqXHR.status == 500) {
                msg = 'Internal Server Error [500].';
            } else if (exception === 'parsererror') {
                msg = 'Requested JSON parse failed.';
            } else if (exception === 'timeout') {
                msg = 'Time out error.';
            } else if (exception === 'abort') {
                msg = 'Ajax request aborted.';
            } else {
                msg = 'Uncaught Error.\n' + jqXHR.responseText;
            }
            console.log(msg);
        }
    });

    First I thought that the problem is with data that are being posted by ajax call(I tried also with formData,but still the same error) and after many hours trying to figure out what is happening I found that the cause of error was Rotativa.Another reason why I think that Rotativa is causing the problem is that other APIs that dont relate to pdf generation process work perfectlly.

    My Rotativa folder also has necessary files,

    • help-wkhtmltoimage.txt
    • help-wkhtmltopdf.txt
    • msvcp120.dll
    • msvcp140.dll
    • msvcr120.dll
    • vcruntime140.dll
    • wkhtmltoimage.exe
    • wkhtmltopdf.exe wkhtmltox.dll

    Does anyone have any idea or any suggestion what should I do to solve this problem?

    Friday, September 28, 2018 2:37 PM

All replies