// // KMConvertPDFManagerOC.m // PDF Master // // Created by wanjun on 2023/3/8. // #import "KMConvertPDFManagerOC.h" @implementation KMConvertPDFManagerOC #pragma mark - Public + (NSArray *)supportFileType { NSMutableArray *supportArray = [NSMutableArray array]; [supportArray addObjectsFromArray:[KMConvertPDFManagerOC supportImages]]; if ([KMConvertPDFManagerOC isSupportConvertPages]) { [supportArray addObjectsFromArray:[KMConvertPDFManagerOC supportPages]]; } else if ([KMConvertPDFManagerOC isSupportConvertWord]){ [supportArray addObjectsFromArray:[KMConvertPDFManagerOC supportWord]]; } if ([KMConvertPDFManagerOC isSupportConvertKeynote]) { [supportArray addObjectsFromArray:[KMConvertPDFManagerOC supportKeynote]]; } else if ([KMConvertPDFManagerOC isSupportConvertPPTX]){ [supportArray addObjectsFromArray:[KMConvertPDFManagerOC supportPPTX]]; } if ([KMConvertPDFManagerOC isSupportConvertNumber]) { [supportArray addObjectsFromArray:[KMConvertPDFManagerOC supportNumber]]; } else if ([KMConvertPDFManagerOC isSupportConvertExcel]){ /** 经测试(2011版 Excel)转档成功以后地址会存在问题,暂时建议不要用 **/ [supportArray addObjectsFromArray:[KMConvertPDFManagerOC supportExcel]]; } return supportArray; } + (void)convertFile:(NSString *)filePath savePath:(NSString *)savePath completionHandler:(void (^)(BOOL success,NSDictionary *errorDic))complet { NSString *tSavePath = savePath; if ([[tSavePath substringToIndex:1] isEqualToString:@"/"]) { tSavePath = [tSavePath substringFromIndex:1]; } tSavePath = [tSavePath stringByReplacingOccurrencesOfString:@"/" withString:@":"]; NSString *tFilePath = filePath; if ([[tFilePath substringToIndex:1] isEqualToString:@"/"]) { tFilePath = [tFilePath substringFromIndex:1]; } tFilePath = [tFilePath stringByReplacingOccurrencesOfString:@"/" withString:@":"]; NSString * extension = [filePath.pathExtension lowercaseString]; if ([[KMConvertPDFManagerOC supportPages] containsObject:extension]) { if ([[KMConvertPDFManagerOC supportWord] containsObject:extension]){ [KMConvertPDFManagerOC convertWordPath:tFilePath savePath:tSavePath completionHandler:^(BOOL success,NSDictionary *errorDic){ complet(success,errorDic); }]; } else { [KMConvertPDFManagerOC convertApplicationName:@"Pages" filePath:tFilePath savePath:tSavePath completionHandler:^(BOOL success, NSDictionary *errorDic) { complet(success,errorDic); }]; } } else if ([[KMConvertPDFManagerOC supportKeynote] containsObject:extension]){ if ([[KMConvertPDFManagerOC supportPPTX] containsObject:extension]){ [KMConvertPDFManagerOC convertPPTXPath:tFilePath savePath:tSavePath completionHandler:^(BOOL success,NSDictionary *errorDic){ complet(success,errorDic); }]; } else { [KMConvertPDFManagerOC convertApplicationName:@"Keynote" filePath:tFilePath savePath:tSavePath completionHandler:^(BOOL success, NSDictionary *errorDic) { complet(success,errorDic); }]; } } else if ([[KMConvertPDFManagerOC supportNumber] containsObject:extension]){ if ([[KMConvertPDFManagerOC supportExcel] containsObject:extension]){ [KMConvertPDFManagerOC convertExcelPath:tFilePath savePath:tSavePath completionHandler:^(BOOL success,NSDictionary *errorDic){ complet(success,errorDic); }]; } else { [KMConvertPDFManagerOC convertApplicationName:@"Numbers" filePath:tFilePath savePath:tSavePath completionHandler:^(BOOL success, NSDictionary *errorDic) { complet(success,errorDic); }]; } } else if ([[KMConvertPDFManagerOC supportImages] containsObject:extension]){ [KMConvertPDFManagerOC convertImagePath:filePath savePath:savePath completionHandler:^(BOOL success, NSDictionary *errorDic) { complet(success,errorDic); }]; } else { NSDictionary * dic = nil; complet(NO,dic); } } + (NSArray *)supportImages { return [NSArray arrayWithObjects:@"jpg",@"cur",@"bmp",@"jpeg",@"gif",@"png",@"tiff",@"tif",/*@"pic",*/@"ico",@"icns",@"tga",@"psd",@"eps",@"hdr",@"jp2",@"jpc",@"pict",@"sgi", nil]; } #pragma mark - convert Word + (BOOL)isSupportConvertWord { NSString * fullPath = [[NSWorkspace sharedWorkspace] fullPathForApplication:@"Microsoft Word"]; if (fullPath) { return YES; } return NO; } + (NSArray *)supportWord { return [NSArray arrayWithObjects:@"docx",@"doc",nil]; } + (void)convertWordPath:(NSString *)filePath savePath:(NSString *)savePath completionHandler:(void (^)(BOOL success,NSDictionary *errorDic))complet { dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSString * convertString = [NSString stringWithFormat:@"set filePath to\"%@\"\n",filePath]; convertString = [convertString stringByAppendingFormat:@"set savePath to \"%@\"\n",savePath]; convertString = [convertString stringByAppendingFormat:@"tell application \"Microsoft Word\"\n"]; convertString = [convertString stringByAppendingFormat:@"set isRun to running\n"]; convertString = [convertString stringByAppendingFormat:@"open file filePath\n"]; convertString = [convertString stringByAppendingFormat:@"save as active document file format format PDF file name savePath\n"]; convertString = [convertString stringByAppendingFormat:@"close active document\n"]; convertString = [convertString stringByAppendingFormat:@"if not isRun then quit\n"]; convertString = [convertString stringByAppendingFormat:@"end tell\n"]; NSDictionary * dic = nil; NSAppleScript *docScript = [[NSAppleScript alloc] initWithSource:convertString]; [docScript executeAndReturnError:&dic]; dispatch_async(dispatch_get_main_queue(), ^{ complet(YES,dic); }); }); } #pragma mark - convert Numbers + (BOOL)isSupportConvertNumber { NSString * fullPath = [[NSWorkspace sharedWorkspace] fullPathForApplication:@"Numbers"]; if (fullPath) { return YES; } return NO; } + (NSArray *)supportNumber { return [NSArray arrayWithObjects:@"xls",@"xlsx",@"numbers",@"csv",nil]; } + (void)convertApplicationName:(NSString *)appName filePath:(NSString *)filePath savePath:(NSString *)savePath completionHandler:(void (^)(BOOL success,NSDictionary *errorDic))complet { dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSString * convertString = [NSString stringWithFormat:@"tell application \"%@\"\n",appName]; convertString = [convertString stringByAppendingFormat:@"set isRun to running\n"]; convertString = [convertString stringByAppendingFormat:@"set in_file to \"%@\"\n",filePath]; convertString = [convertString stringByAppendingFormat:@"set out_file to \"%@\"\n",savePath]; convertString = [convertString stringByAppendingFormat:@"set mydoc to open file in_file\n"]; convertString = [convertString stringByAppendingFormat:@"export mydoc to file out_file as PDF\n"]; convertString = [convertString stringByAppendingFormat:@"close mydoc saving no\n"]; convertString = [convertString stringByAppendingFormat:@"if not isRun then quit\n"]; convertString = [convertString stringByAppendingFormat:@"end tell"]; NSDictionary * dic = nil; NSAppleScript *docScript = [[NSAppleScript alloc] initWithSource:convertString]; [docScript executeAndReturnError:&dic]; dispatch_async(dispatch_get_main_queue(), ^{ complet(YES,dic); }); }); } #pragma mark - convert Keynote + (BOOL)isSupportConvertKeynote { NSString * fullPath = [[NSWorkspace sharedWorkspace] fullPathForApplication:@"Keynote"]; if (fullPath) { return YES; } return NO; } + (NSArray *)supportKeynote { return [NSArray arrayWithObjects:@"ppt",@"pptx",@"key",nil]; } #pragma mark - convert Pages + (BOOL)isSupportConvertPages { NSString * fullPath = [[NSWorkspace sharedWorkspace] fullPathForApplication:@"Pages"]; if (fullPath) { return YES; } return NO; } + (NSArray *)supportPages { return [NSArray arrayWithObjects:@"pages",@"docx",@"doc",@"txt",@"rtf",nil]; } #pragma mark - convert PPTX + (BOOL)isSupportConvertPPTX { NSString * fullPath = [[NSWorkspace sharedWorkspace] fullPathForApplication:@"Microsoft PowerPoint"]; if (fullPath) { return YES; } return NO; } + (NSArray *)supportPPTX { return [NSArray arrayWithObjects:@"ppt",@"pptx",nil]; } + (void)convertPPTXPath:(NSString *)filePath savePath:(NSString *)savePath completionHandler:(void (^)(BOOL success,NSDictionary *errorDic))complet { dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSString * convertString = [NSString stringWithFormat:@"tell application \"Microsoft PowerPoint\"\n"]; convertString = [convertString stringByAppendingFormat:@"set isRun to running\n"]; convertString = [convertString stringByAppendingFormat:@"set savePath to \"%@\"\n",savePath]; convertString = [convertString stringByAppendingFormat:@"set filePath to \"%@\"\n",filePath]; convertString = [convertString stringByAppendingFormat:@"open file filePath\n"]; convertString = [convertString stringByAppendingFormat:@"save active presentation in savePath as save as PDF\n"]; convertString = [convertString stringByAppendingFormat:@"if not isRun then quit\n"]; convertString = [convertString stringByAppendingFormat:@"close active presentation\n"]; convertString = [convertString stringByAppendingFormat:@"end tell\n"]; NSDictionary * dic = nil; NSAppleScript *docScript = [[NSAppleScript alloc] initWithSource:convertString]; [docScript executeAndReturnError:&dic]; dispatch_async(dispatch_get_main_queue(), ^{ complet(YES,dic); }); }); } #pragma mark - convert Excel + (BOOL)isSupportConvertExcel { NSString * fullPath = [[NSWorkspace sharedWorkspace] fullPathForApplication:@"Microsoft Excel"]; if (fullPath) { return YES; } return NO; } + (NSArray *)supportExcel { return [NSArray arrayWithObjects:@"xlsx",@"xls",nil]; } + (void)convertExcelPath:(NSString *)filePath savePath:(NSString *)savePath completionHandler:(void (^)(BOOL success,NSDictionary *errorDic))complet { dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSString * convertString = [NSString stringWithFormat:@"set filePath to\"%@\"\n",filePath]; convertString = [convertString stringByAppendingFormat:@"set savePath to \"%@\"\n",savePath]; convertString = [convertString stringByAppendingFormat:@"set tFile to (POSIX path of filePath) as POSIX file\n"]; convertString = [convertString stringByAppendingFormat:@"tell application \"Microsoft Excel\"\n"]; convertString = [convertString stringByAppendingFormat:@"set isRun to running\n"]; convertString = [convertString stringByAppendingFormat:@"set wkbk1 to open workbook workbook file name tFile\n"]; convertString = [convertString stringByAppendingFormat:@"alias savePath\n"]; convertString = [convertString stringByAppendingFormat:@"save workbook as wkbk1 filename savePath file format PDF file format with overwrite\n"]; convertString = [convertString stringByAppendingFormat:@"close wkbk1 saving no\n"]; convertString = [convertString stringByAppendingFormat:@"if not isRun then quit\n"]; convertString = [convertString stringByAppendingFormat:@"end tell\n"]; NSDictionary * dic = nil; NSAppleScript *docScript = [[NSAppleScript alloc] initWithSource:convertString]; [docScript executeAndReturnError:&dic]; dispatch_async(dispatch_get_main_queue(), ^{ complet(YES,dic); }); }); } #pragma mark - convert Image + (void)convertImagePath:(NSString *)imgaePath savePath:(NSString *)savePath completionHandler:(void (^)(BOOL success,NSDictionary *errorDic))complet { dispatch_async(dispatch_get_global_queue(0, 0), ^{ PDFDocument *pdf = [[PDFDocument alloc] init]; NSImage *imag = [[NSImage alloc] initWithContentsOfFile:imgaePath]; PDFPage *page = [[PDFPage alloc] initWithImage:imag]; [pdf insertPage:page atIndex: [pdf pageCount]]; BOOL isSucceed = [pdf writeToFile:savePath]; dispatch_async(dispatch_get_main_queue(), ^{ NSDictionary * dic = nil; complet(isSucceed,dic); }); }); } @end