KMBatchManager.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. //
  2. // KMBatchManager.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/2/17.
  6. //
  7. import Cocoa
  8. enum KMBatchManagerSate: Int, CaseIterable {
  9. case unknow = 0
  10. case processing
  11. case complete
  12. case error
  13. }
  14. let kBacthFilesProcessNotification = "kBacthFilesProcessNotification"
  15. let kBacthProcessNotification = "kBacthProcessNotification"
  16. class KMBatchManager: NSObject {
  17. public static let manager = KMBatchManager()
  18. fileprivate(set) var state: KMBatchManagerSate = .unknow
  19. var filesData: [KMBatchProcessingTableViewModel] = []
  20. var batchFilesData: [KMBatchProcessingTableViewModel] {
  21. get {
  22. var resultArray:[KMBatchProcessingTableViewModel] = []
  23. for item in filesData {
  24. if !item.isLock {
  25. resultArray.append(item)
  26. }
  27. }
  28. return resultArray
  29. }
  30. }
  31. func batch(type: KMBatchCollectionViewType, data: KMBatchSettingItemViewModel) {
  32. let panel = NSOpenPanel()
  33. panel.canChooseFiles = false
  34. panel.canChooseDirectories = true
  35. panel.canCreateDirectories = true
  36. panel.beginSheetModal(for: NSWindow.currentWindow()) { response in
  37. if response == .cancel {
  38. return
  39. }
  40. let outputFolderPath = (panel.url?.path)!
  41. //
  42. self.batchUnkown()
  43. switch type {
  44. case .convertPDF:
  45. self.convertPDFExport(data: data, outputFolderPath: outputFolderPath)
  46. break
  47. case .OCR:
  48. self.convertOCRExport(data: data, outputFolderPath: outputFolderPath)
  49. break
  50. case .compress:
  51. self.compressExport(data: data, outputFolderPath: outputFolderPath)
  52. break
  53. case .security:
  54. self.securityExport(data: data, outputFolderPath: outputFolderPath)
  55. break
  56. case .watermark:
  57. break
  58. case .background:
  59. self.backgroundApplay(data: data, outputFolderPath: outputFolderPath)
  60. break
  61. case .headerAndFooter:
  62. break
  63. case .batesNumber:
  64. break
  65. case .batchRemove:
  66. self.removeApplay(data: data, outputFolderPath: outputFolderPath)
  67. break
  68. default:
  69. KMPrint("找不到")
  70. break
  71. }
  72. //
  73. self.batchProgress()
  74. }
  75. }
  76. }
  77. protocol KMBatchSettingViewExport {}
  78. extension KMBatchManager: KMBatchSettingViewExport {
  79. //MARK: 转档
  80. func convertPDFExport(data: KMBatchSettingItemViewModel, outputFolderPath: String) {
  81. self.convertFile(outputFolderPath: outputFolderPath, data: data, filesData: self.batchFilesData)
  82. }
  83. func convertFile(outputFolderPath: String, data: KMBatchSettingItemViewModel, filesData: [KMBatchProcessingTableViewModel]) {
  84. if filesData.count != 0 {
  85. DispatchQueue.global().async {
  86. for i in 0..<filesData.count {
  87. let item = filesData[i]
  88. //创建Document
  89. let filePath = item.filePath
  90. let document = self.fetchDocument(filePath: filePath, model: item)
  91. let settingData = data as? KMBatchConvertPDFViewModel ?? KMBatchConvertPDFViewModel()
  92. var fileName = filePath.deletingPathExtension.lastPathComponent
  93. if ((fileName.isEmpty)) {
  94. fileName = NSLocalizedString("Untitled", comment: "")
  95. }
  96. let convert = self.addConvertParameter(settingData)
  97. let pageCount = document.pageCount
  98. //获取page
  99. var pages:[Int] = []
  100. for i in 0..<pageCount {
  101. pages.append(Int(i)+1)
  102. }
  103. convert.outputFolderPath = outputFolderPath
  104. convert.filePath = filePath
  105. convert.outputFileName = fileName
  106. convert.pages = pages
  107. convert.isAllowOCR = settingData.needRecognizeText
  108. convert.ocrLanguage = settingData.languageType
  109. item.state = .clock
  110. KMPDFConvertManager.defaultManager.convert(convert: convert, progress: { [unowned self] progressValue in
  111. print("转档进度 - \(progressValue)")
  112. let progress = Float(progressValue) / Float(pageCount)
  113. self.itemProgress(item: item, processValue: progress)
  114. }, completion: { [unowned self] finished, error in
  115. if finished {
  116. if FileManager.default.fileExists(atPath: outputFolderPath) {
  117. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: outputFolderPath)])
  118. }
  119. self.itemSuccess(item: item)
  120. } else {
  121. self.itemFailure(item: item, error: error! as NSError)
  122. }
  123. if i == filesData.count - 1 {
  124. self.batchSuccess()
  125. }
  126. })
  127. }
  128. }
  129. }
  130. }
  131. func addConvertParameter(_ data: KMBatchConvertPDFViewModel) -> KMPDFConvert {
  132. let settingData = data
  133. var convert = KMPDFConvert()
  134. switch settingData.convertPDFType {
  135. case .word:
  136. convert = KMPDFConvertWord()
  137. if settingData.layoutSettingType == .flowingText {
  138. convert.isAllInOneSheet = false
  139. } else {
  140. convert.isAllInOneSheet = true
  141. }
  142. case .excel:
  143. convert = KMPDFConvertExcel()
  144. if settingData.excelSetting == .separate {
  145. convert.isAllInOneSheet = false
  146. convert.isExtractTable = false
  147. } else if settingData.excelSetting == .format {
  148. convert.isAllInOneSheet = true
  149. convert.isExtractTable = false
  150. } else if settingData.excelSetting == .tables {
  151. convert.isAllInOneSheet = false
  152. convert.isExtractTable = true
  153. switch settingData.excelTablesType {
  154. case .oneTable:
  155. convert.extractTableIndex = 0
  156. case .pageTable:
  157. convert.extractTableIndex = 1
  158. case .allTable:
  159. convert.extractTableIndex = 2
  160. default:
  161. KMPrint("未找到")
  162. }
  163. }
  164. case .ppt:
  165. convert = KMPDFConvertPPT()
  166. case .csv:
  167. convert = KMPDFConvertCSV()
  168. if settingData.csvOnlyTables {
  169. convert.isExtractTable = true
  170. switch settingData.excelTablesType {
  171. case .oneTable:
  172. convert.extractTableIndex = 0
  173. case .pageTable:
  174. convert.extractTableIndex = 1
  175. case .allTable:
  176. convert.extractTableIndex = 2
  177. default:
  178. KMPrint("未找到")
  179. }
  180. } else {
  181. convert.isExtractTable = false
  182. }
  183. case .image:
  184. convert = KMPDFConvertImage()
  185. convert.convertType = data.imageType
  186. var dpi: Int = 150
  187. if data.imageDpiIndex == 0 {
  188. dpi = 50
  189. } else if data.imageDpiIndex == 1 {
  190. dpi = 72
  191. } else if data.imageDpiIndex == 2 {
  192. dpi = 96
  193. } else if data.imageDpiIndex == 3 {
  194. dpi = 150
  195. } else if data.imageDpiIndex == 4 {
  196. dpi = 300
  197. } else if data.imageDpiIndex == 5 {
  198. dpi = 600
  199. } else {
  200. dpi = 150
  201. }
  202. if (convert.convertType == .jpeg) {
  203. (convert as! KMPDFConvertImage).imageType = .JPEG
  204. (convert as! KMPDFConvertImage).imageDpi = dpi
  205. } else if (convert.convertType == .png) {
  206. (convert as! KMPDFConvertImage).imageType = .PNG
  207. (convert as! KMPDFConvertImage).imageDpi = dpi
  208. } else {
  209. (convert as! KMPDFConvertImage).imageDpi = 150
  210. }
  211. case .html:
  212. convert = KMPDFConvertHTML()
  213. case .rtf:
  214. convert = KMPDFConvertRTF()
  215. case .json:
  216. convert = KMPDFConvertJson()
  217. if settingData.jsonType == .extractText {
  218. convert.isAllInOneSheet = false
  219. } else {
  220. convert.isAllInOneSheet = true
  221. }
  222. case .text:
  223. convert = KMPDFConvertText()
  224. default:
  225. KMPrint("不清楚")
  226. }
  227. return convert
  228. }
  229. //MARK: OCR
  230. func convertOCRExport(data: KMBatchSettingItemViewModel, outputFolderPath: String) {
  231. self.convertOCR(outputFolderPath: outputFolderPath, data: data as! KMOCRModel, filesData: self.batchFilesData)
  232. }
  233. func convertOCR(outputFolderPath: String, data: KMOCRModel, filesData: [KMBatchProcessingTableViewModel]?) {
  234. guard let filesData = filesData else { return }
  235. for i in 0..<filesData.count {
  236. let item = (filesData[i])
  237. let document = CPDFDocument.init(url: URL(fileURLWithPath: item.filePath))
  238. if document != nil {
  239. //计算需要处理的页面
  240. var fileName = item.filePath.deletingPathExtension.lastPathComponent
  241. if ((fileName.isEmpty)) {
  242. fileName = NSLocalizedString("Untitled", comment: "")
  243. }
  244. let path = outputFolderPath + "/" + fileName + ".pdf"
  245. KMOCRManager.manager.convertBatchOCR(document: document!, saveFilePath: path, model: data, progress: { [unowned self] progress in
  246. self.itemProgress(item: item, processValue: progress)
  247. }) { [unowned self] document, text, error in
  248. if error == nil {
  249. self.itemSuccess(item: item)
  250. } else {
  251. self.itemFailure(item: item, error: error! as NSError)
  252. }
  253. if i == filesData.count - 1 {
  254. self.batchSuccess()
  255. }
  256. }
  257. }
  258. }
  259. }
  260. //MARK: 压缩
  261. func compressExport(data: KMBatchSettingItemViewModel, outputFolderPath: String) {
  262. self.compressFile(outputFolderPath: outputFolderPath, data: (data as? KMCompressSettingModel)!, filesData: self.batchFilesData)
  263. }
  264. func compressFile(outputFolderPath: String, data: KMCompressSettingModel, filesData: [KMBatchProcessingTableViewModel]) {
  265. if filesData.count != 0 {
  266. for i in 0..<filesData.count {
  267. let item = filesData[i]
  268. let document = CPDFDocument.init(url: URL(fileURLWithPath: item.filePath))
  269. if document != nil {
  270. var fileName = item.filePath.deletingPathExtension.lastPathComponent
  271. if ((fileName.isEmpty)) {
  272. fileName = NSLocalizedString("Untitled", comment: "")
  273. }
  274. let path = outputFolderPath + "/" + fileName + ".pdf"
  275. KMCompressManager.shared.compress(documentURL: URL(fileURLWithPath: item.filePath), fileURL: URL(fileURLWithPath: path), limit: false, model: data) { currentPage, totalPages in
  276. let progress = Float(currentPage) / Float(totalPages)
  277. self.itemProgress(item: item, processValue: progress)
  278. } cancelHandler: {
  279. return false
  280. } completionHandler: { [unowned self] isFinish in
  281. if isFinish {
  282. self.itemSuccess(item: item)
  283. } else {
  284. self.itemFailure(item: item, error: nil)
  285. }
  286. if i == filesData.count - 1 {
  287. self.batchSuccess()
  288. }
  289. }
  290. }
  291. }
  292. }
  293. }
  294. //MARK: 安全
  295. func securityExport(data: KMBatchSettingItemViewModel, outputFolderPath: String) {
  296. self.securityFile(outputFolderPath: outputFolderPath, data: data as! KMBatchSecurityViewModel, filesData: self.batchFilesData)
  297. }
  298. func securityFile(outputFolderPath: String, data: KMBatchSecurityViewModel, filesData: [KMBatchProcessingTableViewModel]) {
  299. if filesData.count != 0 {
  300. for i in 0..<filesData.count {
  301. let item = filesData[i]
  302. let docuemt = CPDFDocument.init(url: URL(fileURLWithPath: item.filePath))
  303. if (docuemt != nil) {
  304. var fileName = item.filePath.deletingPathExtension.lastPathComponent
  305. if ((fileName.isEmpty)) {
  306. fileName = NSLocalizedString("Untitled", comment: "")
  307. }
  308. let path = outputFolderPath + "/" + fileName + ".pdf"
  309. var options: [CPDFDocumentWriteOption : Any] = [:]
  310. //开启密码
  311. if data.isOpenPassword &&
  312. !data.openPasswordString.isEmpty {
  313. options.updateValue(data.openPasswordString, forKey: .userPasswordOption)
  314. }
  315. //
  316. //权限密码
  317. if data.isPermission &&
  318. !data.permissionString.isEmpty {
  319. options.updateValue(data.permissionString, forKey: .ownerPasswordOption)
  320. }
  321. // 限制打印
  322. if data.restrictOptions.contains(.print) {
  323. options.updateValue(false, forKey: .allowsPrintingOption)
  324. } else {
  325. options.updateValue(true, forKey: .allowsPrintingOption)
  326. }
  327. //限制复制
  328. if data.restrictOptions.contains(.copy) {
  329. options.updateValue(false, forKey: .allowsCopyingOption)
  330. } else {
  331. options.updateValue(true, forKey: .allowsCopyingOption)
  332. }
  333. let result = docuemt!.write(toFile: path, withOptions: options)
  334. // let result = docuemt!.write(to: URL(fileURLWithPath: path), withOptions: options)
  335. if result {
  336. KMPrint("成功")
  337. self.itemSuccess(item: item)
  338. } else {
  339. KMPrint("失败")
  340. self.itemFailure(item: item, error: nil)
  341. }
  342. if i == filesData.count - 1 {
  343. self.batchSuccess()
  344. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: outputFolderPath)])
  345. }
  346. }
  347. }
  348. }
  349. }
  350. //MARK: 水印
  351. func waterMarkApplay(data: KMBatchSettingItemViewModel, outputFolderPath: String) {
  352. }
  353. func waterMarkFile(outputFolderPath: String, data: KMBatchSettingItemViewModel, filesData: [KMBatchProcessingTableViewModel]?) {
  354. }
  355. //MARK: 背景
  356. func backgroundApplay(data: KMBatchSettingItemViewModel, outputFolderPath: String) {
  357. if let data = data as? KMBatchBackgroundModel {
  358. self.backgroundFile(outputFolderPath: outputFolderPath, data: data, filesData: self.batchFilesData)
  359. }
  360. }
  361. func backgroundFile(outputFolderPath: String, data: KMBatchBackgroundModel, filesData: [KMBatchProcessingTableViewModel]) {
  362. if filesData.count != 0 {
  363. for i in 0..<filesData.count {
  364. let item = filesData[i]
  365. var fileName = item.filePath.deletingPathExtension.lastPathComponent
  366. if ((fileName.isEmpty)) {
  367. fileName = NSLocalizedString("Untitled", comment: "")
  368. }
  369. let path = outputFolderPath + "/" + fileName + ".pdf"
  370. let document = CPDFDocument.init(url: URL(fileURLWithPath: item.filePath))
  371. if (document?.allowsPrinting == false || document?.allowsCopying == false) {
  372. let alert = NSAlert()
  373. alert.alertStyle = .critical
  374. alert.messageText = "此文档不允许修改"
  375. alert.runModal()
  376. return
  377. }
  378. if let background = document?.background(), let model = data.backgroundModel {
  379. KMBackgroundManager.defaultManager.updateBackground(background, withModel: model)
  380. var pages = Array(0..<Int(document!.pageCount))
  381. let pageIndexString = pages.isEmpty ? "" : pages.map { "\($0)" }.joined(separator: ",")
  382. background.pageString = pageIndexString
  383. background.update()
  384. }
  385. if (FileManager.default.fileExists(atPath: path)) {
  386. try?FileManager.default.removeItem(atPath: path)
  387. }
  388. let result = document?.write(to: URL(fileURLWithPath: path)) ?? false
  389. if (result) {
  390. KMPrint("removeFile成功")
  391. self.itemSuccess(item: item)
  392. } else {
  393. KMPrint("removeFile失败")
  394. self.itemFailure(item: item, error: nil)
  395. }
  396. if i == filesData.count - 1 {
  397. self.batchSuccess()
  398. }
  399. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: path)])
  400. }
  401. }
  402. }
  403. //MARK: 页眉页脚
  404. func headAndFooterApplay(data: KMBatchSettingItemViewModel, outputFolderPath: String) {
  405. }
  406. func headAndFooterFile(outputFolderPath: String, data: KMBatchSettingItemViewModel, filesData: [KMBatchProcessingTableViewModel]?) {
  407. }
  408. //MARK: 贝茨码
  409. func batesApplay(data: KMBatchSettingItemViewModel, outputFolderPath: String) {
  410. }
  411. func batesFile(outputFolderPath: String, data: KMBatchSettingItemViewModel, filesData: [KMBatchProcessingTableViewModel]?) {
  412. }
  413. //MARK: 移除
  414. func removeApplay(data: KMBatchSettingItemViewModel, outputFolderPath: String) {
  415. self.removeFile(outputFolderPath: outputFolderPath, data: data as! KMBatchRemoveViewModel, filesData: self.batchFilesData)
  416. }
  417. func removeFile(outputFolderPath: String, data: KMBatchRemoveViewModel, filesData: [KMBatchProcessingTableViewModel]) {
  418. if filesData.count != 0 {
  419. for i in 0..<filesData.count {
  420. let item = filesData[i]
  421. // DispatchQueue.global().async {
  422. var fileName = item.filePath.deletingPathExtension.lastPathComponent
  423. if ((fileName.isEmpty)) {
  424. fileName = NSLocalizedString("Untitled", comment: "")
  425. }
  426. let path = outputFolderPath + "/" + fileName + ".pdf"
  427. let document = CPDFDocument.init(url: URL(fileURLWithPath: item.filePath))
  428. if document != nil {
  429. if (document!.allowsPrinting == false || document!.allowsCopying == false) {
  430. let alert = NSAlert()
  431. alert.alertStyle = .critical
  432. alert.messageText = "此文档不允许修改"
  433. alert.runModal()
  434. return
  435. }
  436. if (data.options.contains(.security)) {
  437. }
  438. if (data.options.contains(.batesNumber)) {
  439. let property = document!.bates()
  440. property?.clear()
  441. }
  442. if (data.options.contains(.headerAndFooter)) {
  443. let property = document!.headerFooter()
  444. property?.clear()
  445. }
  446. if (data.options.contains(.background)) {
  447. let property = document!.background()
  448. property?.clear()
  449. }
  450. if (data.options.contains(.watermark)) {
  451. let array: Array<CPDFWatermark> = document!.watermarks() ?? []
  452. for model in array {
  453. document!.removeWatermark(model)
  454. }
  455. }
  456. if (FileManager.default.fileExists(atPath: path)) {
  457. try?FileManager.default.removeItem(atPath: path)
  458. }
  459. let result = document!.write(to: URL(fileURLWithPath: path))
  460. if (result) {
  461. KMPrint("removeFile成功")
  462. self.itemSuccess(item: item)
  463. } else {
  464. KMPrint("removeFile失败")
  465. self.itemFailure(item: item, error: nil)
  466. }
  467. if i == filesData.count - 1 {
  468. self.batchSuccess()
  469. }
  470. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: outputFolderPath)])
  471. }
  472. // }
  473. }
  474. }
  475. }
  476. }
  477. //MARK: private
  478. extension KMBatchManager {
  479. func fetchDocument(filePath: String, model: KMBatchProcessingTableViewModel) -> CPDFDocument {
  480. var document = CPDFDocument(url: URL(fileURLWithPath: filePath))
  481. if model.pageRange == .all {
  482. } else {
  483. let data = KMOCRModel()
  484. data.pageRangeType = model.pageRange
  485. data.pageRangeString = model.pageRangeString
  486. let pages:[Int] = KMOCRManager.manager.fetchPageIndex(document: document!, model: data)
  487. var tempDocument = CPDFDocument()
  488. for i in 0..<pages.count {
  489. let page = document?.page(at: UInt(i))
  490. tempDocument?.insertPageObject(page, at: tempDocument?.pageCount ?? 0)
  491. }
  492. let fileName = filePath.deletingPathExtension.lastPathComponent
  493. let isSuccess = tempDocument?.write(toFile: self.fetchTempFilePath(fileName: fileName))
  494. if isSuccess != nil {
  495. document = tempDocument
  496. }
  497. }
  498. return document ?? CPDFDocument()
  499. }
  500. func fetchTempFilePath(fileName: String) -> String {
  501. let floderPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("BatchTemp")
  502. let filePath = floderPath?.stringByAppendingPathComponent("\(fileName).pdf")
  503. if let data = floderPath, !FileManager.default.fileExists(atPath: data) {
  504. try?FileManager.default.createDirectory(atPath: data, withIntermediateDirectories: false)
  505. }
  506. if let data = filePath, !FileManager.default.fileExists(atPath: data) {
  507. FileManager.default.createFile(atPath: data, contents: nil)
  508. }
  509. return filePath ?? ""
  510. }
  511. func removeTempFilePath(filePath: String) {
  512. let fileName = filePath.deletingPathExtension.lastPathComponent
  513. let path = self.fetchTempFilePath(fileName: fileName)
  514. if (FileManager.default.fileExists(atPath: path)) {
  515. try?FileManager.default.removeItem(atPath: path)
  516. }
  517. }
  518. }
  519. //MARK: Alert
  520. extension KMBatchManager {
  521. func batchUnkown() {
  522. self.state = .unknow
  523. NotificationCenter.default.post(name: NSNotification.Name(kBacthProcessNotification), object: nil)
  524. }
  525. func batchProgress() {
  526. self.state = .processing
  527. NotificationCenter.default.post(name: NSNotification.Name(kBacthProcessNotification), object: nil)
  528. }
  529. func batchSuccess() {
  530. self.state = .complete
  531. NotificationCenter.default.post(name: NSNotification.Name(kBacthProcessNotification), object: nil)
  532. }
  533. func batchFailure() {
  534. self.state = .error
  535. NotificationCenter.default.post(name: NSNotification.Name(kBacthProcessNotification), object: nil)
  536. }
  537. func itemProgress(item: KMBatchProcessingTableViewModel, processValue: Float) {
  538. if processValue > 0.7 {
  539. item.state = .loading70
  540. } else {
  541. item.state = .loading
  542. }
  543. NotificationCenter.default.post(name: NSNotification.Name(kBacthFilesProcessNotification), object: item)
  544. }
  545. func itemSuccess(item: KMBatchProcessingTableViewModel) {
  546. self.removeTempFilePath(filePath: item.filePath)
  547. item.state = .success
  548. NotificationCenter.default.post(name: NSNotification.Name(kBacthFilesProcessNotification), object: item)
  549. }
  550. func itemFailure(item: KMBatchProcessingTableViewModel, error: NSError?) {
  551. self.removeTempFilePath(filePath: item.filePath)
  552. item.state = .error
  553. NotificationCenter.default.post(name: NSNotification.Name(kBacthFilesProcessNotification), object: item)
  554. guard let error = error else { return }
  555. var errorString = ""
  556. let myError: NSError = error as NSError
  557. if myError.code == 1 {
  558. errorString = NSLocalizedString("Password required or incorrect password. Please re-enter your password and try again", comment: "")
  559. } else if myError.code == 2 {
  560. errorString = NSLocalizedString("The license doesn't allow the permission", comment: "")
  561. } else if myError.code == 3 {
  562. errorString = NSLocalizedString("Malloc failure", comment: "")
  563. } else if myError.code == 4 {
  564. errorString = NSLocalizedString("Unknown error in processing conversion. Please try again later", comment: "")
  565. } else if myError.code == 5 {
  566. errorString = NSLocalizedString("Unknown error in processing PDF. Please try again later", comment: "")
  567. } else if myError.code == 6 {
  568. errorString = NSLocalizedString("File not found or could not be opened. Check if your file exists or choose another file to convert", comment: "")
  569. } else if myError.code == 7 {
  570. errorString = NSLocalizedString("File not in PDF format or corruptead. Change a PDF file and try again", comment: "")
  571. } else if myError.code == 8 {
  572. errorString = NSLocalizedString("Unsupported security scheme", comment: "")
  573. } else if myError.code == 9 {
  574. errorString = NSLocalizedString("Page not found or content error", comment: "")
  575. } else {
  576. errorString = NSLocalizedString("Table not found", comment: "")
  577. }
  578. let alert = NSAlert()
  579. alert.alertStyle = .critical
  580. alert.messageText = NSLocalizedString("Conversion Failed", comment: "")
  581. alert.informativeText = errorString
  582. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  583. alert.runModal()
  584. }
  585. }