Lưu ý: Tôi đang làm việc với Swift 4 cho macOS.In nội dung chế độ xem web có tiêu đề tĩnh
Tôi có một số NSTableView
có thể được lấp đầy dữ liệu và được lưu vào Dữ liệu chính. Tôi muốn hiển thị các giá trị TableView với "thiết kế".
tôi đã nhận ra nó như thế này:
- tạo một template html
- lấy mã html như là một chuỗi và thay thế "placeholders html" với tableview tôi đánh giá cao
- hiển thị chuỗi html sửa đổi qua Web Xem
Hoạt động tốt! Và điều này "giải pháp" sẽ làm tự động ngắt trang :)
Dưới đây là một chút mã của tôi
// 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 {}
}
và tôi có thể in này:
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()
}
Nhưng có một vấn đề: - Tôi muốn có "Thông báo-tiêu đề" màu đỏ sau mỗi lần ngắt trang một lần nữa trên đầu trang
Có ý tưởng nào không, làm cách nào tôi có thể nhận ra điều đó?
CẬP NHẬT
Có lẽ phương pháp này xem web có thể giúp?
func webView(_ sender: WebView!, drawFooterIn rect: NSRect) {
// DO SOMETHING
}
func webView(_ sender: WebView!, drawHeaderIn rect: NSRect) {
// DO SOMETHING
}