4

I've been using what seems to be a standard way of generating prints/PDF's on MacOS by using a WebView to generate the contents and the following to print/save as a PDF.

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

This works great, however WebView has been deprecated since 10.14 and with 10.15 on the way it's time to move over to WKWebView. Passing the WKWebView's view to the NSPrintOperation always gives a blank page which has been reported in several other questions here.

I have it all working with the following code:

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);
            }
        }];
    }
}

It generates an image snapshot of the WKWebView then uses an NSImageView to pass to the NSPrintOperation. The problem is that the quality of the PDF/Print is not as good as the old method.

OLD NEW

How can I get the same quality from a WKWebView that I did from the old WebView?

Darren
  • 10,182
  • 20
  • 95
  • 162

1 Answers1

0

There is currently no support for printing on macOS from a WKWebView.

See:

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

Antoine Rosset
  • 1,045
  • 2
  • 13
  • 22
  • What would be an alternative to using a WKWebView? So many apps generate prints, so what other way than using a html? – Darren Aug 16 '19 at 14:38
  • You could include wkhtmltopdf (https://wkhtmltopdf.org) in your application, when you need to produce a PDF. – Antoine Rosset Aug 16 '19 at 15:05
  • @Darren printing an offscreen view is a possible way. It depends on the data and the print. – Willeke Aug 17 '19 at 11:21
  • @AntoineRosset `wkhtmltopdf` looks great, but I can't find any info anywhere on how I might add this to an app to work independently of pre-installing it. Any tips? – Darren Aug 18 '19 at 10:47