I was just about to give up and tell you to create a demo project that just opened one web view and printed it. But I thought I should try that first myself. I did and also got 2 blank pages.
There are lots and lots of possible options. But in this demo app, here is how I fixed it using a Swift Storyboard document-based app:
In Xcode Interface Builder, find the print menu item. Change its sent action from "print:" to "printDocument:" in NSResponder.
Observe that the app now prints correctly. I tried this with your sample and got 12 pages, including the last. Nothing seems to be missing except horizontal overview inside a div in the web view, but that's a different problem.
I poked around for a bit. It looks like the old XIB templates in Xcode used "printDocument:" as the sent action. For some reason, in the newer Storyboard templates this was changed to just "print:", which doesn't seem to work. No clue about SwiftUI.
I have a newer app in Swift that isn't document-based. In this app, I seem to have already changed the print action to "printView:", which I implemented in my view controller. My old Objective-C app uses printDocument, which the XIB template also uses.
So this solves your blank page problem with "printView:". This is also complicated by the NSView and NSWindow Objective-C methods "print:" that are re-defined in Swift as "printView:" and "printWindow:".
I'm still not sure why you're getting partial output. My demo app uses your code and prints fine. I played around with it a bit, putting the print method in both the view controller and the document. Worked fine either place. Perhaps the demo app would be a good place to start after all.
Topic:
Safari & Web
SubTopic:
General
Tags: