Printing With Objective C

Discussion in 'Mac Programming' started by Jitin Jindal, Feb 19, 2013.

  1. Jitin Jindal, Feb 19, 2013
    Last edited by a moderator: Feb 19, 2013

    macrumors newbie

    Joined:
    Feb 18, 2013
    Location:
    India
    #1
    Please help in order to resolve the printing issue that i am facing while coding in objective c.
    I want to print any file like (*.pages,*.numbers,*.keynote)
    but i am not able to print these files using PMPrinterPrintWithFile function of objective c
    I am able to print other files like *.txt,*.jpeg,*.png, *.pdf.

    when I try to print iwork document like *.pages file with PMPrinterPRintWithFile, it returns a status as 1028
    when I am printing other files like *.pdf,*.txt it is printed easily with status 0

    and one more point I am not able to find out the MimeType of files like *.pages,*.number,*.keynote
    the MIMETYPE object gets NULL value when i fetch it using the below code.
    The Code I am using :
    Code:
        #import "Cocoa/Cocoa.h"
    
        int main(int argc, char *argv[])
        {
    
        NSPrintInfo *printInfo = [[NSPrintInfo alloc] init];
        PMPrintSession myPrintSession= (PMPrintSession)[printInfo PMPrintSession];
        PMPrintSettings printSetting= (PMPrintSettings)[printInfo PMPrintSettings];
        PMPageFormat pageFormat=(PMPageFormat)[printInfo PMPageFormat]; 
        OSStatus err = PMCreateSession(&myPrintSession);
        OSStatus status;
        CFURLRef fileURL =(CFURLRef) [NSURL fileURLWithPath:@"filename example *.*"];
        NSString* filePath=@"filename ex *.*";
        NSString* FullPath=[filePath stringByExpandingTildeInPath];
        NSURL * url= [NSURL fileURLWithPath:FullPath];
        NSURLRequest* fileUrlRequest = [[NSURLRequest alloc] initWithURL:url];
        NSError* error = nil;
        NSURLResponse* response = nil;
        NSData* fileData = [NSURLConnection sendSynchronousRequest:fileUrlRequest 
        returningResponse:&response error:&error];
        NSString * MIMETYPE= [response MIMEType];
        [fileUrlRequest release];
        NSLog(@"MIMETYPE %@",MIMETYPE);
        PMPrinter thisPrinter= "printername";		
        status = PMPrinterGetMimeTypes(thisPrinter, printSetting,&mimeTypes);
        if(status==noErr)
    	{
    	status= PMPrinterPrintWithFile(thisPrinter, printSetting, pageFormat,      
            (CFStringRef)MIMETYPE, fileURL);
    	NSLog(@" value :%@",MIMETYPE);
    	NSLog(@"%i",status);
    	NSLog(@"=======Printer %@=======", PMPrinterGetName( thisPrinter) );
    	}
    	[printInfo release];
          return 0;
        return NSApplicationMain(argc,  (const char **) argv);
        }
     
  2. macrumors 6502

    ElectricSheep

    Joined:
    Feb 18, 2004
    Location:
    Wilmington, DE
    #2
    PMPrinterGetMimeTypes(thisPrinter, printSetting,&mimeTypes) will give you a list of MIME types supported by the printer, if you actually pass a declared CFArray mimeTypes to it. I would wager that .pages, .numbers, and .keynote files do not appear in the list of supported MIME types.

    These files need to be rendered into a format suitable for printing before you can submit a print job.
     
  3. thread starter macrumors newbie

    Joined:
    Feb 18, 2013
    Location:
    India
    #3
    In which format do we need to render iwork documents to get it print

    yes, you are right i tried PMPrinterGetMimeTypes(thisPrinter, printSetting,&mimeTypes) and i am getting various mimetypes supported by printer but i am not able to understand which one to use in order to print the iwork documents.
    And if i hav to render the iwork documents in the compatible mimetype for printing then please tell me how to render it and in which format.

    Thanks....
     
  4. thread starter macrumors newbie

    Joined:
    Feb 18, 2013
    Location:
    India
    #4
    Printing With Objective C

    Please help in order to resolve the printing issue that i am facing while coding in objective c.
    I want to print any file like (*.pages,*.numbers,*.keynote)
    but i am not able to print these files using PMPrinterPrintWithFile function of objective c
    I am able to print other files like *.txt,*.jpeg,*.png, *.pdf.

    when I try to print iwork document like *.pages file with PMPrinterPRintWithFile, it returns a status as 1028
    when I am printing other files like *.pdf,*.txt it is printed easily with status 0
     
  5. macrumors regular

    Joined:
    Aug 21, 2011
    Location:
    Stockholm Sweden
    #5
    I believe the only way forward is to use the actual program to do the printing. You need to somehow start-up pages / numbers / keynotes and order it to do the printing. I believe that file formats are not published and I have not heard of any library or similar things that allows you to interpret the file and do printing.

    I would approach this by trying to automate the printing somehow. There might be parameters you could pass to the program or it could be possible to use some other automation. Once this is understood, next step would be to see how to start this from inside your application. There are ways to start a process running a shell from inside Objective-C which could be used. You might want to mimic the parameters passed to the program or do automation functions. This is not any nice situation as it will probably be quite dependant on version of OS and/or version of pages -- and these change over time and between different client stations.

    // Gunnar
     
  6. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #6
    That's because Pages, Numbers etc. are applications that know how to print their documents; it is not built into the operating system.

    Start Script Editor, look at the dictionaries for Pages, Numbers and so on, and check if you can use AppleScript to let the apps print these documents.
     
  7. thread starter macrumors newbie

    Joined:
    Feb 18, 2013
    Location:
    India
    #7
    yes i am able to print the pages, keynote and numbers file using applescript but i am not able to set the various settings like orientation and duplex printing with them.
    so I am thinking that is there any way to convert these documents into PDF files so that i can print PDF files using objective c, that can be done easily even with different printing settings.
     
  8. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #8
    There's lots of things you can do with AppleScript. Have a closer look at everything in these dictionaries.
     
  9. thread starter macrumors newbie

    Joined:
    Feb 18, 2013
    Location:
    India
    #9
    You are saying about GUI script and all. Actually I dont want to use the GUI script as in future if the apple changes the print panel layout then it needs to be updated thats the issue in using GUI scripting.

    If you have any idea how to convert the iwork document like keynote and numbers into PDF using applescript then please let me know.

    Thanks...
     
  10. thread starter macrumors newbie

    Joined:
    Feb 18, 2013
    Location:
    India
    #10
    Hello
    if you know anything about how to convert a document of type pages,keynote ,numbers into PDF using applescript then please let me know its urgent...

    thanks
     
  11. macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #11
    If it's urgent then I would say go the quick and easy route of relying on the current menu paths to the export options. There's a good chance it'll fail when iWork is updated, but it'll work until the machines running it are updated with a new version.

    Or rely on the include preview button being checked in the Save dialog and just grab the preview.pdf file I mentioned in another thread.
     
  12. gnasher729, Feb 28, 2013
    Last edited: Feb 28, 2013

    macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #12
    If it's urgent, then maybe your boss should post, and tell us what idea of payment he has.

    You did see "print settings" in AppleScript?
     
  13. thread starter macrumors newbie

    Joined:
    Feb 18, 2013
    Location:
    India
    #13
    ya i also tried with the preview button being enabled but with this case the print quality of the document is not as same as the preview is just not the exact pdf with the same resolution when you will print the preview.pdf file manually then you will see the size of it, it will be very small.

    ----------

    yes i saw the print settings but it is not able to set the duplex and orientation settings of the document when i will try to print it through applescript.
     

Share This Page