exportToPdfGrid method

PdfGrid exportToPdfGrid(
  1. {List<DataGridRow>? rows,
  2. bool exportStackedHeaders = true,
  3. bool canRepeatHeaders = true,
  4. bool fitAllColumnsInOnePage = false,
  5. bool autoColumnWidth = true,
  6. bool exportTableSummaries = true,
  7. List<String> excludeColumns = const <String>[],
  8. DataGridToPdfConverter? converter,
  9. DataGridCellPdfExportCallback? cellExport}
)

Exports the SfDataGrid to PdfGrid.

If the rows is set, the given list of DataGridRow collections is exported. Typically, you can set this property to export the selected rows from SfDataGrid.

Use the cellExport argument as the callback, and it will be called for each cell. You can customize the cell in Pdf document.

Use fitAllColumnsInOnePage argument to fit all the columns in a page.

Use the converter argument to set the customized Pdf converter. To customize the default Pdf converter, override the DataGridToPdfConverter class and override the necessary methods.

The following example shows how to export the SfDataGrid to PdfGrid.

final GlobalKey<SfDataGridState> _key = GlobalKey<SfDataGridState>();

Future<void> exportDataGridToPdf() async {
  final PdfDocument pdfDocument = PdfDocument();
  final PdfGrid pdfGrid =
      _key.currentState!.exportToPdfGrid(fitAllColumnsInOnePage: true);
  pdfGrid.draw(
      page: pdfDocument.pages.add(), bounds: const Rect.fromLTWH(0, 0, 0, 0));
  final List<int> bytes = pdfDocument.save();
  File('DataGrid.pdf').writeAsBytes(bytes);
  pdfDocument.dispose();
}

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: const Text(
        'Syncfusion Flutter DataGrid Export',
        overflow: TextOverflow.ellipsis,
      ),
    ),
    body: Column(
      children: <Widget>[
        Container(
          margin: const EdgeInsets.all(12.0),
          child: const SizedBox(
            height: 40.0,
            width: 150.0,
            child: MaterialButton(
                color: Colors.blue,
                child: Center(
                    child: Text(
                  'Export to PDF',
                  style: TextStyle(color: Colors.white),
                )),
                onPressed: exportDataGridToPdf),
          ),
        ),
        Expanded(
          child: SfDataGrid(
            key: _key,
            source: employeeDataSource,
            columns: <GridColumn>[
              GridColumn(
                  columnName: 'ID',
                  label: Container(
                      padding: const EdgeInsets.all(16.0),
                      alignment: Alignment.center,
                      child: const Text(
                        'ID',
                      ))),
              GridColumn(
                  columnName: 'Name',
                  label: Container(
                      padding: const EdgeInsets.all(8.0),
                      alignment: Alignment.center,
                      child: const Text('Name'))),
              GridColumn(
                  columnName: 'Designation',
                  label: Container(
                      padding: const EdgeInsets.all(8.0),
                      alignment: Alignment.center,
                      child: const Text(
                        'Designation',
                        overflow: TextOverflow.ellipsis,
                      ))),
              GridColumn(
                  columnName: 'Salary',
                  label: Container(
                      padding: const EdgeInsets.all(8.0),
                      alignment: Alignment.center,
                      child: const Text('Salary'))),
            ],
          ),
        ),
      ],
    ),
  );
}

Implementation

PdfGrid exportToPdfGrid(
    {List<DataGridRow>? rows,
    bool exportStackedHeaders = true,
    bool canRepeatHeaders = true,
    bool fitAllColumnsInOnePage = false,
    bool autoColumnWidth = true,
    bool exportTableSummaries = true,
    List<String> excludeColumns = const <String>[],
    DataGridToPdfConverter? converter,
    DataGridCellPdfExportCallback? cellExport}) {
  converter ??= DataGridToPdfConverter();
  _initializeProperties(converter,
      autoColumnWidth: autoColumnWidth,
      fitAllColumnsInOnePage: fitAllColumnsInOnePage,
      canRepeatHeaders: canRepeatHeaders,
      cellExport: cellExport,
      exportStackedHeaders: exportStackedHeaders,
      excludeColumns: excludeColumns,
      exportTableSummaries: exportTableSummaries);

  return converter.exportToPdfGrid(widget, rows);
}