Error generating PDF with Rotary

Asked

Viewed 679 times

2

Guys, I’m having a problem here to generate a pdf with the data coming from the bank. My system has a register of students and their occurrences. What I wanted was that, at the time of detailing the data, I could print the data that already comes on the screen (the student’s data and the occurrences related to it), because when detailing a student, it is a specific chosen from the Index (total list of students). Highlighting 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/generatingreports-em-pdf-com-rotary w7/, but I think I’m wrong, causing this error here:
Undefined object reference for an object instance.
In that 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 a base to build 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 when searching for student image: @Html.Displaynamefor(model => model. Photo)

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

I wonder if someone could help me ?

  • What is the need to create another object? You can’t pass the aluno directly in Model?

  • So, I don’t know, because I’m following the tutrial that is on the link, and according to the tutorial, creates this other object... I didn’t understand either, but I was following right.

  • Try to pass aluno directly and see what happens.

  • taking this part "...new Student." and leaving only the licking part to pull the data and the right occurrences ?

  • That’s right. I’ll put it in answer.

  • All right, I’m trying here !

  • Eric, our issues collided, check out the arrangements I made, it is better to use Markdown instead of HTML

  • Gee man, it was really worth ! Fight, I even tried to use, but I guess I still don’t understand, and thanks for clarifying better !

Show 3 more comments

3 answers

2

The Rotary bundle7 is no longer active! When to use use the Rotary.

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

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

Any questions I’m at your disposal!

  • 1

    Dude, I was able to use the W7 in a good way. I installed it and I was able to generate the pdf quietly !

1


I solved the problem. What was happening was that I wasn’t 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 print word on the link, when clicked it goes in the controller where the Gerapdf action is, and shows the page, searching for the Id of the student who is in Hidden. Simple !

1

I see no need to assemble the second object. Possibly it is causing the error.

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 don’t know what mistake you made, but this statement

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

Ask that you have in Model the following:

[Display(Name = "Foto")]
public String Foto { get; set; }
  • Now I’m making a mistake in the html I created.... Neither and I thought it would be quiet ! Ta giving error in Razor, and it is the same error of the previous, is not finding the object...

  • @Érikthiago Update your question with the error so I can update the answer.

  • beauty, I’ll put the error you’re giving and the view I’m using as a basis to generate the pdf

  • @Érikthiago I updated the answer.

  • But in my model has this attribute. If I detail in the system, the photo appears normal... Only in the generation of pdf that gives this error....

  • Gypsy, something is wrong. Because it is causing error in the whole view. Now he is not finding the name of the student...

  • @Érikthiago Placing the breakpoint on this line: var pdf = new ViewAsPdf, how is the object aluno?

  • You’re coming as null... and I don’t understand why....

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.