10
注:我正在使用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
}