|
@@ -40,6 +40,10 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
|
|
|
}
|
|
|
|
|
|
func converter(_ inSrcPath: String, inDesPath: String) -> Bool {
|
|
|
+ return converter(inSrcPath, inDesPath: inDesPath, params: nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ func converter(_ inSrcPath: String, inDesPath: String, params:NSDictionary?) -> Bool {
|
|
|
if !FileManager.default.fileExists(atPath: inSrcPath) {
|
|
|
return false
|
|
|
}
|
|
@@ -68,6 +72,21 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
|
|
|
self.pathExtension = NSString(string: self.desPath).pathExtension
|
|
|
// let outputPath = NSString(string: self.desPath).deletingPathExtension
|
|
|
// let output = self.desPath
|
|
|
+ let password = params?.value(forKey: "password")
|
|
|
+ let tParams = params as? NSDictionary
|
|
|
+ let useOldLibValue = params?.value(forKey: "useOldLib")
|
|
|
+
|
|
|
+ var useOldLib = false;
|
|
|
+ if (nil != useOldLibValue) {
|
|
|
+ if ((useOldLibValue as? String) != nil) {
|
|
|
+ useOldLib = NSArray(array: ["TRUE", "true", "1"]).contains((useOldLibValue as! String))
|
|
|
+ }else if ((useOldLibValue as? NSNumber) != nil) {
|
|
|
+ useOldLib = (useOldLibValue as? NSNumber)!.boolValue || (useOldLibValue as? NSNumber)!.intValue == 1
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ NSLog("%@ - %@", useOldLib ? "老库" : "新库", params ?? "")
|
|
|
+
|
|
|
self.convertQueue.async {
|
|
|
let url = URL.init(fileURLWithPath: self.srcPath, isDirectory: false)
|
|
|
let document = PDFDocument(url: url)
|
|
@@ -82,66 +101,128 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
|
|
|
self.pages.append(i)
|
|
|
}
|
|
|
|
|
|
- if NSArray(array: ["jpg", "JPG", "png", "PNG"]).contains(self.pathExtension) {
|
|
|
- let cachePath = NSString(string: self.desPath).deletingPathExtension+".zip"
|
|
|
- self.pdfConverter = CPDFConverterImg.init(url: url, password: nil)
|
|
|
- self.pdfConverter?.delegate = self
|
|
|
- self.options = CPDFConvertImgOptions()
|
|
|
- self.pdfConverter?.convert(toFilePath: cachePath, pageIndexs: self.pages, options: self.options)
|
|
|
- }else {
|
|
|
-
|
|
|
- // if NSArray(array: ["ppt", "PPT", "PPTX", "pptx"]).contains(pathExtension) {
|
|
|
- // self.pdfConverter = CPDFConverterPPT.init(url: url, password: kDefaultPassword)
|
|
|
- // self.pdfConverter?.delegate = self
|
|
|
- // self.options = CPDFConvertPPTOptions()
|
|
|
- // self.pdfConverter?.convert(toFilePath: outputPath,
|
|
|
- // pageIndexs: [], options: nil)
|
|
|
- // }else
|
|
|
- // if NSArray(array: ["doc", "DOC", "docx", "DOCX"]).contains(pathExtension) {
|
|
|
- // self.pdfConverter = CPDFConverterWord.init(url: url, password: nil)
|
|
|
- // self.pdfConverter?.delegate = self
|
|
|
- // self.options = CPDFConvertWordOptions()
|
|
|
- // self.pdfConverter?.convert(toFilePath: self.self.desPath,
|
|
|
- // pageIndexs: pagesArray, options: nil)
|
|
|
- // }else
|
|
|
- // if NSArray(array: ["xls", "XLS", "xlsx", "XLSX"]).contains(pathExtension) {
|
|
|
- // self.pdfConverter = CPDFConverterTable.init(url: url, password: nil)
|
|
|
- // self.pdfConverter?.delegate = self
|
|
|
- // self.options = CPDFConvertTableOptions()
|
|
|
- // self.pdfConverter?.convert(toFilePath: self.self.desPath,
|
|
|
- // pageIndexs: pagesArray, options: nil)
|
|
|
- // }else if NSArray(array: ["csv", "CSV"]).contains(pathExtension) {
|
|
|
- // self.pdfConverter = CPDFConverterCsv.init(url: url, password: nil)
|
|
|
- // self.pdfConverter?.delegate = self
|
|
|
- // self.options = CPDFConvertCsvOptions()
|
|
|
- // self.pdfConverter?.convert(toFilePath: self.self.desPath,
|
|
|
- // pageIndexs: pagesArray, options: nil)
|
|
|
- // }else {
|
|
|
- if self.fpConverter == nil {
|
|
|
- self.fpConverter = CPDFConverterFP.init()
|
|
|
- self.fpConverter?.setDelegate(self)
|
|
|
- }else {
|
|
|
- self.fpConverter?.stopConvertsionIfNeed()
|
|
|
+ if !useOldLib && NSArray(array: ["jpg", "JPG", "png", "PNG"]).contains(self.pathExtension) {
|
|
|
+ autoreleasepool {
|
|
|
+ let cachePath = NSString(string: self.desPath).deletingPathExtension+".zip"
|
|
|
+ self.pdfConverter = CPDFConverterImg.init(url: url, password: nil)
|
|
|
+ if (nil == self.pdfConverter && nil != password) {
|
|
|
+ self.pdfConverter = CPDFConverterImg.init(url: url, password: password as? String)
|
|
|
+ }
|
|
|
+ self.pdfConverter?.delegate = self
|
|
|
+ self.options = CPDFConvertImgOptions()
|
|
|
+ if (NSArray(array: ["jpg", "JPG"]).contains(self.pathExtension)) {
|
|
|
+ (self.options as! CPDFConvertImgOptions).type = .JPEG
|
|
|
+ }else {
|
|
|
+ (self.options as! CPDFConvertImgOptions).type = .PNG
|
|
|
+ }
|
|
|
+ self.pdfConverter?.convert(toFilePath: cachePath, pageIndexs: self.pages, options: self.options)
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
- let needMerge = NSArray(array: ["csv", "CSV"]).contains(self.pathExtension)
|
|
|
- // let allInOneSheetKey = String(CPDFConvertOptionsKey.allInOneSheet)
|
|
|
-
|
|
|
- // Task.init {
|
|
|
+ }else if !useOldLib && NSArray(array: ["ppt", "PPT", "PPTX", "pptx"]).contains(self.pathExtension) {
|
|
|
+ autoreleasepool {
|
|
|
+ self.pdfConverter = CPDFConverterPPT.init(url: url, password: nil)
|
|
|
+ if (nil == self.pdfConverter && nil != password) {
|
|
|
+ self.pdfConverter = CPDFConverterPPT.init(url: url, password: password as? String)
|
|
|
+ }
|
|
|
+ self.pdfConverter?.delegate = self
|
|
|
+ self.options = CPDFConvertPPTOptions()
|
|
|
+ self.pdfConverter?.convert(toFilePath: self.desPath,
|
|
|
+ pageIndexs: self.pages, options: self.options)
|
|
|
+ }
|
|
|
+ }else if !useOldLib && NSArray(array: ["doc", "DOC", "docx", "DOCX"]).contains(self.pathExtension) {
|
|
|
+ autoreleasepool {
|
|
|
+ self.pdfConverter = CPDFConverterWord.init(url: url, password: nil)
|
|
|
+ if (nil == self.pdfConverter && nil != password) {
|
|
|
+ self.pdfConverter = CPDFConverterWord.init(url: url, password: password as? String)
|
|
|
+ }
|
|
|
+ self.pdfConverter?.delegate = self
|
|
|
+ self.options = CPDFConvertWordOptions()
|
|
|
+ self.pdfConverter?.convert(toFilePath: self.desPath,
|
|
|
+ pageIndexs: self.pages, options: self.options)
|
|
|
+ }
|
|
|
+ }else if !useOldLib && NSArray(array: ["xls", "XLS", "xlsx", "XLSX"]).contains(self.pathExtension) {
|
|
|
+ autoreleasepool {
|
|
|
+ self.pdfConverter = CPDFConverterExcel.init(url: url, password: nil)
|
|
|
+ if (nil == self.pdfConverter && nil != password) {
|
|
|
+ self.pdfConverter = CPDFConverterExcel.init(url: url, password: password as? String)
|
|
|
+ }
|
|
|
+ self.pdfConverter?.delegate = self
|
|
|
+ self.options = CPDFConvertExcelOptions()
|
|
|
+ self.pdfConverter?.convert(toFilePath: self.desPath,
|
|
|
+ pageIndexs: self.pages, options: self.options)
|
|
|
+ }
|
|
|
+ }else if !useOldLib && NSArray(array: ["csv", "CSV"]).contains(self.pathExtension) {
|
|
|
+ autoreleasepool {
|
|
|
+ self.pdfConverter = CPDFConverterCsv.init(url: url, password: nil)
|
|
|
+ if (nil == self.pdfConverter && nil != password) {
|
|
|
+ self.pdfConverter = CPDFConverterCsv.init(url: url, password: password as? String)
|
|
|
+ }
|
|
|
+ self.pdfConverter?.delegate = self
|
|
|
+ self.options = CPDFConvertCsvOptions()
|
|
|
+ self.pdfConverter?.convert(toFilePath: self.desPath,
|
|
|
+ pageIndexs: self.pages, options: self.options)
|
|
|
+ }
|
|
|
+ }else if !useOldLib && NSArray(array: ["html", "HTML"]).contains(self.pathExtension) {
|
|
|
+ autoreleasepool {
|
|
|
+ let cachePath = NSString(string: self.desPath).deletingPathExtension+".zip"
|
|
|
+ self.pdfConverter = CPDFConverterHtml.init(url: url, password: nil)
|
|
|
+ if (nil == self.pdfConverter && nil != password) {
|
|
|
+ self.pdfConverter = CPDFConverterHtml.init(url: url, password: password as? String)
|
|
|
+ }
|
|
|
+ self.pdfConverter?.delegate = self
|
|
|
+ self.options = CPDFConvertHtmlOptions()
|
|
|
+ if ((tParams?.value(forKey: "paneOptions") as? NSNumber) != nil) {
|
|
|
+ (self.options as! CPDFConvertHtmlOptions).paneOptions = CPDFConvertHtmlPageAndNavigationPaneOptions(rawValue: (tParams?.value(forKey: "paneOptions") as! NSNumber).intValue)!
|
|
|
+ }
|
|
|
+ self.pdfConverter?.convert(toFilePath: cachePath,
|
|
|
+ pageIndexs: self.pages, options: self.options)
|
|
|
+ }
|
|
|
+ }else if !useOldLib && NSArray(array: ["rtf", "RTF"]).contains(self.pathExtension) {
|
|
|
+ autoreleasepool {
|
|
|
+ self.pdfConverter = CPDFConverterRtf.init(url: url, password: nil)
|
|
|
+ if (nil == self.pdfConverter && nil != password) {
|
|
|
+ self.pdfConverter = CPDFConverterRtf.init(url: url, password: password as? String)
|
|
|
+ }
|
|
|
+ self.pdfConverter?.delegate = self
|
|
|
+ self.options = CPDFConvertRtfOptions()
|
|
|
+ self.pdfConverter?.convert(toFilePath: self.desPath,
|
|
|
+ pageIndexs: self.pages, options: self.options)
|
|
|
+ }
|
|
|
+ }else if !useOldLib && NSArray(array: ["txt", "TXT"]).contains(self.pathExtension) {
|
|
|
+ autoreleasepool {
|
|
|
+ self.pdfConverter = CPDFConverterTxt.init(url: url, password: nil)
|
|
|
+ if (nil == self.pdfConverter && nil != password) {
|
|
|
+ self.pdfConverter = CPDFConverterTxt.init(url: url, password: password as? String)
|
|
|
+ }
|
|
|
+ self.pdfConverter?.delegate = self
|
|
|
+ self.options = CPDFConvertTxtOptions()
|
|
|
+ self.pdfConverter?.convert(toFilePath: self.desPath,
|
|
|
+ pageIndexs: self.pages, options: self.options)
|
|
|
+ }
|
|
|
+ }else {
|
|
|
autoreleasepool {
|
|
|
+ if self.fpConverter == nil {
|
|
|
+ self.fpConverter = CPDFConverterFP.init()
|
|
|
+ self.fpConverter?.setDelegate(self)
|
|
|
+ }else {
|
|
|
+ self.fpConverter?.stopConvertsionIfNeed()
|
|
|
+ }
|
|
|
+
|
|
|
+ var needMerge = NSArray(array: ["csv", "CSV"]).contains(self.pathExtension)
|
|
|
+ let dpi = tParams?.value(forKey: "KMPDFConvertOptionsKeyImageDPI")
|
|
|
+ if (needMerge && tParams?.value(forKey: "CPDFConvertOptionsKeyAllInOneSheet") != nil) {
|
|
|
+ needMerge = (tParams?.value(forKey: "CPDFConvertOptionsKeyAllInOneSheet") as! NSNumber).boolValue
|
|
|
+ }
|
|
|
+
|
|
|
self.fpConverter?.convertPDF(atPath: self.srcPath,
|
|
|
- pdfPassword: nil,
|
|
|
+ pdfPassword: password as? String,
|
|
|
pdfPageIndexs: self.pages,
|
|
|
destDocType: self.pathExtension,
|
|
|
destDocPath: self.desPath,
|
|
|
moreOptions: [
|
|
|
- "KMPDFConvertOptionsKeyImageDPI" : "72",
|
|
|
+ "KMPDFConvertOptionsKeyImageDPI" : (dpi as? String) ?? "72",
|
|
|
"CPDFConvertOptionsKeyAllInOneSheet":NSNumber(booleanLiteral: needMerge)
|
|
|
])
|
|
|
}
|
|
|
- // }
|
|
|
- // }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -162,15 +243,16 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
|
|
|
autoreleasepool {
|
|
|
sleep(2)
|
|
|
|
|
|
- let cachePath = NSString(string: self.desPath).deletingPathExtension+".zip"
|
|
|
- // try? FileManager.default.createDirectory(atPath: self.desPath, withIntermediateDirectories: true)
|
|
|
-
|
|
|
- let zip = ZipArchive.init()
|
|
|
-
|
|
|
- zip.unzipOpenFile(cachePath)
|
|
|
- zip.unzipFile(to: self.desPath, overWrite: true)
|
|
|
-
|
|
|
- try? FileManager.default.removeItem(atPath: cachePath)
|
|
|
+ if NSArray(array: ["jpg", "JPG", "png", "PNG", "HTML", "html"]).contains(self.pathExtension) {
|
|
|
+ let cachePath = NSString(string: self.desPath).deletingPathExtension+".zip"
|
|
|
+
|
|
|
+ let zip = ZipArchive.init()
|
|
|
+
|
|
|
+ zip.unzipOpenFile(cachePath)
|
|
|
+ zip.unzipFile(to: self.desPath, overWrite: true)
|
|
|
+
|
|
|
+ try? FileManager.default.removeItem(atPath: cachePath)
|
|
|
+ }
|
|
|
|
|
|
sleep(1)
|
|
|
|
|
@@ -179,6 +261,7 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
|
|
|
}
|
|
|
self.pdfConverter?.delegate = nil
|
|
|
self.pdfConverter = nil
|
|
|
+
|
|
|
semaphore?.signal()
|
|
|
}
|
|
|
}
|