Print a SwiftUI view on macOS?

I made a test with a new XIB-based project using this code

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        guard let content = window.contentView else {
            print("Error: cannot access windows contentview!")
            return
        }
        
        let tv = NSTextView(frame: NSMakeRect(100, 300, 200, 40))
        tv.string = "Text subview"
        tv.font = NSFont.systemFont(ofSize: 30.0)
        content.addSubview(tv)

        let chartView = NSHostingView(rootView: myChart())
        chartView.setFrameSize(content.frame.size)
        content.addSubview(chartView)

        content.printView(self)        
    }

"myChart" is s sample view using Charts.

The window does show the text and the chart but the print shows the text only.

How can I get the SwiftUI-View printed?

My goal is a PDF output of a SwiftUI view.

There are already several older threads with similar questions but no solution, yet.

Are you still seeing this on a current version of macOS?

No idea. I've implemented a workaround by creating a PDF. This works well for me.

Here is some sample code:

    func savePDF(docName : String?) {
        guard let saveURL = showSavePDFPanel(docName) else {
            return
        }
        var mediaBox = NSRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 1100, height: 600))
        
        if let dataConsumer = CGDataConsumer(url: saveURL as CFURL) {
            if let pdfContext = CGContext(consumer: dataConsumer, mediaBox: &mediaBox, nil) {
                let options: [CFString: Any] = [kCGPDFContextMediaBox: mediaBox]
                
                for sem in ItrSemester.allCases {
                    pdfContext.beginPDFPage(options as CFDictionary)
                    let renderer = ImageRenderer(content: MyView().environmentObject(appData))
                    renderer.render { size, renderFunction in
                        pdfContext.translateBy(x: (mediaBox.width - size.width) / 2.0,
                                               y: (mediaBox.height - size.height) / 2.0)
                        renderFunction(pdfContext)
                    }
                    
                    /// Add a day header to each page with document name
                    let titleString = "\(docName ?? "Ohne Titel")"
                    let attrs : [NSAttributedString.Key : Any] = [.font: NSFont.boldSystemFont(ofSize: 16.0)]
                    let day  = NSAttributedString(string: titleString, attributes: attrs)
                    let path = CGMutablePath()
                    let strWidth  = day.size().width + 1
                    let strHeight = day.size().height + 1
                    let dayXPos   = 20.0
                    let dayYPos   = mediaBox.height - strHeight - 32
                    path.addRect(CGRect(x: dayXPos, y: dayYPos, width: strWidth, height: strHeight))
                    let fSetter = CTFramesetterCreateWithAttributedString(day as CFAttributedString)
                    let frame = CTFramesetterCreateFrame(fSetter, CFRangeMake(0, day.length), path, nil)
                    CTFrameDraw(frame, pdfContext)

                    pdfContext.endPDFPage()
                }
                pdfContext.closePDF()
            }
        }
    }
Print a SwiftUI view on macOS?
 
 
Q