fromAsset static method

Future<PDFDocument> fromAsset(
  1. String asset, {
  2. dynamic clearPreviewCache = true,
})

Load a PDF File from assets folder

Automatically clears the on-disk cache of previously rendered PDF previews unless clearPreviewCache is set to false. The option to disable it comes in handy when working with more than one document at the same time. If you do this, you are responsible for eventually clearing the cache by hand by calling PDFDocument.clearPreviewCache.

Implementation

static Future<PDFDocument> fromAsset(String asset,
    {clearPreviewCache = true}) async {
  // To open from assets, you can copy them to the app storage folder, and the access them "locally"
  File file;
  try {
    var dir = await getApplicationDocumentsDirectory();

    file = File("${dir.path}/file.pdf");
    var data = await rootBundle.load(asset);
    var bytes = data.buffer.asUint8List();
    await file.writeAsBytes(bytes, flush: true);
  } catch (e) {
    throw Exception('Error parsing asset file!');
  }
  PDFDocument document = PDFDocument();
  document._filePath = file.path;
  try {
    var pageCount = await _channel.invokeMethod('getNumberOfPages',
        {'filePath': file.path, 'clearCacheDir': clearPreviewCache});
    document.count = document.count = int.parse(pageCount);
  } catch (e) {
    throw Exception('Error reading PDF!');
  }
  return document;
}