Gho*_*108 10 macos cocoa pdf-generation webview swift
注意:我正在使用Swift 4 for macOS.
我有一个NSTableView可以填充数据并保存到Core Data中.我想用"设计"显示TableView值.
我已经意识到这样:
效果很好!而这个"解决方案"将自动分页:)
这里有一点我的代码
// Example with statics values
func createHTML() {
let pathToHTMLTemplate = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("basic.html")
let pathToNoticeHTMLTemplate = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("notice.html")
do {
htmlString = try String(contentsOf: pathToInvoiceHTMLTemplate)
for _ in 0 ..< 40 {
var itemHTMLContent = try String(contentsOf: pathToNoticeHTMLTemplate)
itemHTMLContent = itemHTMLContent.replacingOccurrences(of: "#NOTICE_NAME#", with: "My notice")
htmlPositionen += itemHTMLContent
}
htmlString = htmlString.replacingOccurrences(of: "#NOTICE#", with: htmlPositionen)
let totalCount = 20 //Fix value as example
htmlString = htmlString.replacingOccurrences(of: "#TOTAL_COUNT#", with: "\(totalCount)")
myWebView.mainFrame.loadHTMLString(htmlString, baseURL: nil)
} catch {}
}
Run Code Online (Sandbox Code Playgroud)
我可以打印这个:
func webView(_ sender: WebView!, didFinishLoadFor frame: WebFrame!) {
let fileURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("test.pdf")
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
let printOp = NSPrintOperation(view: sender.mainFrame.frameView.documentView, printInfo: printInfo)
printOp.showsProgressPanel = false
printOp.showsPrintPanel = false
printOp.run()
printOp.cleanUp()
}
Run Code Online (Sandbox Code Playgroud)
但是有一个问题: - 我希望在每个页面中断之后再次显示红色的"Notice-header"
有人有想法,我怎么能意识到这一点?
UPDATE
也许这种网络视图方法可以帮助?
func webView(_ sender: WebView!, drawFooterIn rect: NSRect) {
// DO SOMETHING
}
func webView(_ sender: WebView!, drawHeaderIn rect: NSRect) {
// DO SOMETHING
}
Run Code Online (Sandbox Code Playgroud)
据我所知,您没有指定使用页面标题,但您只是显示一次通知。
您可能需要设置nsprintinfo.headerandfooter属性来定义应打印标题,然后将标题文本/显示文本(注意等)移动到该部分。
不确定这是否NSView对您有益...这里有一个关于打印的很好的部分:Mac 打印编程指南