Pdf export options in Vue Grid component
28 Mar 202324 minutes to read
Export current page
PDF export provides an option to export the current page into PDF. To export current page, define the exportType
to CurrentPage.
<template>
<div id="app">
<ejs-grid ref='grid' id='Grid' :dataSource='data' :toolbar='toolbarOptions' height='220px' :allowPaging='true' :allowPdfExport='true' :toolbarClick='toolbarClick'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' width=120></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=150></e-column>
<e-column field='ShipCity' headerText='Ship City' width=150></e-column>
<e-column field='ShipName' headerText='Ship Name' width=150></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import Vue from "vue";
import { GridPlugin, Toolbar, PdfExport, Page } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
Vue.use(GridPlugin);
export default {
data() {
return {
data: data,
toolbarOptions: ['PdfExport']
};
},
methods: {
toolbarClick(args) {
if (args.item.id === 'Grid_pdfexport') { // 'Grid_pdfexport' -> Grid component id + _ + toolbar item name
let pdfExportProperties = {
exportType: 'CurrentPage'
};
this.$refs.grid.pdfExport(pdfExportProperties);
}
}
},
provide: {
grid: [Toolbar, PdfExport, Page]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>
Export the selected records only
You can export the selected records data by passing it to exportProperties.dataSource
property in the toolbarClick
event.
In the below exporting demo, We can get the selected records using getSelectedRecords
method and pass the selected data to PdfExport
or excelExport
property.
<template>
<div id="app">
<ejs-grid ref='grid' :dataSource='data' :toolbar='toolbarOptions' :allowPaging='true' :allowFiltering='true' :allowPdfExport='true' :allowExcelExport='true' :pageSettings='pageSettings' :toolbarClick='toolbarClick' :selectionSettings='selectionOption'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' width=100></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=150></e-column>
<e-column field='ShipCity' headerText='Ship City' width=150></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import Vue from "vue";
import { GridPlugin, Toolbar, PdfExport, Filter, Page, ExcelExport } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
import { DataManager } from "@syncfusion/ej2-data";
Vue.use(GridPlugin);
export default {
data: () => {
return {
data: data,
toolbarOptions: ['PdfExport', 'ExcelExport'],
pageSettings: { pageSize: 5, pageCount:5 },
selectionOption: {type: 'Multiple'}
};
},
methods: {
toolbarClick(args: ClickEventArgs) {
if (args['item'].id.indexOf("pdfexport") != -1) {
let selectedRecords = this.$refs.grid.getSelectedRecords();
let exportProperties = {
dataSource: selectedRecords
};
this.$refs.grid.pdfExport(exportProperties);
}
else if (args['item'].id.indexOf("excelexport") != -1) {
let selectedRecords = this.$refs.grid.getSelectedRecords();
let exportProperties = {
dataSource: selectedRecords
};
this.$refs.grid.excelExport(exportProperties);
}
},
},
provide: {
grid: [Toolbar, PdfExport, Filter, Page, ExcelExport]
},
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>
Export filtered data only
You can export the filtered data by defining the resulted data in exportProperties.dataSource
before export.
In the below Pdf exporting demo, We have gotten the filtered data by applying filter query to the grid data and then defines the resulted data in exportProperties.dataSource
and pass it to pdfExport
method.
<template>
<div id="app">
<ejs-grid ref='grid' :dataSource='data' :toolbar='toolbarOptions' :allowPaging='true' :allowFiltering='true' :allowPdfExport='true' :pageSettings='pageSettings'
:toolbarClick='toolbarClick'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' width=100></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=150></e-column>
<e-column field='ShipCity' headerText='Ship City' width=150></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import Vue from "vue";
import { GridPlugin,Toolbar,PdfExport,Filter,Page } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
import { DataManager } from "@syncfusion/ej2-data";
Vue.use(GridPlugin);
export default {
data: () => {
return {
data: data,
toolbarOptions: ['PdfExport'],
pageSettings: { pageSize: 5, pageCount:5 }
};
},
methods: {
toolbarClick: function (args) {
if (args['item'].id.indexOf("pdfexport") != -1) {
let pdfdata;
let query = this.$refs.grid.ej2Instances.renderModule.data.generateQuery(); // get grid corresponding query
for(var i=0; i<query.queries.length; i++ ){
if(query.queries[i].fn=='onPage'){
query.queries.splice(i,1); // remove page query to get all records
break;
}
}
let fdata = new DataManager({ json: data}).executeQuery(query).then((e) => {
pdfdata = e.result; // get all filtered records
let exportProperties = {
dataSource: pdfdata,
};
this.$refs.grid.pdfExport(exportProperties);
}).catch((e) => true);
}
},
},
provide: {
grid: [Toolbar, PdfExport, Filter, Page]
},
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>
Export hidden columns
PDF export provides an option to export hidden columns of Grid by defining the includeHiddenColumn
as true.
<template>
<div id="app">
<ejs-grid ref='grid' id='Grid' :dataSource='data' :toolbar='toolbarOptions' height='272px' :allowPaging='true' :allowPdfExport='true' :toolbarClick='toolbarClick'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' width=120></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=150></e-column>
<e-column field='ShipCity' headerText='Ship City' width=150 :visible='false'></e-column>
<e-column field='ShipName' headerText='Ship Name' width=150></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import Vue from "vue";
import { GridPlugin, Toolbar, PdfExport } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
Vue.use(GridPlugin);
export default {
data() {
return {
data: data,
toolbarOptions: ['PdfExport']
};
},
methods: {
toolbarClick(args) {
if (args.item.id === 'Grid_pdfexport') { // 'Grid_pdfexport' -> Grid component id + _ + toolbar item name
let pdfExportProperties = {
includeHiddenColumn: true
};
this.$refs.grid.pdfExport(pdfExportProperties);
}
}
},
provide: {
grid: [Toolbar, PdfExport]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>
Show or hide columns
You can show a hidden column or hide a visible column while exporting the grid using toolbarClick
and pdfExportComplete
events.
In the toolbarClick
event, based on args.item.id as Grid_pdfexport. We can show or hide columns by setting column.visible
property to true or false respectively.
In the pdfExportComplete event, We have reversed the state back to the previous state.
In the below example, we have CustomerID as a hidden column in the grid. While exporting, we have changed CustomerID to visible column and ShipCity as hidden column.
<template>
<div id="app">
<ejs-grid ref='grid' id='Grid' :dataSource='data' :toolbar='toolbarOptions' height='272px' :allowPdfExport='true' :pdfExportComplete='pdfExportComplete'
:toolbarClick='toolbarClick'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' width=120></e-column>
<e-column field='CustomerID' headerText='Customer ID' :visible='false' width=150></e-column>
<e-column field='ShipCity' headerText='Ship City' width=150></e-column>
<e-column field='ShipName' headerText='Ship Name' width=150></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import Vue from "vue";
import { GridPlugin, Toolbar, PdfExport } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
Vue.use(GridPlugin);
export default {
data() {
return {
data: data,
toolbarOptions: ['PdfExport']
};
},
methods: {
toolbarClick(args) {
if (args.item.id === 'Grid_pdfexport') {
this.$refs.grid.getColumns()[1].visible = true;
this.$refs.grid.getColumns()[3].visible = false;
this.$refs.grid.pdfExport();
}
},
pdfExportComplete(args) {
this.$refs.grid.getColumns()[1].visible = false;
this.$refs.grid.getColumns()[3].visible = true;
}
},
provide: {
grid: [Toolbar, PdfExport]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>
Change page orientation
Page orientation can be changed Landscape(Default Portrait) for the exported document using the pdfExportProperties
.
<template>
<div id="app">
<ejs-grid ref='grid' id='Grid' :dataSource='data' :toolbar='toolbarOptions' height='272px' :allowPaging='true' :allowPdfExport='true' :toolbarClick='toolbarClick'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' width=120></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=150></e-column>
<e-column field='ShipCity' headerText='Ship City' width=150></e-column>
<e-column field='ShipName' headerText='Ship Name' width=150></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import Vue from "vue";
import { GridPlugin, Toolbar, PdfExport } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
Vue.use(GridPlugin);
export default {
data() {
return {
data: data,
toolbarOptions: ['PdfExport']
};
},
methods: {
toolbarClick(args) {
if (args.item.id === 'Grid_pdfexport') { // 'Grid_pdfexport' -> Grid component id + _ + toolbar item name
let pdfExportProperties = {
pageOrientation: 'Landscape',
};
this.$refs.grid.pdfExport(pdfExportProperties);
}
}
},
provide: {
grid: [Toolbar, PdfExport]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>
Change page size
Page size can be customized for the exported document using the pdfExportProperties
. Supported page sizes are:
- Letter
- Note
- Legal
- A0
- A1
- A2
- A3
- A5
- A6
- A7
- A8
- A9
- B0
- B1
- B2
- B3
- B4
- B5
- Archa
- Archb
- Archc
- Archd
- Arche
- Flsa
- HalfLetter
- Letter11x17
- Ledger
<template>
<div id="app">
<ejs-grid ref='grid' id='Grid' :dataSource='data' :toolbar='toolbarOptions' height='272px' :allowPaging='true' :allowPdfExport='true' :toolbarClick='toolbarClick'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' width=120></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=150></e-column>
<e-column field='ShipCity' headerText='Ship City' width=150></e-column>
<e-column field='ShipName' headerText='Ship Name' width=150></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import Vue from "vue";
import { GridPlugin, Toolbar, PdfExport } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
Vue.use(GridPlugin);
export default {
data() {
return {
data: data,
toolbarOptions: ['PdfExport']
};
},
methods: {
toolbarClick(args) {
if (args.item.id === 'Grid_pdfexport') { // 'Grid_pdfexport' -> Grid component id + _ + toolbar item name
let pdfExportProperties = {
pageSize: 'Letter'
};
this.$refs.grid.pdfExport(pdfExportProperties);
}
}
},
provide: {
grid: [Toolbar, PdfExport]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>
Define file name
You can assign the file name for the exported document by defining fileName property of PdfExportProperties in the toolbarClick
event.
<template>
<div id="app">
<ejs-grid ref='grid' id='Grid' :dataSource='data' :allowPaging='true' :allowPdfExport='true' :toolbar='toolbarOptions' :toolbarClick='toolbarClick' height='258px'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' type='number' width=120></e-column>
<e-column field='CustomerID' headerText='Customer ID' type='string' width=140>
</e-column>
<e-column field='Freight' headerText='Freight' textAlign='Right' format='C2' width=120>
</e-column>
<e-column field='OrderDate' headerText='Order Date' textAlign='Right' type='date' format='yMd' width=150></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import Vue from "vue";
import { GridPlugin, Toolbar, Page, PdfExport } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
Vue.use(GridPlugin);
export default {
data() {
return {
data: data,
toolbarOptions: ["PdfExport"],
pageSettings: { pageSize: 7 }
};
},
methods: {
toolbarClick(args) {
if (args["item"].id === "Grid_pdfexport") {
let exportProperties = {
fileName: "new.pdf"
};
this.$refs.grid.pdfExport(exportProperties);
}
}
},
provide: {
grid: [Toolbar, Page, PdfExport]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>
Font customization
Default fonts
By default, grid uses Helvetica font in the exported document. You can change the default font by using pdfExportProperties.theme
property. The available default fonts are,
- Helvetica
- TimesRoman
- Courier
- Symbol
- ZapfDingbats
The code example for changing default font,
import { PdfStandardFont, PdfFontFamily, PdfFontStyle } from '@syncfusion/ej2-pdf-export';
...
let pdfExportProperties = {
theme: {
header: {font: new PdfStandardFont(PdfFontFamily.TimesRoman, 11, PdfFontStyle.Bold)},
caption: { font: new PdfStandardFont(PdfFontFamily.TimesRoman, 9) },
record: { font: new PdfStandardFont(PdfFontFamily.TimesRoman, 10) }
}
};
Add custom font
You can change the default font of Grid header, content and caption cells in the exported document by using pdfExportProperties.theme
property.
In the following example, we have used Algeria font to export the grid.
<template>
<div id="app">
<ejs-grid ref='grid' id='Grid' :dataSource='data' :toolbar='toolbarOptions' height='272px' :allowPaging='true' :allowPdfExport='true' :toolbarClick='toolbarClick'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' width=120></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=150></e-column>
<e-column field='ShipCity' headerText='Ship City' width=150></e-column>
<e-column field='ShipName' headerText='Ship Name' width=150></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import Vue from "vue";
import { GridPlugin, Toolbar, PdfExport } from "@syncfusion/ej2-vue-grids";
import { data, adventProFont } from './datasource.js';
import { PdfTrueTypeFont } from '@syncfusion/ej2-pdf-export';
Vue.use(GridPlugin);
export default {
data() {
return {
data: data,
toolbarOptions: ['PdfExport']
};
},
methods: {
toolbarClick(args) {
if (args.item.id === 'Grid_pdfexport') { // 'Grid_pdfexport' -> Grid component id + _ + toolbar item name
let pdfExportProperties = {
theme: {
header: {font: new PdfTrueTypeFont(adventProFont, 12) },
caption: { font: new PdfTrueTypeFont(adventProFont, 10) },
record: { font: new PdfTrueTypeFont(adventProFont, 9) }
}
};
this.$refs.grid.pdfExport(pdfExportProperties);
}
}
},
provide: {
grid: [Toolbar, PdfExport]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>
PdfTrueTypeFont accepts base 64 format of the Custom Font.