This is a migrated thread and some comments may be shown as answers.

Telerik MVC grid PDF Export not working

2 Answers 258 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Federico
Top achievements
Rank 1
Federico asked on 22 Feb 2016, 06:20 PM

I'm testing the grid for an almost certain purchase, and I've tried to export it to both PDF and XLS. With Excel it was almost straight forward, but when I try to follow the steps presented on the demo (http://demos.telerik.com/aspnet-mvc/Beta/grid/pdf-export) it does not work. When I click on the export button, the progress bar loads and when it finishes nothing happens, the action never gets to be executed.

This is my view:

@model IEnumerable<ViewModels.TestViewModel>

@{
    ViewBag.Title = "Tests KendoUI";
}

<script src="~/Scripts/lib/jszip.min.js" type="text/javascript"></script>
<script src="~/Scripts/lib/pako.min.js" type="text/javascript"></script>

<style>
    /*
                Use the DejaVu Sans font for display and embedding in the PDF file.
                The standard PDF fonts have no support for Unicode characters.
            */
    .k-grid {
        font-family: "DejaVu Sans", "Arial", sans-serif;
    }

    /* Hide the Grid header and pager during export */
    .k-pdf-export .k-grid-toolbar,
    .k-pdf-export .k-pager-wrap,
    .k-pdf-export a.k-button.k-button-icontext,
    .k-pdf-export .k-filter-row,
    .k-pdf-export .k-grouping-header,
    .k-pdf-export .k-grid tr td:last-child {
        display: none !important;
    }
</style>

@(Html.Kendo().Grid(Model)
      .Name("grid")
      .DataSource(dataSource => dataSource
          .Ajax()
          .Read(read => read.Action("Products_Read", "Home"))
          .Aggregates(aggregates => aggregates.Add(p => p.oper_monto).Sum())
          )
      .Columns(columns =>
      {
        columns.Bound(p => p.oper_numero).Hidden(true);
        columns.Bound(p => p.oper_monto).ClientFooterTemplate("Total: #=sum#");
        columns.Bound(p => p.cpto_codigo);
      })
      .Excel(excel => excel
        .FileName("Reporte.xlsx")
        .Filterable(true)
        .AllPages(true)
        .ProxyURL(Url.Action("Excel_Export_Save", "Home"))
      )
      .Pdf(pdf => pdf
            .AllPages()
            .FileName("Reporte.pdf")
            .ProxyURL(Url.Action("Pdf_Export_Save", "Home"))
        )
      .ColumnMenu()
      .ToolBar(t => t.Excel().Text("Exportar a Excel"))
      .ToolBar(t => t.Pdf().Text("Exportar a PDF"))
      .ToolBar(toolBar => 
                    toolBar.Custom()
                        .Text("Guardar Preferencias")
                        .HtmlAttributes(new { id = "save" })
      )
      .ToolBar(toolBar =>
                    toolBar.Custom()
                        .Text("Cargar Preferencias")
                        .HtmlAttributes(new { id = "load" })
      )                      
      .Filterable(ftb => ftb.Mode(GridFilterMode.Menu))
)

<script>

    $(function () {
        var grid = $("#grid").data("kendoGrid");

        $("#save").click(function (e) {
            e.preventDefault();
            localStorage["kendo-grid-options"] = kendo.stringify(grid.getOptions());
        });

        $("#load").click(function (e) {
            e.preventDefault();
            var options = localStorage["kendo-grid-options"];
            if (options) {
                grid.setOptions(JSON.parse(options));
            }
        });
    });
</script>

And this is my controller action:

[HttpPost]
        public ActionResult Pdf_Export_Save(string contentType, string base64, string fileName)
        {
            var fileContents = Convert.FromBase64String(base64);

            return File(fileContents, contentType, fileName);
        }

 

Thanks in advance.

2 Answers, 1 is accepted

Sort by
0
Federico
Top achievements
Rank 1
answered on 22 Feb 2016, 08:02 PM
Well, the export function uses a js named pako that you can find here: https://github.com/nodeca/pako/tree/master/dist, but if you right click and download the file, it will fail and download it anyways, but the code inside it won't be the necessary javascript. So, I had to enter the file, copy all the text and paste it in my local file. It works like a charm.
0
Dimiter Madjarov
Telerik team
answered on 24 Feb 2016, 11:51 AM

Hello Federico,

Thanks for the update. I am glad the export issue is resolved.

Regards,
Dimiter Madjarov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Federico
Top achievements
Rank 1
Answers by
Federico
Top achievements
Rank 1
Dimiter Madjarov
Telerik team
Share this question
or