saveAsPDF() function is used to implement the export to PDF functionality.
- <!DOCTYPE html>
- <html>
- <head>
-
- <style>
- html {
- font-size: 14px;
- font-family: Arial, Helvetica, sans-serif;
- }
- </style>
- <title></title>
- <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.3.1017/styles/kendo.common-material.min.css" />
- <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.3.1017/styles/kendo.material.min.css" />
- <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.3.1017/styles/kendo.material.mobile.min.css" />
-
- <script src="https://kendo.cdn.telerik.com/2018.3.1017/js/jquery.min.js"></script>
- <script src="https://kendo.cdn.telerik.com/2018.3.1017/js/jszip.min.js"></script>
- <script src="https://kendo.cdn.telerik.com/2018.3.1017/js/kendo.all.min.js"></script>
-
-
- </head>
- <body>
- <div id="example">
- <button id="exportPDF" class="k-button">Export to PDF</button>
-
- <div id="spreadsheet" style="width: 100%"></div>
- <script>
-
- $(function () {
- var dataSource = new kendo.data.DataSource({
- transport: {
- read: onRead,
-
- },
- schema: {
- model: {
- id: "ProductID",
- fields: {
- productID: { type: "number" },
- productName: { type: "string" },
- price: { type: "number" },
- tax: { type: "number" }
- }
- }
- }
- });
-
- $("#spreadsheet").kendoSpreadsheet({
- columns: 20,
- rows: 100,
- toolbar: false,
- sheetsbar: false,
- sheets: [{
- name: "Products",
- dataSource: dataSource,
- filter: {
- ref: "A1:D3",
- columns: []
- },
- rows: [{
- height: 40,
-
- cells: [
- {
- bold: "true",
- background: "#9c27b0",
- textAlign: "center",
- color: "white",
- title: "ID"
- }, {
- bold: "true",
- background: "#9c27b0",
- textAlign: "center",
- color: "white",
- title: "Product Name"
- }, {
- bold: "true",
- background: "#9c27b0",
- textAlign: "center",
- color: "white",
- title: "Price"
- }, {
- bold: "true",
- background: "#9c27b0",
- textAlign: "center",
- color: "white"
- },]
- }],
- columns: [
- { width: 100 },
- { width: 415 },
- { width: 145 },
- { width: 145 },
- { width: 145 }
- ]
- }]
- });
-
-
-
-
- function onRead(options) {
- $.ajax({
- url: "http://localhost:11207/api/Products/ProductDetails",
- dataType: "json",
- success: function (result) {
- options.success(result);
- },
- error: function (result) {
- options.error(result);
- }
- });
- }
-
-
- });
-
- $("#exportPDF").on('click', function (e) {
- exportPDF(e);
-
- })
- function exportPDF(e) {
- var spreadsheet = $("#spreadsheet").data("kendoSpreadsheet");
- spreadsheet.saveAsPDF();
- }
-
- </script>
- </div>
-
-
- </body>
- </html>
Run the application
Figure 1
Clicking on the button Export to PDF will fire exportPDF functions; in this function saveAsPDF() is used to print the spreadsheet.
Figure-2 Kendo spreadsheet exported as PDF
The area property in the Kendo spreadsheet object is used to set the area to export; it holds a string value.
“workbook” - > export full workbook, including all sheets
“sheet” -> export current sheet
“selection”-> export only selected area.
- function exportPDF(e) {
- var spreadsheet = $("#spreadsheet").data("kendoSpreadsheet");
- spreadsheet.saveAsPDF({ area: "selection" });
- }
From the above code you can notice I have given the area as selection, so it will print only the selected area from the spreadsheet.
Figure 3
Figure 4
So, as per the program only selected fields' product ID and product Name is exported to PDF
Source Code -
GitHub
Reference
https://docs.telerik.com/kendo-ui/api/javascript/ui/spreadsheet
Summary
We have seen how to export the Kendo spreadsheet to PDF and how to work with area property while exporting the content to PDF.
I hope you have enjoyed this article. Your valuable feedback, questions, or comments about this are always welcomed.