# grid-pdf-export-template

> Data grid with a PDF export toolbar action and a query cell callback so templated columns export as plain text.

**Framework:** react  **Component:** Grid  **Variant:** pdf-export-template

## Get this item

**If you are an agent, fetch the JSON.** Source is inlined, so one request is enough and no tooling is required:

```
GET https://ai.syncfusion.com/r/react/ts/grid-pdf-export-template.json
```

**Install Package(s)**

```bash
npm install @syncfusion/ej2-react-grids
```

**Notes**

- Syncfusion release: 2026 Volume 3 (v35.1.37)
- The Syncfusion package is licensed. The composition in this file is source you own and edit. See https://ai.syncfusion.com/licensing.md

## Source files

### src/components/grid-pdf-export-template/Grid.tsx

```tsx
import { useRef } from 'react';
import { GridComponent, ColumnsDirective, ColumnDirective, Inject, Page, Toolbar, PdfExport } from '@syncfusion/ej2-react-grids';
import { orderDetails } from '../datasource/grid-data';

const palette: string[] = ['#6366f1', '#0ea5e9', '#10b981', '#f59e0b', '#ec4899', '#8b5cf6'];
const colorFor = (text: string): string => palette[[...text].reduce((sum, ch) => sum + ch.charCodeAt(0), 0) % palette.length];
const initials = (name: string): string => name.trim().split(/\s+/).map((p) => p[0]).slice(0, 2).join('').toUpperCase();
const daysToShip = (o: any): number => Math.max(0, Math.round((new Date(o.ShippedDate).getTime() - new Date(o.OrderDate).getTime()) / 86400000));
const statusOf = (days: number) => (days <= 5 ? { label: 'Fast', color: '#059669', bg: '#d1fae5' }
  : days <= 10 ? { label: 'On time', color: '#0369a1', bg: '#e0f2fe' }
  : { label: 'Delayed', color: '#b45309', bg: '#fef3c7' });

const avatarStyle: React.CSSProperties = { width: 36, height: 36, borderRadius: 12, color: '#fff', display: 'inline-flex', alignItems: 'center', justifyContent: 'center', fontWeight: 600, fontSize: 13 };
const chipStyle: React.CSSProperties = { borderRadius: 999, padding: '2px 10px', fontSize: 12, fontWeight: 600 };

function PdfExportTemplate() {
  const gridRef = useRef<GridComponent>(null);
  const toolbar: string[] = ['PdfExport'];

  const customerTemplate = (p: any) => {
    const name: string = p.CustomerName.trim();
    return (
      <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
        <span style={{ ...avatarStyle, background: colorFor(p.CustomerID) }}>{initials(name)}</span>
        <div style={{ display: 'flex', flexDirection: 'column', justifyContent: 'center' }}>
          <span style={{ display: 'block', fontWeight: 600, color: '#0f172a', lineHeight: '20px' }}>{name}</span>
          <span style={{ display: 'block', fontSize: 12, color: '#94a3b8', lineHeight: '16px' }}>{p.CustomerID}</span>
        </div>
      </div>
    );
  };
  const statusTemplate = (p: any) => {
    const days: number = daysToShip(p);
    const s = statusOf(days);
    return <span style={{ ...chipStyle, background: s.bg, color: s.color }}>{s.label} · {days}d</span>;
  };

  const toolbarClick = (args: any): void => {
    if (args.item.id === 'PdfExportTemplateGrid_pdfexport') {
      gridRef.current?.pdfExport({ fileName: 'Orders.pdf' });
    }
  };
  // Templates are HTML-only, so give the PDF plain text for each templated column.
  const pdfQueryCellInfo = (args: any): void => {
    const d = args.data;
    if (args.column.field === 'CustomerName') args.value = `${d.CustomerName.trim()} (${d.CustomerID})`;
    if (args.column.headerText === 'Status') {
      const days: number = daysToShip(d);
      args.value = `${statusOf(days).label} - ${days} days`;
    }
  };

  return (
    <div className='control-pane'>
      <div className='control-section'>
        <GridComponent id='PdfExportTemplateGrid' ref={gridRef} dataSource={orderDetails}
          height='440' rowHeight={60} allowPaging={true} pageSettings={{ pageSize: 8 }} gridLines='Horizontal'
          allowPdfExport={true} toolbar={toolbar} toolbarClick={toolbarClick} pdfQueryCellInfo={pdfQueryCellInfo}>
          <ColumnsDirective>
            <ColumnDirective field='OrderID' headerText='Order' width='110' isPrimaryKey={true} />
            <ColumnDirective field='CustomerName' headerText='Customer' width='230' template={customerTemplate} />
            <ColumnDirective field='ShipCity' headerText='City' width='150' />
            <ColumnDirective headerText='Status' width='150' template={statusTemplate} />
            <ColumnDirective field='Freight' headerText='Freight' width='120' format='C2' textAlign='Right' />
          </ColumnsDirective>
          <Inject services={[Page, Toolbar, PdfExport]} />
        </GridComponent>
      </div>
    </div>
  );
}
export default PdfExportTemplate;
```
