1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- /// cpdf_converter.dart
- ///
- /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
- ///
- /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
- /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
- /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
- /// This notice may not be removed from this file.
- import 'dart:collection';
- import 'package:flutter/services.dart';
- import 'package:kmpdfkit_conversion_flutter/models/task_result.dart';
- import 'models/task_status.dart';
- import 'models/convert_type.dart';
- import 'models/options/options.dart';
- import 'package:path/path.dart' as p;
- typedef TaskResultCallback = Function(TaskResult result);
- class CPDFConverter {
- static const EventChannel _channel =
- EventChannel('com.compdfkit.conversion.flutter.convert');
- static const MethodChannel _sdkGlobalChannel =
- MethodChannel('com.compdfkit.conversion.flutter.convert.sdk.info');
- static const _methodChannel =
- MethodChannel('com.compdfkit.conversion.flutter.convert.method');
- static Future<String> init(String key, String secret) async {
- String request = await _sdkGlobalChannel
- .invokeMethod('conversion_init', {"key": key, "secret": secret});
- return request;
- }
- ///get conversion sdk version info
- static Future<String> getSDKBuildTag() async {
- var sdkBuildTag = await _sdkGlobalChannel.invokeMethod('sdk_build_tag');
- return sdkBuildTag;
- }
- static Future<String> getSDKVersion() async {
- var sdkVersion = await _sdkGlobalChannel.invokeMethod('sdk_version');
- return sdkVersion;
- }
- static Future<String> getOutputPath() async {
- var outPutPath =
- await _sdkGlobalChannel.invokeMethod('request_convert_output_path');
- return outPutPath;
- }
- /// Submit the conversion task
- /// taskId : unique value
- /// filePath : PDF file path
- /// password : pdf file password
- /// convertType : Converted format type
- static convert(
- {required String taskId,
- required String filePath,
- String password = "",
- required ConvertType convertType,
- required String outputPath,
- required Options options,
- required TaskResultCallback callback}) {
- HashMap<String, dynamic> map = HashMap();
- map['convertType'] = convertType.name;
- map['filePath'] = filePath;
- map['taskId'] = taskId;
- map['outputDir'] = outputPath;
- map['fileName'] = p.basenameWithoutExtension(filePath);
- map["password"] = password;
- map.addAll(options.map);
- _channel.receiveBroadcastStream(map).listen((event) {
- Map<dynamic, dynamic> result = event;
- int statusCode = result['status'];
- int progress = result['progress'];
- String? outputPath = result['outputPath'];
- String taskId = result['taskId'];
- callback(TaskResult(
- taskId, getConvertStatusByCode(statusCode), progress, outputPath));
- });
- }
- static void cancelTask() {
- _methodChannel.invokeMethod('cancel_task');
- }
- }
|