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;
  const double padding = 5.0;
  document.pageSettings.size = Size(
    bitmap.width.toDouble() + (padding * 2),
    bitmap.height.toDouble() + (padding * 2),
  );

  final PdfPage page = document.pages.add();

  page.graphics.drawImage(
      bitmap,
      Rect.fromLTWH(
          padding, padding, bitmap.width.toDouble(), bitmap.height.toDouble()));
  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);
}