fromAsset static method

Future<PDFDocument> fromAsset(
  1. String asset
)

Load a PDF File from assets folder String asset path of the asset to be loaded

Implementation

static Future<PDFDocument> fromAsset(String asset) async {

  if (kIsWeb) {
    return fromAssetWeb(asset);
  } else {
    File file;
    try {
      final dir = await getApplicationDocumentsDirectory();
      file = File("${dir.path}/${DateTime
          .now()
          .millisecondsSinceEpoch}.pdf");
      final data = await rootBundle.load(asset);
      final bytes = data.buffer.asUint8List();
      await file.writeAsBytes(bytes, flush: true);
    } catch (e) {
      throw Exception('Error parsing asset file!');
    }
    final document = PDFDocument();
    document.filePathPublic = file.path;

    try {
      final pageCount = await _channel
          .invokeMethod('getNumberOfPages', {'filePath': file.path});
      document.count = document.count = int.parse(pageCount as String);
    } catch (e) {
      throw Exception('Error reading PDF!');
    }
    return document;
  }
}