简体   繁体   中英

Telerik MVC grid PDF Export not working

I'm testing the grid, 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 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.

Well, the export function uses a js named pako that you can find here , 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM