123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- //
- // KMImageToPDFMethod.swift
- // PDF Reader Pro
- //
- // Created by kdanmobile on 2023/10/26.
- //
- import Foundation
- let kImageToPDFFolderPath = kTempSavePath?.stringByAppendingPathComponent("ImageToPDF")
- @objc(KMImageToPDFMethod)
- protocol KMImageToPDFMethodDelegate: AnyObject {
- func imageToPDFMethod(_ method: KMImageToPDFMethod, progress: Float)
- }
- typealias ImageToPDFResultBlock = (_ savePath: String, _ errorArr: Array<Any>, _ errorOCRArray: Array<Any>) -> Void
- class KMImageToPDFMethod: NSObject, KMGOCRManagerDelegate {
- var imageTopdfDelegate: KMImageToPDFMethodDelegate?
- var password: String = ""
- var convertIndex: Int = 0
- var photoArray: Array<Any>?
- var errorArray: NSMutableArray!
- var isOCR = false
- var isCreatPDF = false
- var isMerge = false
- var isSaveAsText = false
- var saveAsTestPath = ""
-
- var results: NSMutableArray!
- var fileSavePath = ""
- var OCRResultString = ""
-
- var completeBlock: ImageToPDFResultBlock?
-
- var errorOCRArray: NSMutableArray!
-
- var appendPDF: CPDFDocument?
-
- override init() {
- super.init()
- if !FileManager.default.fileExists(atPath: kImageToPDFFolderPath ?? "") {
- try? FileManager.default.createDirectory(at: URL(fileURLWithPath: kImageToPDFFolderPath ?? "") , withIntermediateDirectories: false, attributes: nil)
- }
- self.errorArray = NSMutableArray()
- self.results = NSMutableArray()
- self.errorOCRArray = NSMutableArray()
- }
-
- deinit {
- NotificationCenter.default.removeObserver(self)
- imageTopdfDelegate = nil
- }
-
- func exportPDFFile(fileArray: Array<Any>, savePath: String, isOCR: Bool, isCreatPDF: Bool, isMerge: Bool, isSaveAsText: Bool, complete: @escaping ImageToPDFResultBlock) {
- self.convertIndex = 0
- self.photoArray = fileArray
- self.isOCR = isOCR
- self.isCreatPDF = isCreatPDF
- self.isMerge = isMerge
- self.isSaveAsText = isSaveAsText
- self.fileSavePath = savePath
- self.completeBlock = complete
- self.OCRResultString = ""
- self.errorArray.removeAllObjects()
- self.errorOCRArray.removeAllObjects()
-
- if !isCreatPDF {
- appendPDF = CPDFDocument(url: URL(fileURLWithPath: savePath))
- if ((appendPDF?.unlock(withPassword:self.password)) != nil) {
-
- }
- }
-
- if isOCR {
- let languages = KMGOCRManager.default().selectedLanguages.value(forKeyPath: KMGOCRLanguageCodeKey) as! [Any]
- var images = [AnyObject]()
- for i in 0 ..< (photoArray?.count ?? 0) {
- let filePaht: String = photoArray?[i] as! String
- if filePaht.count > 0 && FileManager.default.fileExists(atPath: filePaht) {
- let image = NSImage(contentsOfFile: filePaht)
- images.append(image!)
- } else {
- self.errorArray.add(filePaht)
- }
- }
- if images.count < 1 {
- self.completeBlock?("", self.errorArray as! Array<Any>, self.errorOCRArray as! Array<Any>)
- return
- }
- let plan = UserDefaults.standard.integer(forKey: "KMOCRCurrentPlanKey")
- if plan == 0 {
- KMGOCRManager.default().ocrType = .google
- } else {
- KMGOCRManager.default().ocrType = .apple
- }
- KMGOCRManager.default().delegate = self
- KMGOCRManager.default().recognitionImages(images as! [NSImage], withLanguages: languages)
- } else {
- if isMerge {
- NotificationCenter.default.addObserver(self, selector: #selector(pdfDocumentPageWrite(notification:)), name: NSNotification.Name("PDFDocumentDidBeginPageWriteNotification"), object: nil)
- self.imageToPDFFile_MergeToOneFile(path: savePath, complete: complete)
- } else {
- self.imageToPDFFile_SeparateToFiles(path: savePath, complete: complete)
- }
- }
- }
-
- func imageToPDFFile_MergeToOneFile(path: String, complete: @escaping ImageToPDFResultBlock) {
- var pdf: CPDFDocument?
- var newPath: String = ""
- if self.isCreatPDF {
- pdf = CPDFDocument()
- newPath = path.stringByAppendingPathComponent("Untitled").stringByAppendingPathExtension("pdf")
- newPath = getUniqueFilePath(newPath)
- } else {
- newPath = path
- pdf = self.appendPDF
- }
-
- for pageCount in 0..<(photoArray?.count ?? 0) {
- var isDir: ObjCBool = false
- let filePath = photoArray?[pageCount] as! String
- if FileManager.default.fileExists(atPath: filePath, isDirectory: &isDir) && !isDir.boolValue {
- let image = NSImage(contentsOfFile: filePath)!
- _ = pdf?.km_insertPage(image.size, withImage: filePath, at: pdf?.pageCount ?? 0)
- }
- }
-
- var isSucceed = false
- if (pdf?.pageCount ?? 0) < 1 {
-
- } else {
- var options: [CPDFDocumentWriteOption : Any] = [:]
- if pdf!.isEncrypted {
- options.updateValue(password, forKey: .userPasswordOption)
- options.updateValue(password, forKey: .userPasswordOption)
- isSucceed = pdf?.write(toFile: newPath, withOptions: options) ?? false
- } else {
- isSucceed = pdf?.write(toFile: newPath) ?? false
- }
- }
-
- if !isSucceed {
- self.errorArray.add((newPath as NSString).lastPathComponent)
- }
-
- DispatchQueue.main.async {
- complete(newPath, self.errorArray as! Array<Any>, self.errorOCRArray as! Array<Any>)
- }
-
- }
- func imageToPDFFile_SeparateToFiles(path: String, complete: @escaping(ImageToPDFResultBlock)) {
- if convertIndex >= photoArray?.count ?? 0 {
- complete(path, Array<Any>(), errorOCRArray as! Array<Any>)
- return
- }
- let filePath: String = photoArray?[convertIndex] as! String
- let savePath = path
- var isDir: ObjCBool = false
- if FileManager.default.fileExists(atPath: filePath , isDirectory: &isDir) && !isDir.boolValue {
- let model = KMImageModel(filepath: filePath )
- let tString = model.photoName.deletingPathExtension.lastPathComponent//model.photoName.deletingPathExtension
-
- var newpath = path.stringByAppendingPathComponent(tString).stringByAppendingPathExtension("pdf")
- newpath = getUniqueFilePath(newpath)
-
- let pdf = CPDFDocument()
-
- if let imag = NSImage(contentsOfFile: filePath ) {
- _ = pdf?.km_insertPage(imag.size, withImage: filePath , at: pdf?.pageCount ?? 0)
- }
-
- // DispatchQueue.global().async {
- var isSucceed = false
-
- isSucceed = pdf?.write(toFile: newpath) ?? false
- var pre: Float = 0
- if self.photoArray?.count ?? 0 > 0{
- pre = Float(self.convertIndex + 1) / Float(self.photoArray?.count ?? 1)
- }else{
- pre = 0
- }
-
- if !isSucceed {
- self.errorArray.add(filePath)
- }
-
- if self.convertIndex < (self.photoArray?.count ?? 0) - 1 {
- self.convertIndex += 1
- self.imageToPDFFile_SeparateToFiles(path: savePath, complete: complete)
-
- DispatchQueue.main.async {
- self.imageTopdfDelegate?.imageToPDFMethod(self, progress: pre)
- }
- } else {
- DispatchQueue.main.async {
- complete(newpath, self.errorArray as! Array<Any>, self.errorOCRArray as! Array<Any>)
- }
- }
- // }
-
- } else {
- if convertIndex < (photoArray?.count ?? 0) - 1 {
- self.convertIndex += 1
- self.imageToPDFFile_SeparateToFiles(path: savePath, complete: complete)
- } else {
- DispatchQueue.main.async {
- complete(path, self.errorArray as! Array<Any>, self.errorOCRArray as! Array<Any>)
- }
- }
- }
- }
-
- func getUniqueFilePath(_ filePath: String) -> String {
- var i = 0
- var uniqueFilePath = filePath
- let fileManager = FileManager.default
- while fileManager.fileExists(atPath: uniqueFilePath) {
- i += 1
- let path = String(format: "%@(%d)", filePath.deletingPathExtension,i)
- uniqueFilePath = path.stringByAppendingPathExtension(filePath.pathExtension)
- }
-
- return uniqueFilePath
- }
-
- @objc func pdfDocumentPageWrite(notification: NSNotification) {
- if ((notification.userInfo?.isEmpty) != nil) { return }
- let num = notification.userInfo!["PDFDocumentPageIndex"] as! NSNumber
- let pdfDocument = notification.object as! CPDFDocument
-
- let pre = Float(num.intValue + 1) / (Float(pdfDocument.pageCount) * 1.0)
- DispatchQueue.main.async {
- self.imageTopdfDelegate?.imageToPDFMethod(self, progress: pre)
- }
- }
-
- //MARK: KMGOCRManagerDelegate
- func gocrManagerDidStartOCR(_ manager: KMGOCRManager!) {
-
- }
- func gocrManagerDidFinishOCR(_ manager: KMGOCRManager!) {
-
- }
- func gocrManager(_ manager: KMGOCRManager!, didCancelOCRImageAt index: Int) {
-
- }
- func gocrManager(_ manager: KMGOCRManager!, didStartOCRImageAt index: Int) {
-
- }
- func gocrManager(_ manager: KMGOCRManager!, didFinishOCRImageAt index: Int, results: [KMGOCRResult]!) {
- if (results != nil) {
- self.dealWithResults(results, OCRImageAtIndex: index)
- }
- }
- func gocrManager(_ manager: KMGOCRManager!, didFailureOCRImageAt index: Int, error: Error!) {
- let results = Array<KMGOCRResult>()
- self.errorOCRArray.add(self.photoArray?[index] as Any)
- self.dealWithResults(results, OCRImageAtIndex: index)
- }
-
-
- func dealWithResults(_ results: [KMGOCRResult]?, OCRImageAtIndex index: Int) {
- if isOCR {
- if isMerge {
- self.results.add(results as Any)
- var key = index
- if isCreatPDF {
- key = Int((self.appendPDF?.pageCount ?? 0) + UInt(index))
- }
- var contents = ""
- if OCRResultString.count > 0 {
- contents = self.OCRResultString
- }
- var str: String = ""
- if results!.count > 0 {
- guard let firstResult: KMGOCRResult = results?.first else {
- return
- }
- str = firstResult.text
- }
-
- contents = contents + "\n"
- contents = contents + "Page" + "\(key + 1)"
- contents = contents + "\n----------\n"
- contents = contents + str
- self.OCRResultString = contents
- if isCreatPDF {
- if index >= (photoArray?.count ?? 0) - 1 {
- var savePath = self.fileSavePath.stringByAppendingPathComponent("Untitled OCR").stringByAppendingPathExtension("pdf")
- savePath = self.getUniqueFilePath(savePath)
- if self.isSaveAsText {
- var savetextPath = self.fileSavePath.stringByAppendingPathComponent("Untitled OCR").stringByAppendingPathExtension("txt")
- savetextPath = self.getUniqueFilePath(savetextPath)
- try? self.OCRResultString.write(to: URL(fileURLWithPath: savetextPath), atomically: true, encoding: .utf8)
- NSWorkspace.shared.selectFile(savetextPath, inFileViewerRootedAtPath: "")
- }
- KMGOCRManager.default().createPDFFile(savePath, imagePaths: self.photoArray, results: (self.results as! [Any]), scale: 1.0)
- DispatchQueue.main.async {
- self.completeBlock?(savePath, self.errorArray as! Array<Any>, self.errorOCRArray as! Array<Any>)
- }
- } else {
- let pre = Float(index + 1) / (Float(photoArray?.count ?? 0) * 1.0)
- DispatchQueue.main.async {
- self.imageTopdfDelegate?.imageToPDFMethod(self, progress: pre)
- }
- }
- } else {
- if index >= (photoArray?.count ?? 0) - 1 {
- var savePath = kImageToPDFFolderPath?.stringByAppendingPathComponent("Untitled OCR").stringByAppendingPathExtension("pdf")
- KMGOCRManager.default().createPDFFile(savePath, imagePaths: self.photoArray, results: (self.results as! [Any]), scale: 1.0)
- DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3){
- if FileManager.default.fileExists(atPath: savePath ?? "") {
- let appPDF = self.appendPDF
- let newPdf = CPDFDocument.init(url: URL(fileURLWithPath: savePath ?? ""))
- for i in 0 ..< (newPdf?.pageCount ?? 0) {
- let page = newPdf?.page(at: i)
- appPDF?.insertPageObject(page, at: appPDF?.pageCount ?? 0)
- }
- var isSuccessfully = false
- let attributes = NSMutableDictionary(dictionary: appPDF!.documentAttributes()!, copyItems: true)
- if ((appPDF?.isEncrypted) != nil) {
- attributes.setValue(self.password, forKey: (kCGPDFContextUserPassword as NSString) as String)
- attributes.setValue(self.password, forKey: (kCGPDFContextOwnerPassword as NSString) as String)
- isSuccessfully = appPDF?.write(toFile: self.fileSavePath, withOptions: (attributes as? [CPDFDocumentWriteOption : Any])) ?? false
- } else {
- isSuccessfully = appPDF?.write(toFile: self.fileSavePath) ?? false
- }
- if self.isSaveAsText {
- var savetextPath = kImageToPDFFolderPath?.stringByAppendingPathComponent("Untitled OCR").stringByAppendingPathExtension("txt")
- if self.saveAsTestPath.isEmpty == false {
- savetextPath = self.saveAsTestPath.stringByAppendingPathComponent("Untitled OCR").stringByAppendingPathExtension("txt")
- }
- savetextPath = self.getUniqueFilePath(savetextPath ?? "")
- try? self.OCRResultString.write(to: URL(fileURLWithPath: savetextPath ?? ""), atomically: true, encoding: .utf8)
- NSWorkspace.shared.selectFile(savetextPath, inFileViewerRootedAtPath: "")
- }
- try? FileManager.default.removeItem(atPath: savePath ?? "")
- DispatchQueue.main.async {
- self.completeBlock?(self.fileSavePath, self.errorArray as! Array<Any>, self.errorOCRArray as! Array<Any>)
- }
- }
- }
- }else{
- let pre = Float(index + 1) / (Float(photoArray?.count ?? 0) * 1.0)
- DispatchQueue.main.async {
- self.imageTopdfDelegate?.imageToPDFMethod(self, progress: pre)
- }
- }
- }
- } else {
- let filePath: String = photoArray?[index] as! String
- let model = KMImageModel(filepath: filePath)
- let tString = model.photoName.deletingPathExtension.lastPathComponent
- var savePath = fileSavePath.stringByAppendingPathComponent(tString).stringByAppendingPathExtension("pdf")
- savePath = getUniqueFilePath(savePath)
- KMGOCRManager.default().createPDFFile(savePath, imagePaths: [filePath], results: [results as Any], scale: 1.0)
- var tFileName = fileSavePath.stringByAppendingPathComponent(tString).stringByAppendingPathExtension("txt")
- tFileName = getUniqueFilePath(tFileName)
- if isSaveAsText {
- var string: String = ""
- if results!.count > 0 {
- guard let firstResult: KMGOCRResult = results?.first else {
- return
- }
- string = firstResult.text
- try? string.write(toFile: tFileName, atomically: true, encoding: .utf8)
- }
- }
- if index < (photoArray?.count ?? 0) - 1 {
- let pre = Float(index + 1) / Float(photoArray?.count ?? 1)
- DispatchQueue.main.async {
- self.imageTopdfDelegate?.imageToPDFMethod(self, progress: pre)
- }
- } else {
- DispatchQueue.main.async {
- self.completeBlock?(savePath, self.errorArray as! Array<Any>, self.errorOCRArray as! Array<Any>)
- }
- }
- }
- }
- }
-
- static func supportedImageTypes() -> [String] {
- return ["jpg", "cur", "bmp", "jpeg", "gif", "png", "tiff", "tif", "ico", "icns", "tga", "psd", "eps", "hdr", "jp2", "jpc", "pict", "sgi"]
- }
-
- }
|