【问题标题】:IOS Printing PDF from WebView and Page is slightly offsetIOS 从 WebView 和 Page 打印 PDF 略有偏移
【发布时间】:2012-11-28 02:58:18
【问题描述】:

我正在使用下面的代码从名为“webby”的 Webview 打印 PDF。当我打印 PDF 时,页面输出会在顶部显示上一页。换句话说,如果我打印第 3 页,它会在第 3 页显示第 2 页的最后一行,而第 3 页的底部不会打印。关于我做错了什么或应该改变的任何想法?

UIPrintInteractionController *print = [UIPrintInteractionController sharedPrintController];


UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;

printInfo.duplex = UIPrintInfoDuplexLongEdge;
print.printInfo = printInfo;
print.showsPageRange = YES;

UIViewPrintFormatter *viewFormatter = [self.webby viewPrintFormatter];
viewFormatter.startPage = 0;
print.printFormatter = viewFormatter;


UIPrintInteractionCompletionHandler completionHandler = ^(UIPrintInteractionController *printInteractionController, BOOL completed, NSError *error) {};

[print presentAnimated:YES completionHandler:completionHandler];

【问题讨论】:

    标签: objective-c ios webview


    【解决方案1】:

    我使用了几乎相同的代码,只是稍作改动

    你能试试下面的代码吗

    - (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 = [webviwReport 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:10]; 
        CGSize titleSize = [myRenderer.jobTitle sizeWithFont:font];
        myRenderer.headerHeight = myRenderer.footerHeight = titleSize.height + 5;
    #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];
    
    }
    

    【讨论】:

    • 谢谢。更改 titleSize.height + 5 增加了额外的空间以进一步向下推上一页,但我无法调整它以将其从页面中删除。
    • 谢谢,但是如何更改控制器中的背景颜色..或设置图像任何东西???在此先感谢:) @GeoffreyMoore
    • 我知道为时已晚,但您可能想阅读此链接中的讨论discussions.apple.com/message/13146925#13146925
    猜你喜欢
    • 1970-01-01
    • 2019-06-05
    • 1970-01-01
    • 1970-01-01
    • 2022-08-09
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    相关资源
    最近更新 更多