CompdfkitFlutterPlugin.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright © 2014-2025 PDF Technologies, Inc. All Rights Reserved.
  2. //
  3. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  4. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  5. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  6. // This notice may not be removed from this file.
  7. import Flutter
  8. import UIKit
  9. import ComPDFKit
  10. import ComPDFKit_Tools
  11. public class CompdfkitFlutterPlugin: NSObject, FlutterPlugin, CPDFViewBaseControllerDelete, UIDocumentPickerDelegate {
  12. public var messager : FlutterBinaryMessenger?
  13. private var _reuslt: FlutterResult?
  14. private var pdfViewController: CPDFViewController?
  15. public static func register(with registrar: FlutterPluginRegistrar) {
  16. let channel = FlutterMethodChannel.init(name: "com.compdfkit.flutter.plugin", binaryMessenger: registrar.messenger())
  17. let instance = CompdfkitFlutterPlugin()
  18. instance.messager = registrar.messenger()
  19. registrar.addMethodCallDelegate(instance, channel: channel)
  20. let factory = CPDFViewCtrlFactory(messenger: registrar.messenger())
  21. registrar.register(factory, withId: "com.compdfkit.flutter.ui.pdfviewer")
  22. }
  23. public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
  24. switch call.method {
  25. case "sdk_version_code":
  26. result(CPDFKit.sharedInstance().versionNumber)
  27. case "init_sdk":
  28. let initInfo = call.arguments as? [String: Any]
  29. let key = initInfo?["key"] ?? ""
  30. let code = CPDFKit.verify(withKey: key as? String)
  31. print("Code \(code)")
  32. case "init_sdk_keys":
  33. let initInfo = call.arguments as? [String: Any]
  34. let key = initInfo?["iosOnlineLicense"] ?? ""
  35. CPDFKit.verify(withOnlineLicense: key as? String) { code, message in
  36. print("Code: \(code), Message:\(String(describing: message))")
  37. }
  38. case "sdk_build_tag":
  39. result("iOS build tag:\(CPDFKit.sharedInstance().buildNumber)")
  40. case "open_document":
  41. let initInfo = call.arguments as? [String: Any]
  42. let jsonString = initInfo?["configuration"] ?? ""
  43. _ = initInfo?["password"] ?? ""
  44. let path = initInfo?["document"] as? String ?? ""
  45. let document = NSURL(fileURLWithPath: path)
  46. var success = false
  47. var documentPath = path
  48. let homeDiectory = NSHomeDirectory()
  49. let bundlePath = Bundle.main.bundlePath
  50. if (path.hasPrefix(homeDiectory) || path.hasPrefix(bundlePath)) {
  51. let fileManager = FileManager.default
  52. let samplesFilePath = NSHomeDirectory().appending("/Documents/Files")
  53. let fileName = document.lastPathComponent ?? ""
  54. let docsFilePath = samplesFilePath + "/" + fileName
  55. if !fileManager.fileExists(atPath: samplesFilePath) {
  56. try? FileManager.default.createDirectory(atPath: samplesFilePath, withIntermediateDirectories: true, attributes: nil)
  57. }
  58. try? FileManager.default.copyItem(atPath: document.path ?? "", toPath: docsFilePath)
  59. documentPath = docsFilePath
  60. } else {
  61. success = document.startAccessingSecurityScopedResource()
  62. }
  63. let jsonDataParse = CPDFJSONDataParse(String: jsonString as! String)
  64. guard let configuration = jsonDataParse.configuration else { return }
  65. if let rootViewControl = UIApplication.shared.keyWindow?.rootViewController {
  66. var tRootViewControl = rootViewControl
  67. if let presentedViewController = rootViewControl.presentedViewController {
  68. tRootViewControl = presentedViewController
  69. }
  70. pdfViewController = CPDFViewController(filePath: documentPath, password: nil, configuration: configuration)
  71. let navController = CNavigationController(rootViewController: pdfViewController!)
  72. pdfViewController?.delegate = self
  73. navController.modalPresentationStyle = .fullScreen
  74. tRootViewControl.present(navController, animated: true)
  75. }
  76. if success {
  77. document.stopAccessingSecurityScopedResource()
  78. }
  79. case "get_temporary_directory":
  80. result(self.getTemporaryDirectory())
  81. case "pick_file":
  82. let documentTypes = ["com.adobe.pdf"]
  83. let documentPickerViewController = UIDocumentPickerViewController(documentTypes: documentTypes, in: .open)
  84. documentPickerViewController.delegate = self
  85. UIApplication.presentedViewController()?.present(documentPickerViewController, animated: true, completion: nil)
  86. _reuslt = result
  87. case "remove_sign_file_list":
  88. CSignatureManager.sharedManager.removeAllSignatures()
  89. result(true)
  90. case "set_import_font_directory":
  91. let initInfo = call.arguments as? [String: Any]
  92. let dirPath = initInfo?["dir_path"] as? String ?? ""
  93. let addSysFont = initInfo?["add_sys_font"] as? Bool ?? true
  94. let fileManager = FileManager.default
  95. let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
  96. let destinationPath = documentDirectory.appendingPathComponent("Font")
  97. do {
  98. if fileManager.fileExists(atPath: destinationPath.path) {
  99. try fileManager.removeItem(at: destinationPath)
  100. }
  101. try fileManager.copyItem(atPath: dirPath, toPath: destinationPath.path)
  102. CPDFFont.setImportDir(destinationPath.path, isContainSysFont: addSysFont)
  103. } catch {
  104. print("Error copying Font directory: \(error)")
  105. }
  106. result(true)
  107. case "create_document_plugin":
  108. let uId = call.arguments as? String ?? "";
  109. var documentPlugin = CPDFDocumentPlugin(uid: uId, binaryMessager: self.messager!)
  110. result(true)
  111. break;
  112. default:
  113. result(FlutterMethodNotImplemented)
  114. }
  115. }
  116. func getTemporaryDirectory() -> String {
  117. let paths = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)
  118. return paths.first ?? ""
  119. }
  120. // MARK: - CPDFViewBaseControllerDelete
  121. public func PDFViewBaseControllerDissmiss(_ baseControllerDelete: CPDFViewBaseController) {
  122. baseControllerDelete.dismiss(animated: true)
  123. }
  124. // MARK: - UIDocumentPickerDelegate
  125. public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
  126. let fileUrlAuthozied = urls.first?.startAccessingSecurityScopedResource() ?? false
  127. if fileUrlAuthozied {
  128. let filePath = urls.first?.path ?? ""
  129. _reuslt?(filePath)
  130. }
  131. }
  132. }