Export FlexGrid to PdfDocument Instance

This sample shows how to export FlexGrid to an existing PdfDocument using draw and drawToPosition methods with different settings. Apart from export method, FlexGridPdfConverter also provides the draw and drawToPosition class methods which are used to export FlexGrid to an existing PdfDocument instance.

import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; import * as wijmo from '@grapecity/wijmo'; import * as grid from '@grapecity/wijmo.grid'; import * as pdf from '@grapecity/wijmo.pdf'; import * as gridPdf from '@grapecity/wijmo.grid.pdf'; import { getData } from './data'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { let flexGrid = new grid.FlexGrid('#flexGrid', { autoGenerateColumns: false, selectionMode: grid.SelectionMode.ListBox, headersVisibility: grid.HeadersVisibility.All, columns: [ { header: 'ID', binding: 'id' }, { header: 'Start Date', binding: 'start', format: 'd' }, { header: 'End Date', binding: 'end', format: 'd' }, { header: 'Country', binding: 'country' } ], itemsSource: getData(10) }); // document.querySelector('#btnExport').addEventListener('click', () => { let doc = new pdf.PdfDocument({ header: { declarative: { text: '\t&[Page]\\&[Pages]' } }, footer: { declarative: { text: '\t&[Page]\\&[Pages]' } }, ended: (sender, args) => pdf.saveBlob(args.blob, 'FlexGrid.pdf') }); // let settings = { styles: { cellStyle: { backgroundColor: '#ffffff', borderColor: '#c6c6c6' }, altCellStyle: { backgroundColor: '#f9f9f9' }, groupCellStyle: { backgroundColor: '#dddddd' }, headerCellStyle: { backgroundColor: '#eaeaea' } } }; // doc.drawText('This grid is scaled to fit the width of 200 and drawn using the draw method.', null, null, { width: 200 }); doc.moveDown(); gridPdf.FlexGridPdfConverter.draw(flexGrid, doc, 200, null, settings); // doc.drawText('This grid is drawn in its original size using the drawToPosition method.', 220, 0); doc.moveDown(); gridPdf.FlexGridPdfConverter.drawToPosition(flexGrid, doc, new wijmo.Point(220, doc.y), null, null, settings); // doc.drawText('This grid is drawn in its original size using the draw method and is split into multiple pages.', 0, 400); doc.moveDown(); gridPdf.FlexGridPdfConverter.draw(flexGrid, doc, null, null, settings); // doc.end(); }); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Export FlexGrid to the PdfDocument instance</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app'); </script> </head> <body> <div class="container-fluid"> <!-- Export button --> <button class="btn btn-default" id="btnExport">Export</button> <!-- FlexGrid --> <div id="flexGrid" class="grid"></div> </div> </body> </html> export function getData(count) { // data used to generate random items const countries = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece']; // let data = []; let dt = new Date(); // // add count items for (let i = 0; i < count; i++) { // constants used to create data items let date = new Date(dt.getFullYear(), i % 12, 25, i % 24, i % 60, i % 60), countryId = Math.floor(Math.random() * countries.length); // // create the item let item = { id: i, start: date, end: date, country: countries[countryId] }; // // add the item to the list data.push(item); } // return data; } body { margin-bottom: 24px; } .grid { margin-top: 20px; height: 300px; } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; // import * as wijmo from '@grapecity/wijmo'; import * as grid from '@grapecity/wijmo.grid'; import * as pdf from '@grapecity/wijmo.pdf'; import * as gridPdf from '@grapecity/wijmo.grid.pdf'; // import { Component, Inject, enableProdMode, NgModule, ViewChild } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { BrowserModule } from '@angular/platform-browser'; import { DataService } from './app.data'; import { WjGridModule } from '@grapecity/wijmo.angular2.grid'; // @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { data: any[]; @ViewChild('flexGrid') flexGrid: grid.FlexGrid; // constructor(@Inject(DataService) private dataService: DataService) { this.data = dataService.getData(10); } // export() { let doc = new pdf.PdfDocument({ header: { declarative: { text: '\t&[Page]\\&[Pages]' } }, footer: { declarative: { text: '\t&[Page]\\&[Pages]' } }, ended: (sender: pdf.PdfDocument, args: pdf.PdfDocumentEndedEventArgs) => pdf.saveBlob(args.blob, 'FlexGrid.pdf') }); // let settings = { styles: { cellStyle: { backgroundColor: '#ffffff', borderColor: '#c6c6c6' }, altCellStyle: { backgroundColor: '#f9f9f9' }, groupCellStyle: { backgroundColor: '#dddddd' }, headerCellStyle: { backgroundColor: '#eaeaea' } } }; // doc.drawText('This grid is scaled to fit the width of 200 and drawn using the draw method.', null, null, { width: 200 }); doc.moveDown(); gridPdf.FlexGridPdfConverter.draw(this.flexGrid, doc, 200, null, settings); // doc.drawText('This grid is drawn in its original size using the drawToPosition method.', 220, 0); doc.moveDown(); gridPdf.FlexGridPdfConverter.drawToPosition(this.flexGrid, doc, new wijmo.Point(220, doc.y), null, null, settings); // doc.drawText('This grid is drawn in its original size using the draw method and is split into multiple pages.', 0, 400); doc.moveDown(); gridPdf.FlexGridPdfConverter.draw(this.flexGrid, doc, null, null, settings); // doc.end(); } } // @NgModule({ imports: [WjGridModule, BrowserModule], declarations: [AppComponent], providers: [DataService], bootstrap: [AppComponent] }) export class AppModule { } // enableProdMode(); // Bootstrap application with hash style navigation and global services. platformBrowserDynamic().bootstrapModule(AppModule); <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Export FlexGrid to the PdfDocument instance</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Polyfills --> <script src="node_modules/core-js/client/shim.min.js"></script> <script src="node_modules/zone.js/dist/zone.min.js"></script> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.js"></script> <script src="systemjs.config.js"></script> <script> // workaround to load 'rxjs/operators' from the rxjs bundle System.import('rxjs').then(function (m) { System.set(SystemJS.resolveSync('rxjs/operators'), System.newModule(m.operators)); System.import('./src/app.component'); }); </script> </head> <body> <app-component></app-component> </body> </html> <div class="container-fluid"> <!-- Export button --> <button class="btn btn-default" (click)="export()">Export</button> <!-- FlexGrid --> <wj-flex-grid class="grid" #flexGrid [autoGenerateColumns]="false" selectionMode="ListBox" headersVisibility="All" [itemsSource]="data"> <wj-flex-grid-column header="ID" binding="id"></wj-flex-grid-column> <wj-flex-grid-column header="Start Date" binding="start" format="d"></wj-flex-grid-column> <wj-flex-grid-column header="End Date" binding="end" format="d"></wj-flex-grid-column> <wj-flex-grid-column header="Country" binding="country"></wj-flex-grid-column> </wj-flex-grid> </div> import { Injectable } from '@angular/core'; // @Injectable() export class DataService { // getData(count: number) { // data used to generate random items const countries = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece']; // let data = []; let dt = new Date(); // // add count items for (let i = 0; i < count; i++) { // constants used to create data items let date = new Date(dt.getFullYear(), i % 12, 25, i % 24, i % 60, i % 60), countryId = Math.floor(Math.random() * countries.length); // // create the item let item = { id: i, start: date, end: date, country: countries[countryId] }; // // add the item to the list data.push(item); } // return data; } } body { margin-bottom: 24px; } .grid { margin-top: 20px; height: 300px; } <template> <div class="container-fluid"> <!-- Export button --> <button class="btn btn-default" @click="exportPDF">Export</button> <!-- FlexGrid --> <wj-flex-grid class="grid" :autoGenerateColumns=false selectionMode="ListBox" headersVisibility="All" :itemsSource="data" :initialized="initializeGrid"> <wj-flex-grid-column header="ID" binding="id"></wj-flex-grid-column> <wj-flex-grid-column header="Start Date" binding="start" format="d"></wj-flex-grid-column> <wj-flex-grid-column header="End Date" binding="end" format="d"></wj-flex-grid-column> <wj-flex-grid-column header="Country" binding="country"></wj-flex-grid-column> </wj-flex-grid> </div> </template> <script> import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import Vue from 'vue'; import { getData } from './data'; import { WjGridModule } from '@grapecity/wijmo.vue2.grid'; import * as wijmo from '@grapecity/wijmo'; import * as pdf from '@grapecity/wijmo.pdf'; import * as gridPdf from '@grapecity/wijmo.grid.pdf'; new Vue({ el: '#app', data: { data: getData(10) }, methods: { initializeGrid(ctl) { this.flexGrid = ctl; }, exportPDF() { let doc = new pdf.PdfDocument({ header: { declarative: { text: '\t&[Page]\\&[Pages]' } }, footer: { declarative: { text: '\t&[Page]\\&[Pages]' } }, ended: (sender, args) => pdf.saveBlob(args.blob, 'FlexGrid.pdf') }); // let settings = { styles: { cellStyle: { backgroundColor: '#ffffff', borderColor: '#c6c6c6' }, altCellStyle: { backgroundColor: '#f9f9f9' }, groupCellStyle: { backgroundColor: '#dddddd' }, headerCellStyle: { backgroundColor: '#eaeaea' } } }; // doc.drawText('This grid is scaled to fit the width of 200 and drawn using the draw method.', null, null, { width: 200 }); doc.moveDown(); gridPdf.FlexGridPdfConverter.draw(this.flexGrid, doc, 200, null, settings); // doc.drawText('This grid is drawn in its original size using the drawToPosition method.', 220, 0); doc.moveDown(); gridPdf.FlexGridPdfConverter.drawToPosition(this.flexGrid, doc, new wijmo.Point(220, doc.y), null, null, settings); // doc.drawText('This grid is drawn in its original size using the draw method and is split into multiple pages.', 0, 400); doc.moveDown(); gridPdf.FlexGridPdfConverter.draw(this.flexGrid, doc, null, null, settings); // doc.end(); } } }); </script> <style> body { margin-bottom: 24px; } .grid { margin-top: 20px; height: 300px; } </style> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Export FlexGrid to the PdfDocument instance</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app.vue'); </script> </head> <body> <div id="app"> </div> </body> </html> export function getData(count) { // data used to generate random items const countries = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece']; const products = ['Widget', 'Gadget', 'Doohickey']; const colors = ['Orange', 'White', 'Red', 'Green', 'Blue']; // let data = []; let dt = new Date(); // // add count items for (let i = 0; i < count; i++) { // constants used to create data items let date = new Date(dt.getFullYear(), i % 12, 25, i % 24, i % 60, i % 60), countryId = Math.floor(Math.random() * countries.length), productId = Math.floor(Math.random() * products.length), colorId = Math.floor(Math.random() * colors.length); // // create the item let item = { id: i, start: date, end: date, country: countries[countryId], product: products[productId], color: colors[colorId], amount: Math.random() * 10000 - 5000, amount2: Math.random() * 10000 - 5000, discount: Math.random() / 4, active: i % 4 == 0 }; // // add the item to the list data.push(item); } return data; } import "@grapecity/wijmo.styles/wijmo.css"; import "bootstrap.css"; import "./app.css"; // import * as React from 'react'; import * as ReactDOM from 'react-dom'; // import * as wjcCore from "@grapecity/wijmo"; import * as wjcGrid from "@grapecity/wijmo.react.grid"; import * as gridPdf from "@grapecity/wijmo.grid.pdf"; import * as pdf from '@grapecity/wijmo.pdf'; import { getData } from "./data"; class App extends React.Component { constructor(props) { super(props); this.state = { data: getData(10) }; } render() { return <div className="container-fluid"> <button className="btn btn-default" onClick={this.exportPDF.bind(this)}>Export</button> <wjcGrid.FlexGrid className="grid" autoGenerateColumns={false} headersVisibility="Column" selectionMode="ListBox" itemsSource={this.state.data} initialized={this.initializeGrid.bind(this)}> <wjcGrid.FlexGridColumn header="ID" binding="id"></wjcGrid.FlexGridColumn> <wjcGrid.FlexGridColumn header="Start Date" binding="hotel" format="d"></wjcGrid.FlexGridColumn> <wjcGrid.FlexGridColumn header="End Date" binding="end" format="d"></wjcGrid.FlexGridColumn> <wjcGrid.FlexGridColumn header="Country" binding="country"></wjcGrid.FlexGridColumn> </wjcGrid.FlexGrid> </div>; } initializeGrid(ctl) { this.flexGrid = ctl; } exportPDF() { let doc = new pdf.PdfDocument({ header: { declarative: { text: '\t&[Page]\\&[Pages]' } }, footer: { declarative: { text: '\t&[Page]\\&[Pages]' } }, ended: (sender, args) => pdf.saveBlob(args.blob, 'FlexGrid.pdf') }); // let settings = { styles: { cellStyle: { backgroundColor: '#ffffff', borderColor: '#c6c6c6' }, altCellStyle: { backgroundColor: '#f9f9f9' }, groupCellStyle: { backgroundColor: '#dddddd' }, headerCellStyle: { backgroundColor: '#eaeaea' } } }; // doc.drawText('This grid is scaled to fit the width of 200 and drawn using the draw method.', null, null, { width: 200 }); doc.moveDown(); gridPdf.FlexGridPdfConverter.draw(this.flexGrid, doc, 200, null, settings); // doc.drawText('This grid is drawn in its original size using the drawToPosition method.', 220, 0); doc.moveDown(); gridPdf.FlexGridPdfConverter.drawToPosition(this.flexGrid, doc, new wjcCore.Point(220, doc.y), null, null, settings); // doc.drawText('This grid is drawn in its original size using the draw method and is split into multiple pages.', 0, 400); doc.moveDown(); gridPdf.FlexGridPdfConverter.draw(this.flexGrid, doc, null, null, settings); // doc.end(); } } ReactDOM.render(<App />, document.getElementById('app')); <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Grapecity Wijmo OLAP Pivot Chart Overview</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app'); </script> </head> <body> <div id="app"></div> </body> </html> body { margin-bottom: 24px; } .grid { margin-top: 20px; height: 300px; } export function getData(count) { // data used to generate random items const countries = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece']; const products = ['Widget', 'Gadget', 'Doohickey']; const colors = ['Orange', 'White', 'Red', 'Green', 'Blue']; // let data = []; let dt = new Date(); // // add count items for (let i = 0; i < count; i++) { // constants used to create data items let date = new Date(dt.getFullYear(), i % 12, 25, i % 24, i % 60, i % 60), countryId = Math.floor(Math.random() * countries.length), productId = Math.floor(Math.random() * products.length), colorId = Math.floor(Math.random() * colors.length); // // create the item let item = { id: i, start: date, end: date, country: countries[countryId], product: products[productId], color: colors[colorId], amount: Math.random() * 10000 - 5000, amount2: Math.random() * 10000 - 5000, discount: Math.random() / 4, active: i % 4 == 0 }; // // add the item to the list data.push(item); } return data; }