简体   繁体   中英

NSTask running pdftk command

I'm making an interface for pdftk with cocoa app for a school project but for some reason it wouldn't work it throws me this error

Error: Unable to find file.
Error: Failed to open PDF file: 
   articulo.pdf
Done.  Input errors, so no output created.

the current directory where my file is in downloads this is my current code

- (IBAction)getMetadata:(id)sender {

    task = [[NSTask alloc] init];

    NSString *arg = [NSString stringWithFormat:@"%@", fileNamePDF];
    [task setCurrentDirectoryPath: directory];
    task = [NSTask launchedTaskWithLaunchPath:@"/usr/local/bin/pdftk" arguments:@[arg, @"dump_data"]];

    task = nil;
}

"launchedTaskWithLaunchPath" creates new object (you can check in debugger). This is a way to go for you:

- (IBAction)getMetadata:(id)sender {
    task = [[NSTask alloc] init];
    NSPipe  *outputPipe = [NSPipe pipe];
    [task setStandardOutput:outputPipe];
    NSFileHandle *outputFileHandle = [outputPipe fileHandleForReading];
    NSString *arg = [NSString stringWithFormat:@"%@", fileNamePDF];
    [task setCurrentDirectoryPath: directory];
    [task setLaunchPath:@"/usr/local/bin/pdftk"];
    [task setArguments:@[arg, @"dump_data"]];
    [task launch];
    [self.task waitUntilExit];
    //read output
    NSData *outputData = [outputFileHandle readDataToEndOfFile];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM