renderPdf function

Future<void> renderPdf({
  1. Function? onDownloadPdfPath,
  2. GlobalKey<SfCartesianChartState>? globalKey,
  3. String? fileName,
})

Generate PDF file

Implementation

Future<void> renderPdf(
    {Function? onDownloadPdfPath,
    GlobalKey<SfCartesianChartState>? globalKey,
    String? fileName}) async {
  final PdfDocument document = PdfDocument();
  final PdfBitmap bitmap = PdfBitmap(await readImageData(globalKey));

  document.pageSettings.orientation = PdfPageOrientation.portrait;
  document.pageSettings.margins.all = 0;
  document.pageSettings.size =
      Size(bitmap.width.toDouble(), bitmap.height.toDouble());
  final PdfPage page = document.pages.add();
  final Size pageSize = page.getClientSize();
  page.graphics
      .drawImage(bitmap, Rect.fromLTWH(0, 0, pageSize.width, pageSize.height));
  final List<int> bytes = document.saveSync();
  Directory documentsDirectory = await getApplicationDocumentsDirectory();
  final String path = documentsDirectory.path;
  DateTime currentDatetime = DateTime.now();
  String pdfName =
      '${fileName!}_${CalenderDateUtils.fullDateTime(currentDatetime).toString()}.pdf';
  //printLogs("pdfName $pdfName");
  final File file = File('$path/$pdfName');
  file.writeAsBytesSync(bytes, flush: true);
  document.dispose();
  //printLogs("file path ${file.absolute.path}");
  onDownloadPdfPath!.call(file);
}