Explorar el Código

compdfkit(RN) - save操作回调和save失败bug修改

yangliuhua hace 3 meses
padre
commit
7bd31fa2d3

+ 22 - 0
RCTDocumentManager.m

@@ -0,0 +1,22 @@
+//  Copyright © 2014-2024 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 <React/RCTBridgeModule.h>
+
+@interface RCT_EXTERN_MODULE(CPDFViewManager, NSObject)
+
+RCT_EXTERN_METHOD(save:(NSInteger)tag
+                  withResolver:(RCTPromiseResolveBlock)resolve
+                  withRejecter:(RCTPromiseRejectBlock)reject)
+
++ (BOOL)requiresMainQueueSetup
+{
+  return NO;
+}
+
+@end

+ 43 - 0
RCTDocumentManager.swift

@@ -0,0 +1,43 @@
+//
+//  RCTDocumentManager.swift
+//  react-native-compdfkit-pdf
+//
+//  Copyright © 2014-2024 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 UIKit
+import ComPDFKit
+
+@objc(CPDFViewManager)
+class RCTDocumentManager: NSObject, RCTBridgeModule {
+    static func moduleName() -> String! {
+        return "RCTDocumentManager"
+    }
+    
+    internal var bridge: RCTBridge!
+    
+    @objc func readerView() -> RCTCPDFReaderView {
+        self.bridge.module(for: RCTCPDFReaderView.self) as! RCTCPDFReaderView
+    }
+    
+    @objc(save: withResolver: withRejecter:)
+    func save(tag: Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
+        DispatchQueue.main.async {
+            let reader = self.readerView()
+            reader.saveDocument(forCPDFViewTag: tag) { success in
+                if success {
+                    resolve(success)
+                } else {
+                    reject("save_failed", "Failed to save document", nil);
+                }
+            }
+            
+        }
+    }
+    
+}

+ 12 - 1
ios/ComPDFKitRN.swift

