Notice: I working with swift 4 for osx. I would like to generate a pdf file from a WebView.
At the moment my WebView load a html-string and show this successfully. If I press a button, the print panel will open and is ready to print my WebView content 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
//webView.mainFrame.frameView.printOperation(with: printInfo).run()
let printOp: NSPrintOperation = NSPrintOperation(view: webView.mainFrame.frameView.documentView, printInfo: printInfo)
printOp.showsPrintPanel = true
printOp.showsProgressPanel = false
printOp.run()
}
Now I would like to have another button, which save 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 horrible:
Have anybody an idea?
UPDATE This is a part of my web view result:
and that is the result of my print preview / pdf file /the color is missing, but now everywhere. the "logo"(picture) will show with color:



