|
@@ -29,23 +29,24 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
|
|
|
var pages:[Int] = []
|
|
|
var pathExtension = ""
|
|
|
var convertQueue = DispatchQueue.main
|
|
|
+ var operateQueue = OperationQueue()
|
|
|
|
|
|
var semaphore:DispatchSemaphore? = DispatchSemaphore.init(value: 0)
|
|
|
var accessSemaphore:DispatchSemaphore? = DispatchSemaphore.init(value: 1)
|
|
|
- var didSuccess:Bool = false
|
|
|
+ var didSuccess:Int = 1
|
|
|
|
|
|
static var instance = FileConverter()
|
|
|
class func shared() -> FileConverter {
|
|
|
return instance
|
|
|
}
|
|
|
|
|
|
- func converter(_ inSrcPath: String, inDesPath: String) -> Bool {
|
|
|
+ func converter(_ inSrcPath: String, inDesPath: String) -> Int {
|
|
|
return converter(inSrcPath, inDesPath: inDesPath, params: nil)
|
|
|
}
|
|
|
|
|
|
- func converter(_ inSrcPath: String, inDesPath: String, params:NSDictionary?) -> Bool {
|
|
|
+ func converter(_ inSrcPath: String, inDesPath: String, params:NSDictionary?) -> Int {
|
|
|
if !FileManager.default.fileExists(atPath: inSrcPath) {
|
|
|
- return false
|
|
|
+ return -1
|
|
|
}
|
|
|
|
|
|
accessSemaphore?.wait()
|
|
@@ -56,6 +57,10 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
|
|
|
if FileManager.default.fileExists(atPath: desPath) {
|
|
|
try! FileManager.default.removeItem(atPath: desPath)
|
|
|
}
|
|
|
+ let directory = NSString(string: desPath).deletingLastPathComponent
|
|
|
+ if !FileManager.default.fileExists(atPath: directory) {
|
|
|
+ try! FileManager.default.createDirectory(atPath: directory, withIntermediateDirectories: true)
|
|
|
+ }
|
|
|
let attributy = try! FileManager.default.attributesOfItem(atPath: srcPath)
|
|
|
let fileType = attributy[FileAttributeKey.type] as! FileAttributeType
|
|
|
if NSString(string: FileAttributeType.typeSymbolicLink.rawValue).isEqual(to: fileType.rawValue) {
|
|
@@ -73,7 +78,7 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
|
|
|
// let outputPath = NSString(string: self.desPath).deletingPathExtension
|
|
|
// let output = self.desPath
|
|
|
let password = params?.value(forKey: "password")
|
|
|
- let tParams = params as? NSDictionary
|
|
|
+ let tParams = params ?? [:]
|
|
|
let useOldLibValue = params?.value(forKey: "useOldLib")
|
|
|
|
|
|
var useOldLib = false;
|
|
@@ -92,6 +97,7 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
|
|
|
let document = PDFDocument(url: url)
|
|
|
if document == nil {
|
|
|
self.accessSemaphore?.signal()
|
|
|
+ self.didSuccess = -2;
|
|
|
return
|
|
|
}
|
|
|
let pageCount = document!.pageCount
|
|
@@ -102,126 +108,144 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
|
|
|
}
|
|
|
|
|
|
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.operateQueue.addOperation {
|
|
|
+ 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)
|
|
|
}
|
|
|
- self.pdfConverter?.convert(toFilePath: cachePath, pageIndexs: self.pages, options: self.options)
|
|
|
}
|
|
|
}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.operateQueue.addOperation {
|
|
|
+ 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)
|
|
|
}
|
|
|
- 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.operateQueue.addOperation {
|
|
|
+ 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)
|
|
|
}
|
|
|
- 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.operateQueue.addOperation {
|
|
|
+ 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)
|
|
|
}
|
|
|
- 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.operateQueue.addOperation {
|
|
|
+ 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)
|
|
|
}
|
|
|
- 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.operateQueue.addOperation {
|
|
|
+ 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)
|
|
|
}
|
|
|
- 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)
|
|
|
+ self.operateQueue.addOperation {
|
|
|
+ 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.operateQueue.addOperation {
|
|
|
+ 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)
|
|
|
}
|
|
|
- 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: password as? String,
|
|
|
- pdfPageIndexs: self.pages,
|
|
|
- destDocType: self.pathExtension,
|
|
|
- destDocPath: self.desPath,
|
|
|
- moreOptions: [
|
|
|
- "KMPDFConvertOptionsKeyImageDPI" : (dpi as? String) ?? "72",
|
|
|
- "CPDFConvertOptionsKeyAllInOneSheet":NSNumber(booleanLiteral: needMerge)
|
|
|
- ])
|
|
|
+ self.operateQueue.addOperation {
|
|
|
+ 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: password as? String,
|
|
|
+ pdfPageIndexs: self.pages,
|
|
|
+ destDocType: self.pathExtension,
|
|
|
+ destDocPath: self.desPath,
|
|
|
+ moreOptions: [
|
|
|
+ "KMPDFConvertOptionsKeyImageDPI" : (dpi as? String) ?? "72",
|
|
|
+ "CPDFConvertOptionsKeyAllInOneSheet":NSNumber(booleanLiteral: needMerge)
|
|
|
+ ])
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -234,11 +258,12 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
|
|
|
|
|
|
/// CPDFConverterDelegate
|
|
|
func converter(_ converter: CPDFConverter!, didStartConvert error: Error!) {
|
|
|
+ didSuccess = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
func converter(_ converter: CPDFConverter!, didEndConvert error: Error!) {
|
|
|
- didSuccess = nil == error
|
|
|
+ didSuccess = nil == error ? 1 : 0
|
|
|
|
|
|
autoreleasepool {
|
|
|
sleep(2)
|
|
@@ -273,7 +298,7 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
|
|
|
/// CPDFConverterFPDelegate
|
|
|
func fppdfConverter(_ converter: Any!, didEndConversion error: Error!) {
|
|
|
autoreleasepool {
|
|
|
- didSuccess = nil == error
|
|
|
+ didSuccess = nil == error ? 1 : 0
|
|
|
self.fpConverter?.stopConvertsionIfNeed()
|
|
|
|
|
|
sleep(2)
|