getPdfium function

PDFium getPdfium({
  1. String? modulePath,
})

Helper function to get PDFium instance.

PDFium is downloaded and bundled at build time by hook/build.dart. Use modulePath only for custom deployments or tests.

This function supports both Flutter and pure Dart environments, and it tries multiple strategies to locate the PDFium library:

  • If modulePath is provided, it attempts to load the library from that path directly.
  • On iOS or macOS with Flutter, it assumes PDFium is already loaded in the process and uses DynamicLibrary.process.
  • For Linux with Flutter, it looks for the library from the [EXECUTABLE_DIR]/../lib/libpdfium.so path.
  • For other platforms, it tries to load the library from system paths using the standard file name.
  • If all above strategies fail, it falls back to loading the library from the bundled native asset; dart test, dart run, and dart compile ensure the asset is available through .dart_tool/native_assets.yaml.

Implementation

pdfium_bindings.PDFium getPdfium({String? modulePath}) {
  try {
    return pdfium_bindings.PDFium(_getModule(modulePath: modulePath));
  } catch (e) {
    throw Exception('Failed to load PDFium module: $e');
  }
}