I figured out the problem with help from the Google Cloud Print mailing list. Hopefully I can find time to package this into a Pod and make it easier to work with.
Disclaimer: There are some unfinished/not cleaned up pieces here but focus on the Mime Document, ticket, and NSInputStream.
@implementation EBCloudPrint
+(void) printFile:(NSString *)file completion:(void (^)(void))completion{
[EBCloudPrint printImage:[NSData dataWithContentsOfFile:file] completion:completion];
}
+(void) printImage:(NSData *)image completion:(void (^)(void))completion{
NSString *printer = [EBPreferences getUserPreference:PREFERENCE_GOOGLE_CLOUD_PRINTER];
if (printer == nil) {
NSLog(@"Ignoring print");
if (completion) {
completion();
}
return;
}
GTMOAuth2Authentication *auth = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:GOOGLE_KEYCHAIN_NAME
clientID:GOOGLE_CLIENT_ID
clientSecret:GOOGLE_CLIENT_SECRET];
// Return immediately if we haven't auth
if (![auth canAuthorize]) {
if (completion) {
completion();
}
return;
}
[GTMHTTPFetcher setLoggingEnabled:YES];
GTMMIMEDocument *doc = [GTMMIMEDocument MIMEDocument];
// MediaSize docs: https://developers.google.com/cloud-print/docs/cdd#MediaSize
NSString *ticketJson = @"{\"version\": \"1.0\", \"print\": { \"media_size\": { \"width_microns\": 101600, \"height_microns\": 152400 }}}"; //media_size: 4x6
[doc addPartWithHeaders:@{@"Content-Disposition": @"form-data; name=\"ticket\"", @"Content-Type": @"text/json"}
body:[ticketJson dataUsingEncoding:NSUTF8StringEncoding]];
[doc addPartWithHeaders:@{@"Content-Disposition": @"form-data; name=\"content\"; filename=\"eb.png\"", @"Content-Type": @"image/png"}
body:image];
NSString *boundary = nil;
unsigned long long length = -1;
NSInputStream *stream = nil;
[doc generateInputStream:&stream
length:&length
boundary:&boundary];
if (stream) {
NSString *url = [NSString stringWithFormat:@"%@?output=json&printerid=%@&contentType=image/png&title=MyApp&tag=my_app", CLOUD_PRINT_JOB_URL, printer];
NSLog(@"Printing to: %@", url);
NSMutableURLRequest *jobSubmitRequest;
NSTimeInterval timeout = 300.0;
jobSubmitRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:timeout];
NSString *httpHeaderContentTypeString = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[jobSubmitRequest addValue:@"my_app" forHTTPHeaderField:@"X-CloudPrint-Proxy"];
[jobSubmitRequest addValue:httpHeaderContentTypeString forHTTPHeaderField:@"Content-Type"];
[jobSubmitRequest setHTTPMethod:@"POST"];
GTMHTTPFetcher* fetcher = [GTMHTTPFetcher fetcherWithRequest:jobSubmitRequest];
[fetcher setPostStream:stream];
[fetcher setAuthorizer:auth];
[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
if (data) {
NSLog(@"data: %@", [NSString stringWithUTF8String:[data bytes]]);
}
if (error) {
NSLog(@"---- error: %@", error.localizedDescription);
} else if(completion){
completion();
}
}];
stream = nil;
fetcher = nil;
}
}
+(NSArray *) getPrinters{
return [[NSArray alloc] init];
}
@end