代码之家  ›  专栏  ›  技术社区  ›  Darren

wkwebview的MacOS PDF/打印质量差

  •  0
  • Darren  · 技术社区  · 6 年前

    我一直在使用一种在MacOS上生成prints/pdf的标准方法 WebView 生成内容和以下内容以打印/另存为PDF。

    NSPrintOperation *printOperation = [NSPrintOperation printOperationWithView:[[[sender mainFrame] frameView] documentView]
                                                                          printInfo:self.printInfo];
    

    不过,这个效果很好 Web视图 从10.14开始就被弃用了,10.15就要开始了。 WKWebView . 通过 wkwebview网站 的视图 NSPrintOperation 总是给出一个空白页,在这里的其他几个问题中已经报告过。

    我可以使用以下代码:

    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
    self.webView = [[WKWebView alloc] initWithFrame:printRect configuration:configuration];
    self.webView.navigationDelegate = self;
    
    [self.webView loadHTMLString:htmlString baseURL:nil];
    

    - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation API_AVAILABLE(macosx(10.13))
    {   
        if (@available(macOS 10.13, *))
        {
            [webView takeSnapshotWithConfiguration:nil completionHandler:^(NSImage *snapshotImage, NSError *error) {
    
                if (!error)
                {   
                    NSRect vFrame = NSZeroRect;
                    vFrame.size = [snapshotImage size];
                    NSImageView *imageView = [[NSImageView alloc] initWithFrame:vFrame];
                    [imageView setImage:snapshotImage];
    
                    NSPrintOperation *printOperation = [NSPrintOperation printOperationWithView:imageView
                                                                                      printInfo:self.printInfo];
    
                    if (self.saveToFilename)
                    {
                        printOperation.showsPrintPanel = NO;
                        printOperation.showsProgressPanel = YES;
                    }
                    else
                    {
                        printOperation.showsPrintPanel = YES;
                        printOperation.showsProgressPanel = YES;
                    }
    
                    BOOL success = [printOperation runOperation];
    
                    if (self.printCompletionBlock) self.printCompletionBlock(success);
                }
            }];
        }
    }
    

    它生成 wkwebview网站 然后使用 NSImageView 传递给 nsprint操作 。 问题是PDF/打印的质量不如旧方法好。

    OLD NEW

    我如何才能从wkwebview获得与旧webview相同的质量?

    0 回复  |  直到 6 年前
        1
  •  0
  •   Antoine Rosset    6 年前

    目前不支持从wkwebview在MacOS上打印。

    见:

    How does one Print all WKWebView On AND Offscreen content OSX and iOS

    推荐文章