openPDFAsset method

Future<bool> openPDFAsset(
  1. String assetPath,
  2. String title, {
  3. int initialPage = 1,
  4. List<PDFQuote> existingQuotes = const [],
})

Opens a PDF asset bundled with the application

assetPath The path to the asset in the Flutter assets bundle title The title to display in the viewer initialPage Optional parameter to specify which page to open first (defaults to 1) existingQuotes Optional list of quotes to highlight when opening the PDF

Returns a Future that completes with true if the PDF was opened successfully or throws an exception if there was an error.

Implementation

Future<bool> openPDFAsset(
  String assetPath,
  String title, {
  int initialPage = 1,
  List<PDFQuote> existingQuotes = const [],
}) async {
  try {
    // Get temporary directory
    final directory = await getTemporaryDirectory();

    // Create a unique filename from the asset path
    final filename = assetPath.split('/').last;
    final filePath = '${directory.path}/$filename';

    // Create file
    final file = File(filePath);

    // Copy asset to temporary directory
    final ByteData data = await rootBundle.load(assetPath);
    final List<int> bytes = data.buffer.asUint8List();
    await file.writeAsBytes(bytes);

    return await openPDFWithOptions(
      filePath,
      title,
      initialPage: initialPage,
      existingQuotes: existingQuotes,
    );
  } catch (e) {
    throw Exception('Error opening PDF asset: $e');
  }
}