使用静态标题打印网页查看内容
问题描述:
注:我正在使用Swift 4 for macOS。使用静态标题打印网页查看内容
我有一个NSTableView
它可以填充数据并保存到核心数据。 我想用“设计”来显示TableView值。
我已经意识到这样的:
- 创建的HTML模板
- 得到HTML代码作为字符串和替换“HTML占位符”与我的tableview值
- 显示修改后的HTML字符串通过Web浏览
工程很好!而这个“解决方案”会自动完成分页符:)
这里我的代码
// 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 {}
}
一点点,我可以打印此:
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()
}
但有一个问题: - 我想在每个分页后再次出现红色的“Notice-header”
有没有人有想法,我怎么能意识到这一点?
UPDATE
也许Web视图可能有助于这种方法呢?
func webView(_ sender: WebView!, drawFooterIn rect: NSRect) {
// DO SOMETHING
}
func webView(_ sender: WebView!, drawHeaderIn rect: NSRect) {
// DO SOMETHING
}
答
从我所看到的你不指定要使用的标头的页面,但你只显示一次通知。
您可能需要设置nsprintinfo.headerandfooter
属性以定义应打印标题,然后将标题文本/显示文本(注意等)移动到该部分中。
不知道,如果NSView
会为你在这里有利的...有一个很好的部分上打印的位置:Printing Programming Guide for Mac