2017-10-04 208 views
10

注:我正在使用Swift 4 for macOS。使用靜態標題打印網頁查看內容

我有一個NSTableView它可以填充數據並保存到核心數據。 我想用「設計」來顯示TableView值。

我已經意識到這樣的:

  • 創建的HTML模板
  • 得到HTML代碼作爲字符串和替換「HTML佔位符」與我的tableview值
  • 顯示修改後的HTML字符串通過Web瀏覽

enter image description here

工程很好!而這個「解決方案」會自動完成分頁符:)

這裏我的代碼

// 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 
} 

回答

0

從我所看到的你不指定要使用的標頭的頁面,但你只顯示一次通知。

您可能需要設置nsprintinfo.headerandfooter屬性以定義應打印標題,然後將標題文本/顯示文本(注意等)移動到該部分中。

不知道,如果NSView會爲你在這裏有利的...有一個很好的部分上打印的位置:Printing Programming Guide for Mac