hcSavePdfDocuments method

dynamic hcSavePdfDocuments({
  1. required String name,
  2. required Uint8List fileBytes,
  3. String customDirectoryName = "Documents",
  4. BuildContext? context,
})

Save PDF Documents

Implementation

hcSavePdfDocuments(
    {required String name,
    required Uint8List fileBytes,
    String customDirectoryName = "Documents",
    BuildContext? context}) async {
  Directory appDocDirectory = await getApplicationDocumentsDirectory();
  String path = Platform.isAndroid
      ? hcDirPath
      : "${appDocDirectory.path}/$customDirectoryName";
  try {
    bool checkPermission = await Permission.accessMediaLocation.isGranted;
    if (checkPermission) {
      File pdfDoc = File(
          "$path/${DateFormat('yy-HH-mm-ss').format(DateTime.now())}-$name");
      await pdfDoc.writeAsBytes(fileBytes);
      ScaffoldMessenger.of(context!).showSnackBar(SnackBar(
        content: Text(
            "File saved successfully to $path/${DateFormat('yy-HH-mm-ss').format(DateTime.now())}-$name"
            "File saved successfully to $path/${DateFormat('yy-HH-mm-ss').format(DateTime.now())}-$name"),
      ));
    } else {
      ScaffoldMessenger.of(context!).showSnackBar(const SnackBar(
        content: Text("Storage permission denied !, please try again!"),
      ));
      var status = await Permission.accessMediaLocation.status;
      if (!status.isGranted) {
        await Permission.accessMediaLocation.request();
      }
    }
  } on FileSystemException catch (e) {
    ScaffoldMessenger.of(context!).showSnackBar(SnackBar(
      content: Text("ERROR: ${e.message} $path/$name"),
    ));
  } catch (e) {
    ScaffoldMessenger.of(context!).showSnackBar(SnackBar(
      content: Text("ERROR: $e"),
    ));
  }
}