Demo - Export to PDF

by Joel Parks

HTML

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.grapecity.com/wijmo/5.latest/styles/wijmo.min.css">
<script src="https://cdn.grapecity.com/wijmo/5.latest/controls/wijmo.min.js"></script>
<script src="https://cdn.grapecity.com/wijmo/5.latest/controls/wijmo.grid.min.js"></script>
<script src="https://cdn.grapecity.com/wijmo/5.latest/controls/wijmo.input.min.js"></script>
<script src="https://cdn.grapecity.com/wijmo/5.latest/controls/wijmo.grid.filter.min.js"></script>
<script src="https://cdn.grapecity.com/wijmo/5.latest/controls/wijmo.pdf.min.js"></script>
<script src="https://cdn.grapecity.com/wijmo/5.latest/controls/wijmo.grid.pdf.min.js"></script>
<div class="container">

  <h1>
    Export to PDF
  </h1>
  <p>
    To export FlexGrid controls to PDF, you should include
    two extra modules in your application:</p>
  <ol>
    <li>
      <b>wijmo.pdf.js</b>:
      Provides general methods for saving and loading PDF files.</li>
    <li>
      <b>wijmo.grid.pdf.js</b>:
      Contains the <b>FlexGridPdfConverter</b> class that uses
      <b>wijmo.pdf.js</b> to save FlexGrid controls as PDF.</li>
  </ol>    
  <p>
    To export a FlexGrid to PDF, call the 
    <b>FlexGridPdfConverter.export</b> method and
    provide a reference to the grid that will be exported,
    the file name, and extra options to define the
    page format, headers and footers, and styles.</p>
    
  <button id="btnExport" class="btn btn-default">
    Export to PDF
  </button>
  <button id="btnCreateDoc" class="btn btn-default">
    Create PDF Document
  </button>
  
  <div id="theGrid"></div>
  
</div>

CSS

.wj-flexgrid {
  max-height: 250px;
  margin: 12px 0;
  direction: rtl;
}
body {
  margin-bottom: 20px;
}

JavaScript

onload = function() {

  // create some random data
  var countries = 'US,Germany,UK,Japan,Italy,Greece'.split(',');
  var data = [];
  for (var i = 0; i < 200; i++) {
    data.push({
    	id: i,
      country: countries[i % countries.length],
      sales: Math.random() * 10000,
      expenses: Math.random() * 5000
    });
  }

  // show the data in a grid
  var theGrid = new wijmo.grid.FlexGrid('#theGrid', {
  	autoGenerateColumns: false,
    columns: [
    	{ binding: 'id', header: 'ID', width: 60 },
    	{ binding: 'country', header: 'Country', width: '2*'},
    	{ binding: 'sales', header: 'Sales', width: '*', format: 'n2'},
    	{ binding: 'expenses', header: 'Expenses', width: '*', format: 'n2'}
    ],
    itemsSource: data,
  });
  
  // add a filter to show that it works while exporting
  var f = new wijmo.grid.filter.FlexGridFilter(theGrid);
  
	// export grid to PDF
  document.getElementById('btnExport').addEventListener('click', function() {
		wijmo.grid.pdf.FlexGridPdfConverter.export(theGrid, 'LearnWijmo.pdf', {
    	maxPages: 10,
      scaleMode: wijmo.grid.pdf.ScaleMode.PageWidth,
      documentOptions: {
      	compress: true,
        header: { declarative: { text: '\t&[Page] of &[Pages]' } },
        footer: { declarative: { text: '\t&[Page] of &[Pages]' } },
        info: { author: 'C1', title: 'Learn Wijmo' }
			},
    	styles: {
				cellStyle: { backgroundColor: 'blue', borderColor: '#c6c6c6' },
	    	altCellStyle: { backgroundColor: '#f9f9f9' },
	    	groupCellStyle: { backgroundColor: '#dddddd' },
    		headerCellStyle: { backgroundColor: '#eaeaea' }
			}
		});
	});

	// create a PDF document with the grid and some other content
  document.getElementById('btnCreateDoc').addEventListener('click', function() {

		// create the document
		var doc = new wijmo.pdf.PdfDocument({
    	compress: true, 
      header: { declarative: { text: '\t&[Page]\\&[Pages]' } },
      ended: function (sender, args) {
				wijmo.pdf.saveBlob(args.blob,...