file_util.dart 1.0 KB

123456789101112131415161718192021222324252627
  1. // Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
  2. //
  3. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  4. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  5. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  6. // This notice may not be removed from this file.
  7. import 'dart:io';
  8. import 'package:compdfkit_flutter/compdfkit.dart';
  9. import 'package:flutter/material.dart';
  10. Future<File> extractAsset(BuildContext context, String assetPath,
  11. {bool shouldOverwrite = true, String prefix = ''}) async {
  12. final bytes = await DefaultAssetBundle.of(context).load(assetPath);
  13. final list = bytes.buffer.asUint8List();
  14. final tempDir = await ComPDFKit.getTemporaryDirectory();
  15. final tempDocumentPath = '${tempDir.path}/$prefix$assetPath';
  16. final file = File(tempDocumentPath);
  17. if (shouldOverwrite || !file.existsSync()) {
  18. await file.create(recursive: true);
  19. file.writeAsBytesSync(list);
  20. }
  21. return file;
  22. }