Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print paper size and content inset

I'm using following code to print HTML content containing text and images.

if (![UIPrintInteractionController isPrintingAvailable]) {
    UIAlertView *alertView = [[[UIAlertView alloc] 
        initWithTitle:NSLocalizedString(@"Printer Availability Error Title", @"")
        message:NSLocalizedString(@"Printer Availability Error Message", @"")
        delegate:nil
        cancelButtonTitle:NSLocalizedString(@"OK", @"OK")
        otherButtonTitles:nil] autorelease];
    [alertView show];
    return;
}

UIPrintInteractionController *pic = 
    [UIPrintInteractionController sharedPrintController];

if(!pic) {
    NSLog(@"Couldn't get shared UIPrintInteractionController!");
    return;
}

pic.delegate = self;

UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = @"Sample";
pic.printInfo = printInfo;

NSString *htmlString = [self prepareHTMLText];
UIMarkupTextPrintFormatter *htmlFormatter = 
    [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:htmlString];
htmlFormatter.startPage = 0;
// 1-inch margins on all sides
htmlFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); 
// printed content should be 6-inches wide within those margins
htmlFormatter.maximumContentWidth = 6 * 72.0;   
pic.printFormatter = htmlFormatter;
[htmlFormatter release];

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:self.myPrintBarButton 
        animated:YES 
        completionHandler:completionHandler];

} else {
    [pic presentAnimated:YES completionHandler:completionHandler];
}

See attached for results (the scaled down version may not be very clear, but, hopefully, you get the picture).

Here are my questions:

  1. How is the print paper size determined by AirPrint? What if I want to specifically format and print data for A4 paper?

  2. The result of using the above code and printing using different simulated printers (Printer Simulator) is that, in all cases, I get a 1 inch margin on the top of first page, but not on consecutive pages. Why?

  3. The result of using the above code and printing using different simulated printers (Printer Simulator) is that, in some cases, the font style is lost. As a result, the content is shifted down. Why?

Screen-shot

like image 948
Mustafa Avatar asked Apr 28 '11 11:04

Mustafa


People also ask

What paper size should my printer be set at?

If you're in the United States or Canada, standard printer paper dimensions for most documents is that of the standard letter paper size, which is 8.5 inches by 11 inches. In much of the rest of the world, it is A4, which is 297 millimeters by 210 millimeters.

What does fit to print size mean?

You can use the Fit to Page printing function when you want to print, for example, a Letter-size document on another size paper. Fit to Page printing automatically fits images or text you want to print to the paper size you select in the printer software, or it allows you to manually resize the image.


1 Answers

  1. To specifically select A4 paper size, I implemented the printInteractionController:choosePaper: method of the <UIPrintInteractionControllerDelegate> protocol, and returned an A4 paper size if supported by the printer (test with [UIPrintPaper bestPaperForPageSize:withPapersFromArray:]. Note that portrait/landscape orientation is not set here, but by the UIPrintInfo property orientation.

  2. The property htmlFormatter.contentInsets only sets the inset for the content as a whole, before it's been split across pages by the page renderer. I was able to set a 1cm per-page margin by adding blank headers and footers via a UIPrintPageRenderer, and then adding a 1cm margin to the left and right of the HTML print formatter:

    UIPrintPageRenderer *renderer = [[UIPrintPageRenderer alloc] init];
    renderer.headerHeight = 30.0f;
    renderer.footerHeight = 30.0f;
    pic.printPageRenderer = renderer;
    [renderer release];
    
    UIMarkupTextPrintFormatter *htmlFormatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText: htmlString];
    htmlFormatter.startPage = 0;
    htmlFormatter.contentInsets = UIEdgeInsetsMake(0.0f, 30.0f, 0.0f, 30.0f);
    [renderer addPrintFormatter: htmlFormatter startingAtPageAtIndex: 0];
    [htmlFormatter release];
    
  3. Can't help with this one sorry.

like image 72
tobygriffin Avatar answered Sep 23 '22 22:09

tobygriffin