Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIMarkupTextPrintFormatter printing extra blank page

Tags:

ios

printing

When using UIMarkupTextPrintFormatter to print a few lines of simple HTML, it puts out a blank page first, then the page with the text. The code is as follows, and very simple:

- (void) printSomething;
{
    if (![UIPrintInteractionController isPrintingAvailable])
        return;

    NSString* markupText =@"<html><body>THIS IS A TEST</body></html>";

    UIMarkupTextPrintFormatter* printFormatter =[ [ [UIMarkupTextPrintFormatter alloc] initWithMarkupText:markupText] autorelease];

    UIPrintInteractionController* printInteractionController =[UIPrintInteractionController sharedPrintController];
    printInteractionController.printFormatter =printFormatter;
    printInteractionController.delegate =self;  
    //printInteractionController.showsPageRange =YES;       

    [printInteractionController presentAnimated:YES completionHandler:nil];                                 
}

Now, if I uncomment the showsPageRange =YES, a single page prints as expected, BUT the UIPrintInteractionController takes several seconds to appear. Enough to make the user wonder if the app froze.

The very first line of the UIMarkupTextPrintFormatter docs state "Instances of the UIMarkupTextPrintFormatter class lay out HTML markup text for a multipage print job". It'd be kinda crazy if the formatter prints multiple pages, regardless of content...

Any idea what's wrong here? Other apps do this without any issues. Thanks in advance.

like image 725
GB540 Avatar asked Nov 13 '12 23:11

GB540


3 Answers

I got the same problem and i found out it was being caused by the html code

style='page-break-after:always;
like image 82
Hassy Avatar answered Nov 17 '22 06:11

Hassy


I solved it by having the correct HTML skeleton:

NSString *htmlString = "<!DOCTYPE html><html><head><meta charset='UTF-8'><title>Title</title></head><body>Hello!</body></html>"
like image 33
Allan Macatingrao Avatar answered Nov 17 '22 05:11

Allan Macatingrao


I had the same problem with appearance of second blank page with printInteractionController.showsPageRange = NO; and found the Apple example here (page 67). Here is it:

- (IBAction)printContent:(id)sender {
    UIPrintInteractionController *pic = [UIPrintInteractionController
                                         sharedPrintController];
    pic.delegate = self;
    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.outputType = UIPrintInfoOutputGeneral;
    printInfo.jobName = self.documentName;
    pic.printInfo = printInfo;
    UIMarkupTextPrintFormatter *htmlFormatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:self.htmlString];
    htmlFormatter.startPage = 0;
    htmlFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); // 1 inch margins
    pic.printFormatter = htmlFormatter;
    pic.showsPageRange = YES;
    void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
    ^(UIPrintInteractionController *printController, BOOL completed, NSError
      *error) {
        if (!completed && error) {
            NSLog(@"Printing could not complete because of error: %@", error);
        } };
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        [pic presentFromBarButtonItem:sender animated:YES
                    completionHandler:completionHandler];
    } else {
        [pic presentAnimated:YES completionHandler:completionHandler];
    }
}

This example uses printInteractionController.showsPageRange = YES;, and works fine, but if replace this line with

printInteractionController.showsPageRange = NO;, it prints extra second blank page.

So it seems that UIMarkupTextPrintFormatter is implicitly intended to be used with printInteractionController.showsPageRange = YES;, or it's just an API bug.

like image 4
Anastasia Avatar answered Nov 17 '22 04:11

Anastasia