isPdfProtected method
Implementation
@override
Future<bool> isPdfProtected({
required String filePath, // filePath is less relevant on web
html.File? webFile, // This should be passed from file_picker on web
}) async {
if (webFile == null) {
throw PlatformException(
code: "MISSING_FILE_WEB",
message: "Web file data not provided for isPdfProtected.",
);
}
print('[Web] isPdfProtected called for ${webFile.name}');
final completer = Completer<bool>();
final reader = html.FileReader();
reader.readAsArrayBuffer(webFile);
reader.onLoadEnd.listen((event) async {
if (reader.readyState == html.FileReader.DONE) {
final Uint8List fileBytes = reader.result as Uint8List;
print(
'[Web] isPdfProtected: File read, ${fileBytes.lengthInBytes} bytes.',
);
try {
// Call a JavaScript function that uses PDF.js to check protection
final jsPromise = js_util.callMethod(
html.window,
'_webIsPdfProtected',
[fileBytes],
);
final bool isProtected = await js_util.promiseToFuture(
jsPromise as Object,
);
print('[Web] isPdfProtected: Result from JS: $isProtected');
completer.complete(isProtected);
} catch (e) {
print('[Web] isPdfProtected: Error during JS call: ${e.toString()}');
completer.completeError(
PlatformException(
code: 'WEB_JS_CALL_ERROR_CHECK',
message:
'Error calling PDF.js interop for isPdfProtected: ${e.toString()}',
),
);
}
} else {
print('[Web] isPdfProtected: FileReader not DONE.');
completer.completeError(
PlatformException(
code: 'WEB_FILE_READ_INCOMPLETE_CHECK',
message: 'Web file reading was not completed for isPdfProtected.',
),
);
}
});
reader.onError.listen((event) {
print('[Web] isPdfProtected: FileReader error.');
completer.completeError(
PlatformException(
code: 'WEB_FILE_READ_ERROR_CHECK',
message: 'Error reading web file for isPdfProtected.',
),
);
});
return completer.future;
}