Note: I am working with swift 4 for osx. I would like to create a PDF file from WebView.
At the moment, my WebView is loading an html string and showing it successfully. If I press the button, the print panel opens and is ready to print the contents of the WebView in the correct format.
This is my print code:
var webView = WebView() var htmlString = "MY HTML STRING" override func viewDidLoad() { webView.mainFrame.loadHTMLString(htmlString, baseURL: nil) } func webView(_ sender: WebView!, didFinishLoadFor frame: WebFrame!) { let printInfo = NSPrintInfo.shared printInfo.paperSize = NSMakeSize(595.22, 841.85) printInfo.isHorizontallyCentered = true printInfo.isVerticallyCentered = true printInfo.orientation = .portrait printInfo.topMargin = 50 printInfo.rightMargin = 0 printInfo.bottomMargin = 50 printInfo.leftMargin = 0 printInfo.verticalPagination = .autoPagination printInfo.horizontalPagination = .fitPagination

Now I would like to have another button that saves the content directly as a pdf file.
I tried this:
let pdfData = webView.mainFrame.frameView.documentView.dataWithPDF(inside: webView.mainFrame.frameView.documentView.frame) let document = PDFDocument(data: pdfData) let fileURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("myPDF.pdf") document?.write(to: fileURL)
But the result of my pdf looks awful:

Does anyone have an idea?
UPDATE This is part of the result of my web view:

and this is the result of my preview / pdf / color file is missing, but now everywhere. "logo" (image) will be displayed with color: 
source share