KMConvertPDFManagerOC.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //
  2. // KMConvertPDFManagerOC.m
  3. // PDF Master
  4. //
  5. // Created by wanjun on 2023/3/8.
  6. //
  7. #import "KMConvertPDFManagerOC.h"
  8. @implementation KMConvertPDFManagerOC
  9. #pragma mark - Public
  10. + (NSArray *)supportFileType
  11. {
  12. NSMutableArray *supportArray = [NSMutableArray array];
  13. [supportArray addObjectsFromArray:[KMConvertPDFManagerOC supportImages]];
  14. if ([KMConvertPDFManagerOC isSupportConvertPages]) {
  15. [supportArray addObjectsFromArray:[KMConvertPDFManagerOC supportPages]];
  16. } else if ([KMConvertPDFManagerOC isSupportConvertWord]){
  17. [supportArray addObjectsFromArray:[KMConvertPDFManagerOC supportWord]];
  18. }
  19. if ([KMConvertPDFManagerOC isSupportConvertKeynote]) {
  20. [supportArray addObjectsFromArray:[KMConvertPDFManagerOC supportKeynote]];
  21. } else if ([KMConvertPDFManagerOC isSupportConvertPPTX]){
  22. [supportArray addObjectsFromArray:[KMConvertPDFManagerOC supportPPTX]];
  23. }
  24. if ([KMConvertPDFManagerOC isSupportConvertNumber]) {
  25. [supportArray addObjectsFromArray:[KMConvertPDFManagerOC supportNumber]];
  26. } else if ([KMConvertPDFManagerOC isSupportConvertExcel]){
  27. /**
  28. 经测试(2011版 Excel)转档成功以后地址会存在问题,暂时建议不要用
  29. **/
  30. [supportArray addObjectsFromArray:[KMConvertPDFManagerOC supportExcel]];
  31. }
  32. return supportArray;
  33. }
  34. + (void)convertFile:(NSString *)filePath savePath:(NSString *)savePath completionHandler:(void (^)(BOOL success,NSDictionary *errorDic))complet
  35. {
  36. NSString *tSavePath = savePath;
  37. if ([[tSavePath substringToIndex:1] isEqualToString:@"/"]) {
  38. tSavePath = [tSavePath substringFromIndex:1];
  39. }
  40. tSavePath = [tSavePath stringByReplacingOccurrencesOfString:@"/" withString:@":"];
  41. NSString *tFilePath = filePath;
  42. if ([[tFilePath substringToIndex:1] isEqualToString:@"/"]) {
  43. tFilePath = [tFilePath substringFromIndex:1];
  44. }
  45. tFilePath = [tFilePath stringByReplacingOccurrencesOfString:@"/" withString:@":"];
  46. NSString * extension = [filePath.pathExtension lowercaseString];
  47. if ([[KMConvertPDFManagerOC supportPages] containsObject:extension]) {
  48. if ([[KMConvertPDFManagerOC supportWord] containsObject:extension]){
  49. [KMConvertPDFManagerOC convertWordPath:tFilePath savePath:tSavePath completionHandler:^(BOOL success,NSDictionary *errorDic){
  50. complet(success,errorDic);
  51. }];
  52. } else {
  53. [KMConvertPDFManagerOC convertApplicationName:@"Pages" filePath:tFilePath savePath:tSavePath completionHandler:^(BOOL success, NSDictionary *errorDic) {
  54. complet(success,errorDic);
  55. }];
  56. }
  57. } else if ([[KMConvertPDFManagerOC supportKeynote] containsObject:extension]){
  58. if ([[KMConvertPDFManagerOC supportPPTX] containsObject:extension]){
  59. [KMConvertPDFManagerOC convertPPTXPath:tFilePath savePath:tSavePath completionHandler:^(BOOL success,NSDictionary *errorDic){
  60. complet(success,errorDic);
  61. }];
  62. } else {
  63. [KMConvertPDFManagerOC convertApplicationName:@"Keynote" filePath:tFilePath savePath:tSavePath completionHandler:^(BOOL success, NSDictionary *errorDic) {
  64. complet(success,errorDic);
  65. }];
  66. }
  67. } else if ([[KMConvertPDFManagerOC supportNumber] containsObject:extension]){
  68. if ([[KMConvertPDFManagerOC supportExcel] containsObject:extension]){
  69. [KMConvertPDFManagerOC convertExcelPath:tFilePath savePath:tSavePath completionHandler:^(BOOL success,NSDictionary *errorDic){
  70. complet(success,errorDic);
  71. }];
  72. } else {
  73. [KMConvertPDFManagerOC convertApplicationName:@"Numbers" filePath:tFilePath savePath:tSavePath completionHandler:^(BOOL success, NSDictionary *errorDic) {
  74. complet(success,errorDic);
  75. }];
  76. }
  77. } else if ([[KMConvertPDFManagerOC supportImages] containsObject:extension]){
  78. [KMConvertPDFManagerOC convertImagePath:filePath savePath:savePath completionHandler:^(BOOL success, NSDictionary *errorDic) {
  79. complet(success,errorDic);
  80. }];
  81. } else {
  82. NSDictionary * dic = nil;
  83. complet(NO,dic);
  84. }
  85. }
  86. + (NSArray *)supportImages
  87. {
  88. return [NSArray arrayWithObjects:@"jpg",@"cur",@"bmp",@"jpeg",@"gif",@"png",@"tiff",@"tif",/*@"pic",*/@"ico",@"icns",@"tga",@"psd",@"eps",@"hdr",@"jp2",@"jpc",@"pict",@"sgi", nil];
  89. }
  90. #pragma mark - convert Word
  91. + (BOOL)isSupportConvertWord
  92. {
  93. NSString * fullPath = [[NSWorkspace sharedWorkspace] fullPathForApplication:@"Microsoft Word"];
  94. if (fullPath) {
  95. return YES;
  96. }
  97. return NO;
  98. }
  99. + (NSArray *)supportWord
  100. {
  101. return [NSArray arrayWithObjects:@"docx",@"doc",nil];
  102. }
  103. + (void)convertWordPath:(NSString *)filePath savePath:(NSString *)savePath completionHandler:(void (^)(BOOL success,NSDictionary *errorDic))complet
  104. {
  105. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  106. NSString * convertString = [NSString stringWithFormat:@"set filePath to\"%@\"\n",filePath];
  107. convertString = [convertString stringByAppendingFormat:@"set savePath to \"%@\"\n",savePath];
  108. convertString = [convertString stringByAppendingFormat:@"tell application \"Microsoft Word\"\n"];
  109. convertString = [convertString stringByAppendingFormat:@"set isRun to running\n"];
  110. convertString = [convertString stringByAppendingFormat:@"open file filePath\n"];
  111. convertString = [convertString stringByAppendingFormat:@"save as active document file format format PDF file name savePath\n"];
  112. convertString = [convertString stringByAppendingFormat:@"close active document\n"];
  113. convertString = [convertString stringByAppendingFormat:@"if not isRun then quit\n"];
  114. convertString = [convertString stringByAppendingFormat:@"end tell\n"];
  115. NSDictionary * dic = nil;
  116. NSAppleScript *docScript = [[NSAppleScript alloc] initWithSource:convertString];
  117. [docScript executeAndReturnError:&dic];
  118. dispatch_async(dispatch_get_main_queue(), ^{
  119. complet(YES,dic);
  120. });
  121. });
  122. }
  123. #pragma mark - convert Numbers
  124. + (BOOL)isSupportConvertNumber
  125. {
  126. NSString * fullPath = [[NSWorkspace sharedWorkspace] fullPathForApplication:@"Numbers"];
  127. if (fullPath) {
  128. return YES;
  129. }
  130. return NO;
  131. }
  132. + (NSArray *)supportNumber
  133. {
  134. return [NSArray arrayWithObjects:@"xls",@"xlsx",@"numbers",@"csv",nil];
  135. }
  136. + (void)convertApplicationName:(NSString *)appName filePath:(NSString *)filePath savePath:(NSString *)savePath completionHandler:(void (^)(BOOL success,NSDictionary *errorDic))complet
  137. {
  138. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  139. NSString * convertString = [NSString stringWithFormat:@"tell application \"%@\"\n",appName];
  140. convertString = [convertString stringByAppendingFormat:@"set isRun to running\n"];
  141. convertString = [convertString stringByAppendingFormat:@"set in_file to \"%@\"\n",filePath];
  142. convertString = [convertString stringByAppendingFormat:@"set out_file to \"%@\"\n",savePath];
  143. convertString = [convertString stringByAppendingFormat:@"set mydoc to open file in_file\n"];
  144. convertString = [convertString stringByAppendingFormat:@"export mydoc to file out_file as PDF\n"];
  145. convertString = [convertString stringByAppendingFormat:@"close mydoc saving no\n"];
  146. convertString = [convertString stringByAppendingFormat:@"if not isRun then quit\n"];
  147. convertString = [convertString stringByAppendingFormat:@"end tell"];
  148. NSDictionary * dic = nil;
  149. NSAppleScript *docScript = [[NSAppleScript alloc] initWithSource:convertString];
  150. [docScript executeAndReturnError:&dic];
  151. dispatch_async(dispatch_get_main_queue(), ^{
  152. complet(YES,dic);
  153. });
  154. });
  155. }
  156. #pragma mark - convert Keynote
  157. + (BOOL)isSupportConvertKeynote
  158. {
  159. NSString * fullPath = [[NSWorkspace sharedWorkspace] fullPathForApplication:@"Keynote"];
  160. if (fullPath) {
  161. return YES;
  162. }
  163. return NO;
  164. }
  165. + (NSArray *)supportKeynote
  166. {
  167. return [NSArray arrayWithObjects:@"ppt",@"pptx",@"key",nil];
  168. }
  169. #pragma mark - convert Pages
  170. + (BOOL)isSupportConvertPages
  171. {
  172. NSString * fullPath = [[NSWorkspace sharedWorkspace] fullPathForApplication:@"Pages"];
  173. if (fullPath) {
  174. return YES;
  175. }
  176. return NO;
  177. }
  178. + (NSArray *)supportPages
  179. {
  180. return [NSArray arrayWithObjects:@"pages",@"docx",@"doc",@"txt",@"rtf",nil];
  181. }
  182. #pragma mark - convert PPTX
  183. + (BOOL)isSupportConvertPPTX
  184. {
  185. NSString * fullPath = [[NSWorkspace sharedWorkspace] fullPathForApplication:@"Microsoft PowerPoint"];
  186. if (fullPath) {
  187. return YES;
  188. }
  189. return NO;
  190. }
  191. + (NSArray *)supportPPTX
  192. {
  193. return [NSArray arrayWithObjects:@"ppt",@"pptx",nil];
  194. }
  195. + (void)convertPPTXPath:(NSString *)filePath savePath:(NSString *)savePath completionHandler:(void (^)(BOOL success,NSDictionary *errorDic))complet
  196. {
  197. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  198. NSString * convertString = [NSString stringWithFormat:@"tell application \"Microsoft PowerPoint\"\n"];
  199. convertString = [convertString stringByAppendingFormat:@"set isRun to running\n"];
  200. convertString = [convertString stringByAppendingFormat:@"set savePath to \"%@\"\n",savePath];
  201. convertString = [convertString stringByAppendingFormat:@"set filePath to \"%@\"\n",filePath];
  202. convertString = [convertString stringByAppendingFormat:@"open file filePath\n"];
  203. convertString = [convertString stringByAppendingFormat:@"save active presentation in savePath as save as PDF\n"];
  204. convertString = [convertString stringByAppendingFormat:@"if not isRun then quit\n"];
  205. convertString = [convertString stringByAppendingFormat:@"close active presentation\n"];
  206. convertString = [convertString stringByAppendingFormat:@"end tell\n"];
  207. NSDictionary * dic = nil;
  208. NSAppleScript *docScript = [[NSAppleScript alloc] initWithSource:convertString];
  209. [docScript executeAndReturnError:&dic];
  210. dispatch_async(dispatch_get_main_queue(), ^{
  211. complet(YES,dic);
  212. });
  213. });
  214. }
  215. #pragma mark - convert Excel
  216. + (BOOL)isSupportConvertExcel
  217. {
  218. NSString * fullPath = [[NSWorkspace sharedWorkspace] fullPathForApplication:@"Microsoft Excel"];
  219. if (fullPath) {
  220. return YES;
  221. }
  222. return NO;
  223. }
  224. + (NSArray *)supportExcel
  225. {
  226. return [NSArray arrayWithObjects:@"xlsx",@"xls",nil];
  227. }
  228. + (void)convertExcelPath:(NSString *)filePath savePath:(NSString *)savePath completionHandler:(void (^)(BOOL success,NSDictionary *errorDic))complet
  229. {
  230. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  231. NSString * convertString = [NSString stringWithFormat:@"set filePath to\"%@\"\n",filePath];
  232. convertString = [convertString stringByAppendingFormat:@"set savePath to \"%@\"\n",savePath];
  233. convertString = [convertString stringByAppendingFormat:@"set tFile to (POSIX path of filePath) as POSIX file\n"];
  234. convertString = [convertString stringByAppendingFormat:@"tell application \"Microsoft Excel\"\n"];
  235. convertString = [convertString stringByAppendingFormat:@"set isRun to running\n"];
  236. convertString = [convertString stringByAppendingFormat:@"set wkbk1 to open workbook workbook file name tFile\n"];
  237. convertString = [convertString stringByAppendingFormat:@"alias savePath\n"];
  238. convertString = [convertString stringByAppendingFormat:@"save workbook as wkbk1 filename savePath file format PDF file format with overwrite\n"];
  239. convertString = [convertString stringByAppendingFormat:@"close wkbk1 saving no\n"];
  240. convertString = [convertString stringByAppendingFormat:@"if not isRun then quit\n"];
  241. convertString = [convertString stringByAppendingFormat:@"end tell\n"];
  242. NSDictionary * dic = nil;
  243. NSAppleScript *docScript = [[NSAppleScript alloc] initWithSource:convertString];
  244. [docScript executeAndReturnError:&dic];
  245. dispatch_async(dispatch_get_main_queue(), ^{
  246. complet(YES,dic);
  247. });
  248. });
  249. }
  250. #pragma mark - convert Image
  251. + (void)convertImagePath:(NSString *)imgaePath savePath:(NSString *)savePath completionHandler:(void (^)(BOOL success,NSDictionary *errorDic))complet
  252. {
  253. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  254. PDFDocument *pdf = [[PDFDocument alloc] init];
  255. NSImage *imag = [[NSImage alloc] initWithContentsOfFile:imgaePath];
  256. PDFPage *page = [[PDFPage alloc] initWithImage:imag];
  257. [pdf insertPage:page atIndex: [pdf pageCount]];
  258. BOOL isSucceed = [pdf writeToFile:savePath];
  259. dispatch_async(dispatch_get_main_queue(), ^{
  260. NSDictionary * dic = nil;
  261. complet(isSucceed,dic);
  262. });
  263. });
  264. }
  265. @end