The solution I use, is to instantiate a good old WebView object, and use the print method of that object. My example uses a html string as starting point, but it should be easily adaptable to an URL.
Objective C
@interface WebPrinter ()
{
WebView *printView;
NSPrintInfo *printInfo;
}
@implementation WebPrinter
- (void)printHtml:(NSString *)html {
if (!printInfo) {
printInfo = [NSPrintInfo sharedPrintInfo];
printInfo.topMargin = 25;
printInfo.bottomMargin = 10;
printInfo.rightMargin = 10;
printInfo.leftMargin = 10;
}
NSRect printViewFrame = NSMakeRect(0, 0, printInfo.paperSize.width, printInfo.paperSize.height);
printView = [[WebView alloc] initWithFrame:printViewFrame frameName:@"printFrame" groupName:@"printGroup"];
printView.shouldUpdateWhileOffscreen = true;
printView.frameLoadDelegate = self;
[printView.mainFrame loadHTMLString:html baseURL:NSBundle.mainBundle.resourceURL];
}
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
if (frame != sender.mainFrame) {
return;
}
if (sender.isLoading) {
return;
}
if ([[sender stringByEvaluatingJavaScriptFromString:@"document.readyState"] isEqualToString:@"complete"]) {
sender.frameLoadDelegate = nil;
NSWindow *window = NSApp.mainWindow;
if (!window) {
window = NSApp.windows.firstObject;
}
NSPrintOperation *printOperation = [NSPrintOperation printOperationWithView: frame.frameView.documentView printInfo:printInfo];
[printOperation runOperationModalForWindow:window delegate:window didRunSelector:nil contextInfo:nil];
}
}
@end
Swift
class WebPrinter: NSObject, WebFrameLoadDelegate {
let window: NSWindow
var printView: WebView?
let printInfo = NSPrintInfo.shared
init(window: NSWindow) {
self.window = window
printInfo.topMargin = 30
printInfo.bottomMargin = 15
printInfo.rightMargin = 0
printInfo.leftMargin = 0
}
func printHtml(_ html: String) {
let printViewFrame = NSMakeRect(0, 0, printInfo.paperSize.width, printInfo.paperSize.height)
if let printView = WebView(frame: printViewFrame, frameName: "printFrame", groupName: "printGroup") {
printView.shouldUpdateWhileOffscreen = true
printView.frameLoadDelegate = self
printView.mainFrame.loadHTMLString(html, baseURL: Bundle.main.resourceURL)
}
}
func webView(_ sender: WebView!, didFinishLoadFor frame: WebFrame!) {
if frame != sender.mainFrame {
return
}
if sender.isLoading {
return
}
if sender.stringByEvaluatingJavaScript(from: "document.readyState") == "complete" {
sender.frameLoadDelegate = nil
let printOperation = NSPrintOperation(view: frame.frameView.documentView, printInfo: printInfo)
printOperation.runModal(for: window, delegate: window, didRun: nil, contextInfo: nil)
}
}
}