Guest User

Untitled

a guest
May 20th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.29 KB | None | 0 0
  1. #import <Foundation/Foundation.h>
  2. #import <Quartz/Quartz.h>
  3. #import <ScreenSaver/ScreenSaver.h>
  4.  
  5. // cmd = "System/Library/Printers/Libraries/./quartzfilter '%s' '%s' '%s'" % (pdffilepath, qfilterpath, tmpfilepath)
  6.  
  7. // Returns the file path of an unused temporary file
  8. NSString *getTempFilePath (NSString *extension) {
  9. NSString *tempDirectory = NSTemporaryDirectory();
  10. NSFileManager *fileManager = [NSFileManager defaultManager];
  11. NSString *tempFilePath;
  12. BOOL pathExists = YES;
  13. while (pathExists == YES) {
  14. // creating a random integer between 1000 and 9999 with
  15. // a convenient function found in the ScreenSaver framework
  16. int randnum = SSRandomIntBetween(1000, 9999);
  17. // e.g. /TemporaryDirectory/8765.png
  18. tempFilePath = [NSString stringWithFormat:@"%@%d.%@", tempDirectory, randnum, extension];
  19. if ([fileManager fileExistsAtPath:tempFilePath] == NO) {
  20. pathExists = NO;
  21. }
  22. }
  23. // tempFilePath is an autoreleased object (created with NSSString stringWithFormat)
  24. return tempFilePath;
  25. }
  26.  
  27. //
  28. void printUsage () {
  29. printf("Usage: mergepdf -output [PDF path] -folder [PDF folder path] -qfilter [Quartz filter path]\n");
  30. }
  31.  
  32. //
  33. int sortPDFNames(id pdfName1, id pdfName2, void * context)
  34. {
  35. return [pdfName1 compare:pdfName2 options:NSLiteralSearch];
  36. }
  37.  
  38. int main (int argc, const char * argv[]) {
  39. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  40. NSFileManager *fileManager = [NSFileManager defaultManager];
  41. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  42. NSString *outputPath = [userDefaults stringForKey:@"output"];
  43. if ((outputPath == NULL) || ([outputPath isEqualToString:@""])) {
  44. printf("ERROR: Missing or invalid output path\n");
  45. printUsage();
  46. return 1;
  47. }
  48. BOOL applyQuartzFilter;
  49. BOOL isDir;
  50. NSString *quartzFilterPath = [userDefaults stringForKey:@"qfilter"];
  51. if (quartzFilterPath == NULL) {
  52. applyQuartzFilter = NO;
  53. } else {
  54. if ([quartzFilterPath isEqualToString:@""]) {
  55. printf("ERROR: Missing Quartz filter path\n");
  56. printUsage();
  57. return 1;
  58. } else {
  59. if ([fileManager fileExistsAtPath:quartzFilterPath] == NO) {
  60. NSLog(@"Quartz filter path does not exist");
  61. printf("ERROR: The Quartz filter does not exist:\n%s\n", [quartzFilterPath UTF8String]);
  62. return 1;
  63. }
  64. }
  65. applyQuartzFilter = YES;
  66. }
  67.  
  68. NSString *folderPath = [userDefaults stringForKey:@"folder"];
  69. if ((folderPath == NULL) || ([folderPath isEqualToString:@""])) {
  70. printf("ERROR: Missing or invalid folder path\n");
  71. printUsage();
  72. return 1;
  73. }
  74. if ([fileManager fileExistsAtPath:folderPath isDirectory:&isDir] == NO) {
  75. printf("ERROR: The folder does not exist:\n%s\n", [folderPath UTF8String]);
  76. return 1;
  77. } else {
  78. if (isDir == NO) {
  79. printf("ERROR: The given path does not point to a folder:\n%s\n", [folderPath UTF8String]);
  80. return 1;
  81. }
  82. }
  83. NSArray *itemNames = [fileManager directoryContentsAtPath:folderPath];
  84. if ([itemNames count] == 0) {
  85. printf("ERROR: The given folder path is empty:\n%s\n", [folderPath UTF8String]);
  86. }
  87.  
  88. NSMutableArray *pdfPaths = [[NSMutableArray alloc] initWithCapacity:1];
  89. NSEnumerator *itemNamesEnumerator = [itemNames objectEnumerator];
  90. NSString *itemName;
  91. while (itemName = [itemNamesEnumerator nextObject]) {
  92. // ignoring invisible items
  93. if ([itemName characterAtIndex:0] == '.') {
  94. continue;
  95. }
  96. // ignoring directories
  97. NSString *itemPath = [folderPath stringByAppendingPathComponent:itemName];
  98. BOOL isDir;
  99. if ([fileManager fileExistsAtPath:itemPath isDirectory:&isDir] == YES) {
  100. if (isDir == YES) {
  101. continue;
  102. }
  103. // simply discovering PDf files by a .pdf suffix :-)
  104. if ([itemName hasSuffix:@".pdf"]) {
  105. [pdfPaths addObject:itemPath];
  106. }
  107. }
  108. }
  109. if ([pdfPaths count] == NO) {
  110. printf("ERROR: No PDF files found in the given folder:\n%s\n", [folderPath UTF8String]);
  111. return 1;
  112. }
  113. printf("Found %i PDF documents in folder '%s'\n", [pdfPaths count], [folderPath UTF8String]);
  114. printf("Sorting found PDF document names alphabetically...\n");
  115. NSMutableArray *pdfNames = [[NSMutableArray alloc] initWithCapacity:1];
  116. NSEnumerator *pdfPathsEnumerator = [pdfPaths objectEnumerator];
  117. NSString *pdfPath;
  118. while (pdfPath = [pdfPathsEnumerator nextObject]) {
  119. NSString *pdfName = [pdfPath lastPathComponent];
  120. [pdfNames addObject:pdfName];
  121. }
  122. [pdfPaths release];
  123. NSArray *sortedPDFNames = [pdfNames sortedArrayUsingFunction:sortPDFNames context:NULL];
  124. PDFDocument *outputPDF;
  125. outputPDF = [[PDFDocument alloc] init];
  126. if (outputPDF == NULL) {
  127. NSBundle *myBundle = [NSBundle mainBundle];
  128. NSString *parentFolderPath = [myBundle bundlePath];
  129. NSString *dummyPDFPath = [parentFolderPath stringByAppendingPathComponent:@"dummy.pdf"];
  130. if ([fileManager fileExistsAtPath:dummyPDFPath] == NO) {
  131. printf("ERROR: Cannot load dummy PDF file supposed to be located at:\n%s\n", [dummyPDFPath UTF8String]);
  132. return 1;
  133. } else {
  134. NSURL *dummyPDFURL = [NSURL fileURLWithPath:dummyPDFPath];
  135. outputPDF = [[PDFDocument alloc] initWithURL:dummyPDFURL];
  136. if (outputPDF == NULL) {
  137. printf("ERROR: Could not initialize new PDF document\n");
  138. return 1;
  139. }
  140. [outputPDF removePageAtIndex:0];
  141. }
  142. }
  143. PDFDocument *inputPDF;
  144. int pagecounter = 0;
  145. NSEnumerator *pdfNamesEnumerator = [sortedPDFNames objectEnumerator];
  146. NSString *pdfName;
  147. while (pdfName = [pdfNamesEnumerator nextObject]) {
  148. NSString *pdfPath = [folderPath stringByAppendingPathComponent:pdfName];
  149. NSURL *pdfURL = [NSURL fileURLWithPath:pdfPath];
  150. inputPDF = [[PDFDocument alloc] initWithURL:pdfURL];
  151. if (inputPDF == NULL) {
  152. printf("ERROR: The file does not seem to be a valid PDF:\n\%s\n", [pdfPath UTF8String]);
  153. continue;
  154. }
  155. int pagecount = [inputPDF pageCount];
  156. printf("Processing PDF document: %s (Pages: %i)\n", [pdfName UTF8String], pagecount);
  157. int pagenum;
  158. for( pagenum = 0; pagenum < pagecount; pagenum++ ) {
  159. printf("\tAdding page %i\n", (pagenum + 1));
  160. PDFPage *singlePage = [inputPDF pageAtIndex:pagenum];
  161. [outputPDF insertPage:singlePage atIndex:pagecounter];
  162. pagecounter++;
  163. }
  164. }
  165. [pdfNames release];
  166. if (applyQuartzFilter == YES) {
  167. printf("Applying Quartz filter: %s\n", [quartzFilterPath UTF8String]);
  168. NSString *tmpFilePath = getTempFilePath(@"pdf");
  169. BOOL tmpWriteSuccess = [outputPDF writeToFile:tmpFilePath];
  170. if (tmpWriteSuccess == NO) {
  171. printf("ERROR: Could not write PDF content to temporary file.\n");
  172. return 1;
  173. }
  174. NSTask *shellCommand = [[NSTask alloc] init];
  175. [shellCommand setLaunchPath:@"/System/Library/Printers/Libraries/./quartzfilter"];
  176. // arrayWithObjects expects nil as the last object
  177. [shellCommand setArguments:[NSArray arrayWithObjects:tmpFilePath, quartzFilterPath, outputPath, nil]];
  178. [shellCommand launch];
  179. [shellCommand waitUntilExit];
  180. int shellCommandStatus = [shellCommand terminationStatus];
  181. if (shellCommandStatus != 0) {
  182. printf("ERROR: The quartzfilter utility did not run successfully\n");
  183. return 1;
  184. }
  185. [shellCommand release];
  186. [fileManager removeFileAtPath:tmpFilePath handler:NULL];
  187. printf("Created the new PDF (%i pages): '%s'\n", pagecounter, [outputPath UTF8String]);
  188. } else {
  189. printf("Writing the new PDF (%i pages): '%s'\n", pagecounter, [outputPath UTF8String]);
  190. BOOL writeSuccess = [outputPDF writeToFile:outputPath];
  191. if (writeSuccess == NO) {
  192. printf("ERROR: Could not write PDF file:\n%s\n", [outputPath UTF8String]);
  193. return 1;
  194. }
  195. }
  196. [outputPDF release];
  197. [inputPDF release];
  198. [pool drain];
  199. return 0;
  200. }
Add Comment
Please, Sign In to add comment