How to export various locales and emojis to a PDF document in Flutter DataTable (SfDataGrid)?
In this article, you will learn how to properly display complex script languages such as Arabic, Hindi, Hebrew, Tamil, and more. This can be achieved by utilizing true type fonts that support the required characters, without relying on open type features like Arial Unicode MS.
STEP 1: Initialize the SfDataGrid widget with all the required properties. Create a global key and assign it to the SfDataGrid.key property. The key is used to retrieve the current state object of the SfDataGrid widget.
final GlobalKey<SfDataGridState> _key = GlobalKey<SfDataGridState>();
> Note: Using TrueType fonts like Segoe UI Emoji, which allow both font and emoji characters, makes it possible to render emojis.
STEP 2: We can use PdfTrueTypeFont API to provide the necessary font file. To do this, use the exportToPdfGrid method instead of exportToPdfDocument when exporting the DataGrid. In the exportToPdfGrid method, set the PdfTrueTypeFont to the PdfGridStyle.In the cellExport callback, for RTL scripts like Arabic, Hebrew, Persian, and Urdu, you need to specify the PdfStringFormat textDirection as PdfTextDirection.rightToLeft.
bool isMobilePlatform = false;
@override
Widget build(BuildContext context) {
final ThemeData themeData = Theme.of(context);
isMobilePlatform = themeData.platform == TargetPlatform.android;
return Scaffold(
appBar: AppBar(
title: const Text('SfDatagrid Demo'),
),
body: LayoutBuilder(builder: (context, constraints) {
return Column(children: [
Container(
margin: const EdgeInsets.symmetric(vertical: 15),
height: 50,
width: 250,
child: ElevatedButton(
onPressed: () async {
PdfDocument document = PdfDocument();
PdfPage pdfPage = document.pages.add();
PdfGrid pdfGrid = _key.currentState!.exportToPdfGrid(
// Set the text direction as PdfTextDirection.rightToLeft for the cell only for RTL languages.
cellExport: (details) {
details.pdfCell.stringFormat = PdfStringFormat(
textDirection: PdfTextDirection.rightToLeft,
alignment: PdfTextAlignment.right);
},
);
PdfTrueTypeFont pdfTrueTypeFont;
// Use 'seguiemj.ttf' instead 'ARIALUNI.ttf' to export both font and emoji characters.
const String webFileLocation = 'fonts/ARIALUNI.TTF';
const String androidFileLocation =
'assets/fonts/ARIALUNI.TTF';
if (kIsWeb || isMobilePlatform) {
ByteData byte = await rootBundle.load(isMobilePlatform
? androidFileLocation
: webFileLocation);
pdfTrueTypeFont =
PdfTrueTypeFont(byte.buffer.asUint8List(), 14);
} else {
pdfTrueTypeFont = PdfTrueTypeFont(
File('assets/fonts/ARIALUNI.TTF').readAsBytesSync(),
14);
}
PdfGridStyle gridStyle = PdfGridStyle(
font: pdfTrueTypeFont,
);
pdfGrid.rows.applyStyle(gridStyle);
pdfGrid.draw(
page: pdfPage,
bounds: const Rect.fromLTWH(0, 0, 0, 0),
);
final List<int> bytes = document.saveSync();
helper.saveAndLaunchFile(bytes, 'datagrid_pdf.pdf');
},
child: const Text('Export DataGrid to PDF')),
),
Expanded(
child: SfDataGrid(
source: _employeeDataSource,
key: _key,
columnWidthMode: ColumnWidthMode.fill,
columns: getColumns),
)
]);
}));
}