GB540
GB540

Reputation: 121

UIMarkupTextPrintFormatter printing extra blank page

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.

Upvotes: 11

Views: 2137

Answers (4)

alex-i
alex-i

Reputation: 5454

As @Hassy mentioned in his answer, the issue may be with the html/css code.

In my case, I was setting html, body {min-height: 100%;} which probably didn't go well with some margins/padding, but the issue can be anything related to css.
Setting a background-color to body will help check if this is indeed the issue (if the color overlaps on the 'blank' page, then the issue is with the styling).

Upvotes: 0

Hassy
Hassy

Reputation: 5208

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

style='page-break-after:always;

Upvotes: 1

Allan  Macatingrao
Allan Macatingrao

Reputation: 2101

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>"

Upvotes: 8

Anastasia
Anastasia

Reputation: 3034

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.

Upvotes: 4

Related Questions