ConverFilePlugin.swift 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //
  2. // ConverFilePlugin.swift
  3. // Runner
  4. //
  5. // Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. import Foundation
  13. import ComPDFKit_Conversion
  14. class ConverFilePlugin{
  15. init(messenger : FlutterBinaryMessenger){
  16. let channel = FlutterEventChannel(name: "com.compdfkit.conversion.flutter.convert", binaryMessenger: messenger)
  17. channel.setStreamHandler(SwiftStreamHandler())
  18. }
  19. }
  20. class SwiftStreamHandler: NSObject,FlutterStreamHandler {
  21. var util:ConvertUtil?
  22. public func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
  23. let convertData = arguments as! [String: String];
  24. let filePath = convertData[params_file_path] ?? "";
  25. let convertType = convertData[params_convert_type]!;
  26. let dataTag = String(convertData[params_data_tag]!);
  27. let fileName = convertData[params_file_name] ?? "";
  28. let queue = DispatchQueue(label: "com.compdfkit.conversion.flutter")
  29. queue.async {
  30. self.util = ConvertUtil()
  31. let options = self.util?.getConvertOptions(withArguments: arguments)
  32. self.util?.convert(options: options!, filePath: filePath,fileName: fileName, convertType: convertType) { progress, status, outputPath in
  33. events(["progress": progress, params_data_tag: dataTag, "status" : status, "outputPath" : outputPath])
  34. }
  35. }
  36. return nil
  37. }
  38. public func onCancel(withArguments arguments: Any?) -> FlutterError? {
  39. return nil
  40. }
  41. }