Error generating PDF with RotativaW7

2

Well, I'm having a problem here to generate a pdf with the data coming from the bank. My system has a student register and its occurrences. What I wanted is that, when detailing the data, I could print these data on the screen (the data of the student and the occurrences related to it), because when you detail a student, it is a specific chosen of the Index (total list of students). By clicking here, I'm using the RotativaW7 library, which supports Brazilian special characters!

What happens is that I did everything based on a tutorial that has this link: http://cleytonferrari.com/gerando-relatorios-em-pdf-com-rotativa-w7/, but I think I'm wrong, generating this error here:
Object reference not set to an instance of an object.
In this exact line:
    var model = new Student


  I'll put the code I'm using here:

Controller

public ActionResult GeraPDF(long? id)
    {
        Aluno aluno = db
             .Alunos
             .Include(x => x.Ocorrencias)
             .AsNoTracking()
             .FirstOrDefault(f => f.Id == id);

        var pdf = new ViewAsPdf
        {
            ViewName = "GeraPDF",
            Model = modelo,
            PageSize = Size.A4,
            PageMargins = new Margins { Bottom = 2, Left = 2, Right = 2, Top = 2}
        };
        return pdf;
    }

View (used as the basis for mounting the pdf)

@model CEF01.Models.Aluno

         <div>
           <dl class="dl-horizontal">
    <dt>
        @Html.DisplayNameFor(model => model.Nome)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.Nome) <br />
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.Foto)
    </dt>

    <dd>
        <img src="@Model.Foto" border="0" width="150px" height="160px" />
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.NomePai)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.NomePai)<br />
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.NomeMae)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.NomeMae)<br />
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.NomeResponsavel)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.NomeResponsavel)<br />
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.Endereco)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.Endereco)<br />
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.DataDeNascimento)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.DataDeNascimento)<br />
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.AnoLetivo)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.AnoLetivo)<br />
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.Ano)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.Ano)<br />
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.Turma)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.Turma)<br />
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.Numero)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.Numero)<br />
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.Turno)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.Turno)<br />
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.Telefone)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.Telefone)<br />
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.TelefoneContato)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.TelefoneContato)<br />
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.TelefoneResponsavel)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.TelefoneResponsavel)<br />
    </dd>

            </dl>
             </div>

           <br />
          <hr />
                  <h3>Ocorrências</h3>
               <br />
               @Html.Partial("PartialDetalhesOcorrencias", Model.Ocorrencias)

Error encountered when searching for student image: @ Html.DisplayNameFor (model => model.Foto)

The error happens as soon as I click on the link to display in the browser the pdf to be printed.

Can anyone help me?

    
asked by anonymous 05.06.2014 / 23:45

3 answers

1

I solved the problem. What was happening was that I was not passing the specific student ID, so the object came as null. So to solve the problem, I made the following adaptation:

 @Html.ActionLink("Imprimir", "GeraPDF", new { id = Model.Id})

Explaining: It shows the word print on the link, when it is clicked it goes in the controller where the action is GeraPDF, and shows the page, searching for the student ID that is hidden. Simple!

    
07.06.2014 / 14:53
2

The RotativaW7 package is no longer active! When using use Rotating .

At the time the package did not accept accentuation, then we made a fork and corrected that! We contacted the author of the Rotativa and he accepted our correction, and now the Rotativa accepts accentuation!

I wrote an updated post (using the Rotativa) for the Wild IT community Rotate PDF in ASP .Net MVC

Any questions are available to you!

    
13.06.2014 / 17:17
1

I see no need to mount the second object. Maybe he's making a mistake.

public ActionResult GeraPDF(long? id)
{
    var aluno = db
         .Alunos
         .Include(x => x.Ocorrencias)
         .AsNoTracking()
         .FirstOrDefault(f => f.Id == id);

    if (aluno != null) {
        var pdf = new ViewAsPdf
        {
            ViewName = "GeraPDF",
            Model = aluno,
            PageSize = Size.A4,
            PageMargins = new Margins { Bottom = 2, Left = 2, Right = 2, Top = 2}
        };

        return pdf;
    } else {
        // levantar erro
    }
}

EDIT

I do not know what error you had, but this statement

@Html.DisplayNameFor(model => model.Foto)

Asks you to have in the Model the following:

[Display(Name = "Foto")]
public String Foto { get; set; }
    
06.06.2014 / 00:09