ConvertUtil.swift 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // ConvertUtil.swift
  3. // Runner
  4. //
  5. //
  6. // Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  7. //
  8. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  9. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  10. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  11. // This notice may not be removed from this file.
  12. //
  13. import Foundation
  14. import ComPDFKit_Conversion
  15. class ConvertUtil : NSObject,CPDFConverterDelegate{
  16. var pdfConvert : CPDFConverter?
  17. var mPath:String?
  18. var result: ((Int, Int, String?) -> Void)?
  19. func getConvertOptions(withArguments arguments: Any?) -> CPDFConvertOptions {
  20. let convertData = arguments as! [String: Any];
  21. let isContainImages = Bool((convertData[params_contain_images] as? String) ?? "true") ?? true
  22. let isContainAnnotation = Bool((convertData[params_contain_annotations] as? String) ?? "true") ?? true;
  23. let filePath = convertData[params_file_path] as! String;
  24. let fileName = (convertData[params_file_name] as? String) ?? "";
  25. let convertType = convertData[params_convert_type] as! String;
  26. let workSheetOptions = (convertData[params_worksheet_options] as? String) ?? "";
  27. let contentOptions = (convertData[params_content_options] as? String) ?? "";
  28. let imageDpi = (convertData[params_image_dpi] as? String) ?? "300";
  29. let imagePageOptions = (convertData[params_image_page_options] as? String) ?? "";
  30. let mergeCSV = Bool((convertData[params_merge_csv] as? String) ?? "false") ?? false;
  31. let htmlPageOptions = convertData[params_html_page_options];
  32. print("convertType:\(convertType)")
  33. print("isContainImages:\(isContainImages)")
  34. print("isContainAnnotations:\(isContainAnnotation)")
  35. switch convertType.uppercased() {
  36. case ConvertType.PPT.rawValue:
  37. let pptOptions = CPDFConvertPPTOptions()
  38. pptOptions.isContainImages = isContainImages
  39. pptOptions.isContainAnnotations = isContainAnnotation
  40. return pptOptions
  41. case ConvertType.WORD.rawValue:
  42. let wordOptions = CPDFConvertWordOptions()
  43. wordOptions.isContainImages = isContainImages
  44. wordOptions.isContainAnnotations = isContainAnnotation
  45. return wordOptions
  46. case ConvertType.EXCEL.rawValue:
  47. let excelOptions = CPDFConvertExcelOptions()
  48. excelOptions.isContainImages = isContainImages
  49. excelOptions.isContainAnnotations = isContainAnnotation
  50. switch workSheetOptions {
  51. case "ForEachTable":
  52. excelOptions.worksheetOptions = CPDFConvertExcelWorksheetOptions.forEachTable
  53. case "ForEachPage":
  54. excelOptions.worksheetOptions = CPDFConvertExcelWorksheetOptions.forEachPage
  55. default :
  56. excelOptions.worksheetOptions = CPDFConvertExcelWorksheetOptions.forTheDocument
  57. }
  58. switch contentOptions {
  59. case "OnlyText":
  60. excelOptions.contentOptions = CPDFConvertExcelContentOptions.onlyText
  61. case "OnlyTable":
  62. excelOptions.contentOptions = CPDFConvertExcelContentOptions.onlyTable
  63. default :
  64. excelOptions.contentOptions = CPDFConvertExcelContentOptions.allContent
  65. }
  66. return excelOptions
  67. case ConvertType.TXT.rawValue:
  68. return CPDFConvertTxtOptions()
  69. case ConvertType.IMAGE.rawValue:
  70. let imageOptions = CPDFConvertImgOptions()
  71. imageOptions.imageDpi = Int32(imageDpi) ?? 300
  72. imageOptions.isContainAnnotations = isContainAnnotation
  73. switch imagePageOptions {
  74. case "JPEG":
  75. imageOptions.type = CPDFConvertImgType.JPEG
  76. case "PNG":
  77. imageOptions.type = CPDFConvertImgType.PNG
  78. default:
  79. imageOptions.type = CPDFConvertImgType.PNG
  80. }
  81. return imageOptions
  82. case ConvertType.CSV.rawValue:
  83. let csvOptions = CPDFConvertCsvOptions()
  84. csvOptions.isMergeCSV = mergeCSV
  85. return csvOptions
  86. case ConvertType.RTF.rawValue:
  87. let rtfOptions = CPDFConvertRtfOptions()
  88. rtfOptions.isContainImages = isContainImages
  89. rtfOptions.isContainAnnotations = isContainAnnotation
  90. return rtfOptions
  91. case ConvertType.HTML.rawValue:
  92. let htmlOptions = CPDFConvertHtmlOptions()
  93. htmlOptions.isContainImages = isContainImages
  94. htmlOptions.isContainAnnotations = isContainAnnotation
  95. return htmlOptions
  96. default:
  97. return CPDFConvertOptions()
  98. }
  99. }
  100. func convert(options :CPDFConvertOptions, filePath:String,fileName : String, convertType : String, onProgress : @escaping (_ progress : Int, _ status : Int, _ outputPath : String?) -> Void) {
  101. result = onProgress
  102. var outPutPath = NSHomeDirectory() + "/Documents/ConversionDemo"
  103. if !FileManager.default.fileExists(atPath: outPutPath) {
  104. try! FileManager.default.createDirectory(atPath: outPutPath, withIntermediateDirectories: true)
  105. }
  106. let encodeString = filePath.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
  107. let fileURL = URL(string:encodeString ?? "")
  108. switch convertType.uppercased() {
  109. case ConvertType.PPT.rawValue:
  110. pdfConvert = CPDFConverterPPT(url: fileURL, password: "")
  111. outPutPath = outPutPath.appending("/converted_\(fileName)_\(self.timeIntervalChangetoTimeStr(timeInterval: NSDate().timeIntervalSince1970, dateFormat:nil)).pptx")
  112. case ConvertType.WORD.rawValue:
  113. pdfConvert = CPDFConverterWord(url: fileURL, password: "")
  114. outPutPath = outPutPath.appending("/converted_\(fileName)_\(self.timeIntervalChangetoTimeStr(timeInterval: NSDate().timeIntervalSinceNow, dateFormat:nil)).docx")
  115. case ConvertType.EXCEL.rawValue:
  116. pdfConvert = CPDFConverterExcel(url: fileURL, password: "")
  117. outPutPath = outPutPath.appending("/converted_\(fileName)_\(self.timeIntervalChangetoTimeStr(timeInterval: NSDate().timeIntervalSinceNow, dateFormat:nil)).xlsx")
  118. case ConvertType.TXT.rawValue:
  119. pdfConvert = CPDFConverterTxt(url: fileURL, password: "")
  120. outPutPath = outPutPath.appending("/converted_\(fileName)_\(self.timeIntervalChangetoTimeStr(timeInterval: NSDate().timeIntervalSinceNow, dateFormat:nil)).txt")
  121. case ConvertType.IMAGE.rawValue:
  122. pdfConvert = CPDFConverterImg(url: fileURL, password: "")
  123. outPutPath = outPutPath.appending("/converted_\(fileName)_\(self.timeIntervalChangetoTimeStr(timeInterval: NSDate().timeIntervalSinceNow, dateFormat:nil)).zip")
  124. case ConvertType.CSV.rawValue:
  125. pdfConvert = CPDFConverterCsv(url: fileURL, password: "")
  126. outPutPath = outPutPath.appending("/converted_\(fileName)_\(self.timeIntervalChangetoTimeStr(timeInterval: NSDate().timeIntervalSinceNow, dateFormat:nil)).zip")
  127. case ConvertType.RTF.rawValue:
  128. pdfConvert = CPDFConverterRtf(url: fileURL, password: "")
  129. outPutPath = outPutPath.appending("/converted_\(fileName)_\(self.timeIntervalChangetoTimeStr(timeInterval: NSDate().timeIntervalSinceNow, dateFormat:nil)).rtf")
  130. case ConvertType.HTML.rawValue:
  131. pdfConvert = CPDFConverterHtml(url: fileURL, password: "")
  132. outPutPath = outPutPath.appending("/converted_\(fileName)_\(self.timeIntervalChangetoTimeStr(timeInterval: NSDate().timeIntervalSinceNow, dateFormat:nil)).zip")
  133. default:
  134. pdfConvert = CPDFConverter()
  135. }
  136. pdfConvert?.delegate = self
  137. mPath = outPutPath;
  138. pdfConvert?.convert(toFilePath: outPutPath, pageIndexs: nil, options: nil)
  139. }
  140. func timeIntervalChangetoTimeStr(timeInterval:TimeInterval, dateFormat:String?) -> String {
  141. let date:NSDate = NSDate.init(timeIntervalSince1970: timeInterval/1000)
  142. let formatter = DateFormatter.init()
  143. if dateFormat == nil {
  144. formatter.dateFormat = "yyyy-MM-dd_HH:mm:ss"
  145. }else{
  146. formatter.dateFormat = dateFormat
  147. }
  148. return formatter.string(from: date as Date)
  149. }
  150. @objc func converter(_ converter: CPDFConverter!, didStartConvert error: Error!) {
  151. result?(0, task_start, mPath)
  152. }
  153. @objc func converter(_ converter: CPDFConverter!, pageIndex index: UInt, pageCount count: UInt) {
  154. let progress = Double(index) / Double(count)
  155. result?(Int(progress*100), task_converting, mPath)
  156. }
  157. @objc func converter(_ converter: CPDFConverter!, didEndConvert error: Error!) {
  158. result?(100, task_success, mPath)
  159. }
  160. }