Create one PDF from one view with RazorPDF MVC 5

1.8k views Asked by At

I am new using Asp.net MVC 5 in C#.

I am trying to create one PDF from one view but I am not managing. After some days trying for my own, then i began to read in some forums, but I didn't find (or maybe I didn't understand) how to do it.

I installed, from NuGet, RazorPDF, RazorPDF2, and also RazorPDF for MVC. I used iTextSharp 5.0.5.0 and then updated to 5.5.8.0. But in any case, it worked.

I had different errors. First was this one:

RazorPDF.Legacy.Text.DocumentException: Helvetica not found as resource

and then:

Could not load type 'iTextSharp.text.html.HtmlParser' from assembly 'itextsharp, Version=5.5.8.0, Culture=neutral, PublicKeyToken=8354ae6d2174ddca'

I read one fantastic article here wrote by Chris Hass. But I don't know how to apply it. So my question is, how to make it work in my precise case?

This is what I have in my controller:

public ActionResult Factura(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Venta venta = db.Ventas.Find(id);
    if (venta == null)
    {
        return HttpNotFound();
    }

    var detalleVenta = from c in db.DetalleVentas where c.VentaID ==   venta.VentaID select c;

    return new RazorPDF.PdfResult(detalleVenta,"Factura");
}

And this is my view:

@model IEnumerable<MiContexto.Models.DetalleVenta>

<table class="table">
    <tr>
    <th>
        @Html.DisplayNameFor(model => model.NombreProducto)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Cantidad)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Descuento)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.PrecioSinIva)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Iva)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Precio)
    </th>
</tr>
@foreach (var item in Model) {

<tr>
    <td>
        @Html.DisplayFor(modelItem => item.NombreProducto)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Cantidad)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Descuento)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.PrecioSinIva)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Iva)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Precio)
    </td>

</tr>

}

</table>

So what should I do to create the pdf from this view? Do I have to use "RazorPDF" or "RazorPDF for MVC" or maybe another tool in the NuGet? Is my code wrong or the 'only' problem of what I made is that "It hasn't been updated in two and a half years, it requires an obsolete version of iTextSharp and uses an obsolete HTML parser." as Chris has said?

0

There are 0 answers