FileConverter.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // FileConverter.swift
  3. // KdanAuto
  4. //
  5. // Created by 朱东勇 on 2022/12/21.
  6. //
  7. import Foundation
  8. import ComPDFKit_Conversion
  9. import PDFKit
  10. let KMComPDFKit_Conversion_FreeKey = "hw5vIMzKU2Im6cPijLjG45VAHy01ikUF5/GdAMdzCV8n2QTKNN0d8kG8crVbGYjIMsvVP+A9gx5g+W8qEi4ho0YTkGDratHOrX68W7khEBHAv7OEyRxIhlhBmdEQD+21lsRpIEW5zucUAF231lo3ZZiiT2s9CualDQJdAo1G0DI=";
  11. let KMComPDFKit_Conversion_FreeSecret = "mG0c3O3Mzeu5dkZJW3gpqh188cTuhYlGRPrbR/gfX4p9ms/F1zY6gZ1RBu8mNJH8zLwqe9EJswNmwV09TNi2IiIDeG7Ulx+U4pbeFB3/eYl2J6LePb3Kl54OsKlYiaf3Mv7C2jvq4o0q6sQtN3jR4897KG6mIUGJSRuOsvjYroVgMwna6EUx0K0SBmBGoFYDYkwcNWVMyYvggPV2rTFvfawopGIC034QzjthmhwwX90=";
  12. let KMPDFConvertOptionsKeyImageDPI = "KMPDFConvertOptionsKeyImageDPI"
  13. let KMPDFConvertOptionsKeyImageWithAnnotation = "KMPDFConvertOptionsKeyImageWithAnnotation"
  14. let kDefaultPassword = "666666"
  15. class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
  16. var pdfConverter:CPDFConverter?
  17. var fpConverter:CPDFConverterFP?
  18. var options:CPDFConvertOptions?
  19. var srcPath:String = ""
  20. var desPath:String = ""
  21. var pages:[Int] = []
  22. var pathExtension = ""
  23. var convertQueue = DispatchQueue.main
  24. var semaphore:DispatchSemaphore? = DispatchSemaphore.init(value: 0)
  25. var accessSemaphore:DispatchSemaphore? = DispatchSemaphore.init(value: 1)
  26. var didSuccess:Bool = false
  27. static var instance = FileConverter()
  28. class func shared() -> FileConverter {
  29. return instance
  30. }
  31. func converter(_ inSrcPath: String, inDesPath: String) -> Bool {
  32. if !FileManager.default.fileExists(atPath: inSrcPath) {
  33. return false
  34. }
  35. accessSemaphore?.wait()
  36. self.srcPath = inSrcPath
  37. self.desPath = inDesPath
  38. if FileManager.default.fileExists(atPath: desPath) {
  39. try! FileManager.default.removeItem(atPath: desPath)
  40. }
  41. let attributy = try! FileManager.default.attributesOfItem(atPath: srcPath)
  42. let fileType = attributy[FileAttributeKey.type] as! FileAttributeType
  43. if NSString(string: FileAttributeType.typeSymbolicLink.rawValue).isEqual(to: fileType.rawValue) {
  44. let symbolicLink = try? FileManager.default.destinationOfSymbolicLink(atPath: self.srcPath)
  45. if nil != symbolicLink {
  46. self.srcPath = symbolicLink!
  47. }
  48. }
  49. if FileManager.default.fileExists(atPath: self.desPath) {
  50. try! FileManager.default.removeItem(atPath: self.desPath)
  51. }
  52. self.pathExtension = NSString(string: self.desPath).pathExtension
  53. // let outputPath = NSString(string: self.desPath).deletingPathExtension
  54. // let output = self.desPath
  55. self.convertQueue.async {
  56. let url = URL.init(fileURLWithPath: self.srcPath, isDirectory: false)
  57. let document = PDFDocument(url: url)
  58. if document == nil {
  59. self.accessSemaphore?.signal()
  60. return
  61. }
  62. let pageCount = document!.pageCount
  63. self.pages = []
  64. for i in 1...pageCount {
  65. self.pages.append(i)
  66. }
  67. if NSArray(array: ["jpg", "JPG", "png", "PNG"]).contains(self.pathExtension) {
  68. let cachePath = NSString(string: self.desPath).deletingPathExtension+".zip"
  69. self.pdfConverter = CPDFConverterImg.init(url: url, password: nil)
  70. self.pdfConverter?.delegate = self
  71. self.options = CPDFConvertImgOptions()
  72. self.pdfConverter?.convert(toFilePath: cachePath, pageIndexs: self.pages, options: self.options)
  73. }else {
  74. // if NSArray(array: ["ppt", "PPT", "PPTX", "pptx"]).contains(pathExtension) {
  75. // self.pdfConverter = CPDFConverterPPT.init(url: url, password: kDefaultPassword)
  76. // self.pdfConverter?.delegate = self
  77. // self.options = CPDFConvertPPTOptions()
  78. // self.pdfConverter?.convert(toFilePath: outputPath,
  79. // pageIndexs: [], options: nil)
  80. // }else
  81. // if NSArray(array: ["doc", "DOC", "docx", "DOCX"]).contains(pathExtension) {
  82. // self.pdfConverter = CPDFConverterWord.init(url: url, password: nil)
  83. // self.pdfConverter?.delegate = self
  84. // self.options = CPDFConvertWordOptions()
  85. // self.pdfConverter?.convert(toFilePath: self.self.desPath,
  86. // pageIndexs: pagesArray, options: nil)
  87. // }else
  88. // if NSArray(array: ["xls", "XLS", "xlsx", "XLSX"]).contains(pathExtension) {
  89. // self.pdfConverter = CPDFConverterTable.init(url: url, password: nil)
  90. // self.pdfConverter?.delegate = self
  91. // self.options = CPDFConvertTableOptions()
  92. // self.pdfConverter?.convert(toFilePath: self.self.desPath,
  93. // pageIndexs: pagesArray, options: nil)
  94. // }else if NSArray(array: ["csv", "CSV"]).contains(pathExtension) {
  95. // self.pdfConverter = CPDFConverterCsv.init(url: url, password: nil)
  96. // self.pdfConverter?.delegate = self
  97. // self.options = CPDFConvertCsvOptions()
  98. // self.pdfConverter?.convert(toFilePath: self.self.desPath,
  99. // pageIndexs: pagesArray, options: nil)
  100. // }else {
  101. if self.fpConverter == nil {
  102. self.fpConverter = CPDFConverterFP.init()
  103. self.fpConverter?.setDelegate(self)
  104. }else {
  105. self.fpConverter?.stopConvertsionIfNeed()
  106. }
  107. let needMerge = NSArray(array: ["csv", "CSV"]).contains(self.pathExtension)
  108. // let allInOneSheetKey = String(CPDFConvertOptionsKey.allInOneSheet)
  109. // Task.init {
  110. self.fpConverter?.convertPDF(atPath: self.srcPath,
  111. pdfPassword: nil,
  112. pdfPageIndexs: self.pages,
  113. destDocType: self.pathExtension,
  114. destDocPath: self.desPath,
  115. moreOptions: [
  116. "KMPDFConvertOptionsKeyImageDPI" : "72",
  117. "CPDFConvertOptionsKeyAllInOneSheet":NSNumber(booleanLiteral: needMerge)
  118. ])
  119. // }
  120. // }
  121. }
  122. }
  123. semaphore?.wait()
  124. accessSemaphore?.signal()
  125. return didSuccess
  126. }
  127. /// CPDFConverterDelegate
  128. func converter(_ converter: CPDFConverter!, didStartConvert error: Error!) {
  129. }
  130. func converter(_ converter: CPDFConverter!, didEndConvert error: Error!) {
  131. didSuccess = nil == error
  132. sleep(2)
  133. let cachePath = NSString(string: self.desPath).deletingPathExtension+".zip"
  134. // try? FileManager.default.createDirectory(atPath: self.desPath, withIntermediateDirectories: true)
  135. let zip = ZipArchive.init()
  136. zip.unzipOpenFile(cachePath)
  137. zip.unzipFile(to: self.desPath, overWrite: true)
  138. try? FileManager.default.removeItem(atPath: cachePath)
  139. sleep(1)
  140. if self.pdfConverter?.isConverting == true {
  141. self.pdfConverter?.cancel()
  142. }
  143. self.pdfConverter?.delegate = nil
  144. self.pdfConverter = nil
  145. semaphore?.signal()
  146. }
  147. func converter(_ converter: CPDFConverter!, pageIndex index: UInt, pageCount count: UInt) {
  148. }
  149. /// CPDFConverterFPDelegate
  150. func fppdfConverter(_ converter: Any!, didEndConversion error: Error!) {
  151. didSuccess = nil == error
  152. self.fpConverter?.stopConvertsionIfNeed()
  153. sleep(2)
  154. // self.fpConverter?.stopConvertsionIfNeed()
  155. // self.fpConverter?.setDelegate(nil)
  156. // self.fpConverter = nil
  157. //
  158. semaphore?.signal()
  159. }
  160. }