如何在IOS7中打印PDF

时间:2014-04-30 08:36:05

标签: ios pdf printing

在我的应用程序中我有PDF文件网址我想从设备上打印出来请告诉我如何实现这个。我的加载视图中有PDF个网址,我只想点击UIButton打印。

我的加载视图代码。

   - (void)viewDidLoad
 {
    [super viewDidLoad];
      NSURL *myURL =[NSURL URLWithString:@"myurl"];
     NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
     [webview loadRequest:myRequest];

  }

请告诉我如何实现这一点我已经被困在这里很久了。

感谢。

1 个答案:

答案 0 :(得分:0)

请检查以下功能

- (IBAction)printWebPage:(id)sender
    {


        UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
        if(!controller)
        {
            return;
        }

        UIPrintInteractionCompletionHandler completionHandler =
        ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
            if(!completed && error){

            }
        };


        // 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 = @"";
        // 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;

        // 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.
        MyPrintPageRenderer *myRenderer = [[MyPrintPageRenderer alloc] init];
        // The MyPrintPageRenderer 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 = [sclviwReport viewPrintFormatter];

    #if SIMPLE_LAYOUT
        /*
         For the simple layout we simply set the header and footer height to the height of the
         text box containing the text content, plus some padding.

         To do a layout that takes into account the paper size, we need to do that
         at a point where we know that size. The numberOfPages method of the UIPrintPageRenderer
         gets the paper size and can perform any calculations related to deciding header and
         footer size based on the paper size. We'll do that when we aren't doing the 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;
        [myRenderer release];

        // The method we use presenting the printing UI depends on the type of
        // UI idiom that is currently executing. Once we invoke one of these methods
        // to present the printing UI, our application's direct involvement in printing
        // is complete. Our custom printPageRenderer will have its methods invoked at the
        // appropriate time by UIKit.

        //    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        //        [controller presentFromBarButtonItem:printButton animated:YES completionHandler:completionHandler];  // iPad
        //    else
        //        [controller presentAnimated:YES completionHandler:completionHandler];  // iPhone
        [controller presentFromRect:CGRectMake(btnReportPrint.frame.origin.x, btnReportPrint.frame.origin.y+40, btnReportPrint.frame.size.width, btnReportPrint.frame.size.height) inView:self.view animated:YES completionHandler:completionHandler];

    }

这将打开带有打印机名称的弹出控制器。从列表中选择一个打印机以打印网页。而不是sclviwReport使用您的webview