Print image and text using UIPrintInteractionController

557 Views Asked by At

I'm using the following code to print text and image combined.Printing works but the image is not printed, instead, its binary data is printed.

 NSURL *url = [NSURL URLWithString:@"[URL here]"];

 if ([UIPrintInteractionController canPrintURL:url]) {

    NSMutableString *msgBody = [[NSMutableString alloc] initWithString:@""];
    [msgBody appendString:[[NSString stringWithFormat:@"%@", @"Printed fromMyApp"] stringByReplacingOccurrencesOfString:@"\n" withString:@" "]];

    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];

    NSString *base64String = [UIImagePNGRepresentation(image)
                              base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    [msgBody appendString:[NSString stringWithFormat:@"%@",base64String]];


    [msgBody appendString:@""];

     UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
      pic.delegate = self;
     UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.outputType = UIPrintInfoOutputGeneral;
    printInfo.jobName = @"Printing";
    pic.printInfo = printInfo;

    UIMarkupTextPrintFormatter *textFormatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:msgBody];
  textFormatter.startPage = 0;
   textFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); // 1 inch margins
   textFormatter.maximumContentWidth = 6 * 72.0;
   pic.printFormatter = textFormatter;

                           void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error)
    {
        if (!completed && error)
        {
            NSLog(@"Printing could not complete because of error: %@", error);
        }
    };

                           [pic presentAnimated:YES completionHandler:completionHandler];
}

Printed result

Printed fromMyApp iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAAAXNSR0IArs4c6QAA ABxpRE9UAAAAAgAAAAAAAABkAAAAKAAAAGQAAABkAACK2Wi+3EAAAEAASURBVHgB 7L2Fex1Htvb7/Q/3nplJDGJmloWWJdmSZZktMzMzJYY4dsxOzMzMzBRDzAySZbEl S4Y4Axmm895f7Tnt6exIjpPJ+U7OHe/naVV3VXV1q7tWLXrX6v8j6f+82949g3dz oPo58I443i0Q7+bAG+bAu4fzhofzblWtflX9d3ou7wjkHYG8mwNvmAPvHs4bHs6/ 00r57n+tnlu+I5B3BPJuDrxhDrx7OG94OO9W1epX1X+n5/KOQN4RyLs58IY58O7h vOHh/DutlO/+1+q55TsCeUcg7+bAG+bAu4fzhofzblWtflX9d3ou/+c///M/9e++ /e1vf9PszzqqR+9oDRiUrYLHd/7tn8m/+5yw/n/DQf6v/MwFf6q/3//+a300Jl6T xgRp0kg/bVnW7ydzqz/l5/aTeUj/jTfi4CBmfItirBdiP7bvO/d9U9vb9jX9zM8a 6w+//70e3bmtc9t36fr+gzq7c5c+mzhJqz6ZoSsnTupXr7583d9+nn3fGstemvY/ /ekPys29opPH1+n08V16eP+mqdbW1d21eHKIFn0cprHD4/S7333tuB9HI3+cx7GO TbvZt0r7fnV1Vru9zaozZXWbva99336eqTc/+/lvOn5Tm32M6q7hfK69v9VmSvOz t9n3rbbqSqvO6v+m45ra7PXWvr00++ZnrmH9rOtZdab8FoE4dzbHzidadW/T136u tW+dZ43z61/+Us8rynVo+3aN79lLHaLrqbdviMZ4BGusR5BGeAWrr1eghvmEaYhf mPpHxCr/zj/FIGsca3yrtK5jRKjf//7X+tVXhbp1ap4+X52tk4sStHdRR926dMzR bd/WYVo0JVofDAlRv97+WrqorwoLcq0hHKXzuObY/JzrrTp7afatn3WedexcWu32 ce37Vn97P3udva993/Sp6Rzn8+3H9vOsentpXcMa295m7dvb7Pum3fm4pnNMP2ur 7jxrHHsf57Gc2+zH1r4prd+3RCyrk9XBKu0nWXX/avm3v/5Vq6ZOVXfPAA3wDlY/ zyAN9wvXMM8QDfMI1EjPQA139Wc/QIM5HuEeoqEQzHDvUI1q2lJ/+cufX9+C/f6s 

How image is displayed instead of binary data

Any suggestions would be highly appreciated

1

There are 1 best solutions below

0
On

I think the UIMarkupTextPrintFormatter produces a text only output (as the name itself suggests)