@@ -130,12 +130,23 @@ class ComPDFKit: NSObject, CPDFViewBaseControllerDelete{
     @objc(openDocument: password: configurationJson:)
     func openDocument(document : URL, password: String, configurationJson : String) -> Void {
         DispatchQueue.main.async {
+            let fileManager = FileManager.default
+            let samplesFilePath = NSHomeDirectory().appending("/Documents/BasicViewer")
+            let fileName = document.lastPathComponent
+            let docsFilePath = samplesFilePath + "/" + fileName
+            
+            if !fileManager.fileExists(atPath: samplesFilePath) {
+                try? FileManager.default.createDirectory(atPath: samplesFilePath, withIntermediateDirectories: true, attributes: nil)
+            }
+            
+            try? FileManager.default.copyItem(atPath: document.path, toPath: docsFilePath)
+            
             let rootNav = ComPDFKit.presentedViewController()
           
             let jsonDataParse = CPDFJSONDataParse(String: configurationJson)
             guard let configuration = jsonDataParse.configuration else { return }
             
-            let pdfViewController = CPDFViewController(filePath: document.path, password: password, configuration: configuration)
+            let pdfViewController = CPDFViewController(filePath: docsFilePath, password: password, configuration: configuration)
             pdfViewController.delegate = self
             let nav = CNavigationController(rootViewController: pdfViewController)
             nav.modalPresentationStyle = .fullScreen

+ 1 - 0
ios/CompdfkitPdf-Bridging-Header.h

@@ -8,3 +8,4 @@
 
 #import <React/RCTBridgeModule.h>
 #import <React/RCTViewManager.h>
+#import <React/RCTBridgeModule.h>

+ 34 - 2
ios/RCTCPDFView.swift

@@ -12,10 +12,17 @@
 
 import UIKit
 import ComPDFKit_Tools
+import ComPDFKit
+
+protocol RCTCPDFViewDelegate: AnyObject {
+    func cpdfViewAttached(_ cpdfView: RCTCPDFView)
+}
 
 class RCTCPDFView: UIView {
     
-    private var pdfViewController : CPDFViewController?
+    weak var delegate: RCTCPDFViewDelegate?
+    
+    public var pdfViewController : CPDFViewController?
     
     private var navigationController : CNavigationController?
    
@@ -27,11 +34,24 @@ class RCTCPDFView: UIView {
         fatalError("init(coder:) has not been implemented")
     }
     
+    // MARK: - Private Methods
+    
     private func createCPDFView() {
+        let fileManager = FileManager.default
+        let samplesFilePath = NSHomeDirectory().appending("/Documents/ReaderViewer")
+        let fileName = document.lastPathComponent
+        let docsFilePath = samplesFilePath + "/" + fileName
+        
+        if !fileManager.fileExists(atPath: samplesFilePath) {
+            try? FileManager.default.createDirectory(atPath: samplesFilePath, withIntermediateDirectories: true, attributes: nil)
+        }
+        
+        try? FileManager.default.copyItem(atPath: document.path, toPath: docsFilePath)
+        
         let jsonData = CPDFJSONDataParse(String: configuration)
         let configurations = jsonData.configuration ?? CPDFConfiguration()
 
-        pdfViewController = CPDFViewController(filePath: document.path, password: password, configuration: configurations)
+        pdfViewController = CPDFViewController(filePath: docsFilePath, password: password, configuration: configurations)
         navigationController = CNavigationController(rootViewController: pdfViewController!)
         navigationController?.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
         navigationController?.view.frame = self.frame
@@ -39,8 +59,20 @@ class RCTCPDFView: UIView {
         navigationController?.setViewControllers([pdfViewController!], animated: true)
         
         addSubview(navigationController?.view ?? UIView())
+        
+        self.delegate?.cpdfViewAttached(self)
+    }
+    
+    // MARK: - Public Methods
+    
+    func saveDocument(completionHandler: @escaping (Bool) -> Void) {
+        let document = self.pdfViewController?.pdfListView?.document
+        let success = document?.write(to: document?.documentURL ?? URL(fileURLWithPath: ""), isSaveFontSubset: true) ?? false
+        completionHandler(success)
     }
     
+    // MARK: - RCT Methods
+    
     private var configuration: String = ""
     @objc func setConfiguration(_ newSection: String) {
        configuration = newSection

+ 0 - 1
ios/RCTCPDFViewManager.m

@@ -12,7 +12,6 @@
 
 #import <Foundation/Foundation.h>
 #import <React/RCTViewManager.h>
-#import <MapKit/MapKit.h>
 
 @interface RCT_EXTERN_MODULE(RCTCPDFReaderView, RCTViewManager)
 

+ 18 - 2
ios/RCTCPDFViewManager.swift

@@ -11,17 +11,33 @@
 //
 
 import UIKit
+import ComPDFKit
 
 @objc(RCTCPDFReaderView)
-class RCTCPDFReaderView: RCTViewManager {
+class RCTCPDFReaderView: RCTViewManager, RCTCPDFViewDelegate {
     
     @objc override static func requiresMainQueueSetup() -> Bool {
         return true
     }
     
+    var cpdfViews: Dictionary<Int, RCTCPDFView> = [:]
+    
     @objc override func view() -> UIView! {
         let rtcCPDFView = RCTCPDFView()
+        rtcCPDFView.delegate = self
         return rtcCPDFView
     }
-  
+    
+    func saveDocument(forCPDFViewTag tag: Int, completionHandler: @escaping (Bool) -> Void) {
+        let rtcCPDFView = cpdfViews[tag]
+        rtcCPDFView?.saveDocument(completionHandler: { success in
+            completionHandler(success)
+        })
+    }
+    
+    // MARK: - RCTCPDFViewDelegate
+    
+    func cpdfViewAttached(_ cpdfView: RCTCPDFView) {
+        cpdfViews[cpdfView.reactTag.intValue] = cpdfView
+    }
 }