UIPrintInteractionController not printing to full page in iPhone

4.7k Views Asked by At

I am printing a pdf from iPhone to Airprint compatible printer using Apple print document example. But I am not getting full page print from iPhone, please see the picture of what I am getting, I need the pdf to be printed on full paper lenght,width.

enter image description here

The code I am using for print function is given below, its the same code as in Apple print sample app,

    UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
    if(!controller){
        NSLog(@"Couldn't get shared UIPrintInteractionController!");
        return;
    }

    UIPrintInteractionCompletionHandler completionHandler =
    ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
        if(!completed && error){
            NSLog(@"FAILED! due to error in domain %@ with error code %u", error.domain, error.code);
        }
    };

    controller.delegate = self;

    // Obtain a printInfo so that we can set our printing defaults.
    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    // This application produces General content that contains color.
    printInfo.outputType = UIPrintInfoOutputGeneral;
    // We'll use the URL as the job name.
    printInfo.jobName = [_filePath lastPathComponent];
    // Set duplex so that it is available if the printer supports it. We are
    // performing portrait printing so we want to duplex along the long edge.
    printInfo.duplex = UIPrintInfoDuplexLongEdge;
    // Use this printInfo for this print job.
    controller.printInfo = printInfo;
    controller.printingItem = myData;
    // Be sure the page range controls are present for documents of > 1 page.
    controller.showsPageRange = YES;

    // This code uses a custom UIPrintPageRenderer so that it can draw a header and footer.
    APLPrintPageRenderer *myRenderer = [[APLPrintPageRenderer alloc] init];
//  UIPrintPageRenderer *myRenderer = [[UIPrintPageRenderer alloc] init];
    // The APLPrintPageRenderer class provides a jobtitle that it will label each page with.
//    myRenderer.jobTitle = printInfo.jobName;
    // To draw the content of each page, a UIViewPrintFormatter is used.
    UIViewPrintFormatter *viewFormatter = [docWebView viewPrintFormatter];

#if SIMPLE_LAYOUT

    UIFont *font = [UIFont fontWithName:@"Helvetica" size:HEADER_FOOTER_TEXT_HEIGHT];
    CGSize titleSize = [myRenderer.jobTitle sizeWithFont:font];
    myRenderer.headerHeight = myRenderer.footerHeight = titleSize.height + HEADER_FOOTER_MARGIN_PADDING;
#endif
    [myRenderer addPrintFormatter:viewFormatter startingAtPageAtIndex:0];
    // Set our custom renderer as the printPageRenderer for the print job.
    controller.printPageRenderer = myRenderer;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        [controller presentFromBarButtonItem:sender animated:YES completionHandler:completionHandler];  // iPad
    }
    else
    {
        docWebView.hidden = YES;
        [controller presentAnimated:YES completionHandler:completionHandler];  // iPhone
    }
3

There are 3 best solutions below

0
On

I generate PDFs to print full page by drawing to a PDFcontext. I set it up with a call like this:

NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil);

note that because I've passed in CGRectZero for the size the context comes in its default size

 /*
  default pdf..
   8.5 X 11 inch
   612 x 792
   */

**this is only 72dpi, but its the default. I have a trick where I zoom out before drawing to improve resolution:

for (int page = 0 : page < numberPages : page ++ ){
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
CGFloat scaleFactor = 3.0;
CGContextSaveGState(pdfContext);
CGContextScaleCTM(pdfContext, 1.0/scaleFactor, 1.0/scaleFactor);


///draw stuff
/// context size is now (612.0 * 3.0 , 792.0 * 3.0) , i.e. resolution 72.0 * 3.0 dpi..
//


CGContextRestoreGState(pdfContext);

}//page drawing loop


NSString *filePath = [[NSFileManager defaultManager] //etcetc make a path];
BOOL success = [pdfData writeToFile:filePath atomically:YES];

ok so now i have my pdf i print directly to the UIPrintInteractionController

NSData *pdfData = [NSData dataWithContentsOfFile:[[self pdfView]filePath]];
   if ([UIPrintInteractionController canPrintData:pdfData]) {

    UIPrintInteractionController *pr = [UIPrintInteractionController sharedPrintController];

    void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
    ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
      if (!completed && error) NSLog(@"Print error: %@", error);
    };

    pr.printingItem = pdfData;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
      [pr presentFromRect:tool.frame inView:self.view animated:YES completionHandler:completionHandler];
    } else {
      [pr presentAnimated:YES completionHandler:completionHandler];
    } 
  }

Note that I don't mess about with UIPrintPageRenderer. I have no experience with that class, I've never needed to look into it. But it seems to me that if its making room for a header and a footer then these are necessarily going to scale your document down in order to make space. Try incorporating your header and footer into the document itself. Good luck

0
On
bestPaperForPageSize:withPapersFromArray:

Returns the print-paper object that UIKit determines to be the best for a print job based on the given page size and the paper size–imageable area combinations specific to the printer.

+ (UIPrintPaper *)bestPaperForPageSize:(CGSize)pageSize withPapersFromArray:(NSArray *)paperList

The delegate of UIPrintInteractionController may call this method in its implementation of the printInteractionController:choosePaper: method declared in the UIPrintInteractionControllerDelegate protocol.

0
On

instead of UIViewPrintFormatter *viewFormatter = [docWebView viewPrintFormatter]; use viewPrintFormatter *viewFormatter = [[UIPrintFormatter alloc] init];

don't need specific formatter for pdf and image