3

I am loading a pdf file that is NSData into a webview as such:

[webView loadData:pdf MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil];

It displays beautifully in the UIWebView, and correctly shows the number of pages of the pdf.

However when I try to print, I get an entire page and then a left over page filled with maybe the last tenth or so of a single pdf page. In other words, I don't have a one to one page correspondence. It doesn't seem to scale the content down.

How do I scale the printing output? I print using this snippet:

UIPrintInteractionController *pic = 
     [UIPrintInteractionController sharedPrintController];

UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = printName;
printInfo.duplex = UIPrintInfoDuplexNone;

pic.printInfo = printInfo;

pic.printFormatter = [webView viewPrintFormatter];

pic.showsPageRange = YES;

This so question describes a method which involves scaling the input, but I am hoping something simpler exists.

Regards, Dane

Community
  • 1
  • 1
Danedo
  • 2,193
  • 5
  • 29
  • 37

1 Answers1

6

So I had this problem too. I was using a UIWebView to access a website link which opened a PDF document in the window. When the PDF was printing, the document was getting cropped at the bottom and sent to the next page. This was annoying because now a two page PDF document was printing as a three page PDF document with basically nothing on the third page. My fix was simple I did two things. I have not tested if both of these things are necessary to fix this problem however I will post both anything.

In this method in my ViewController.m:

- (void) webViewDidStartLoad:(UIWebView *)webView
{
    webView.scalesPageToFit = YES;
}

and then in my printing method (I have a method to print since I have multiple scenes with webviews and print buttons.

-(void) printWebView:(UIWebView *)webView {
 UIPrintInfo *pi = [UIPrintInfo printInfo];
    pi.outputType = UIPrintInfoOutputGeneral;
    pi.jobName = webView.request.URL.absoluteString;
    pi.orientation = UIPrintInfoOrientationPortrait;
    UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
    pic.printInfo = pi;
    pic.showsPageRange= NO;
    UIPrintPageRenderer *renderer = [[UIPrintPageRenderer alloc] init];
    webView.viewPrintFormatter.printPageRenderer.headerHeight = 30.0f;
    webView.viewPrintFormatter.printPageRenderer.footerHeight = 30.0f;
    webView.viewPrintFormatter.contentInsets = UIEdgeInsetsMake(0.0f, 30.0f, 0.0f, 30.0f);
    webView.viewPrintFormatter.startPage = 0;
    [renderer addPrintFormatter:webView.viewPrintFormatter startingAtPageAtIndex:0];
    pic.printPageRenderer = renderer;
    [pic presentAnimated:YES completionHandler:^(UIPrintInteractionController *pic2, BOOL completed, NSError *error) {
        // indicate done or error
    }];
}

Now I don't know if all that is necessary for you application but the lines I added which helped me with my scaling problem are

    webView.scalesPageToFit = YES;

and pic.showsPageRange= NO;

This may seem a bit odd but from what I've read, the

    pic.showsPageRange= NO;

is the key here. If this were set to yes, XCode AirPrint would scale the paged all screwy and your margins would get all messed up like mine did with my PDF loaded inside my webview. I actually read this on StackOverflow, and would like to credit the author of the post answer but I am having some difficulty locating his post. This seems like a bit of a trade off, as the print dialog will no longer show the page range for the document being printed, however for short documents, I personally think its worth the tradeoff for the simplicity of implementing a scaled document print vs a lengthy workaround.

Anyways, I hope this works for you, and if you find something better let us know.

PS I know my code for formatting/rendering is all messed up, I was trying to figure out how I could manipulate the printing options in XCode and have not deleted unnecessary lines yet.

eNc
  • 1,021
  • 10
  • 23
  • It worked thanks. For me the culprit is " pic.showsPageRange= NO;" – iPhoneDev Nov 19 '13 at 22:28
  • 1
    iPhoneDev, if your app is running on an iOS7 device, would you mind sharing your code, for some reason mine is broken since this iOS7 update. Pages are being printed both vertically and horizontally centered, but scaled down to 50% of the full size. – eNc Nov 22 '13 at 01:15
  • For some reason the above solution doesn't work for me. The PDF shows as only one page in the webView, but when i try to print , an extra empty page shows up – humblePilgrim Aug 02 '16 at 17:25