Print a web view content with a static header

Viewed 668

Note: I am working with with Swift 4 for macOS.

I have an NSTableView which can be filled with data and saved into Core Data. I would like to display the TableView values with a "design".

I have realized it like this:

  • created a html template
  • get the html code as a string and replaces the "html placeholders" with my tableview values
  • show the modified html string via Web View

enter image description here

Works good ! And this "solution" will do automatically page breaks :)

Here a little bit of my code

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

And I can print this:

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()
    } 

But there is a problem: - I would like to have the red "Notice-header" after each page break again on the top

Have anybody an idea, how I can realize that?

UPDATE

Maybe this methods of web view could helps?

func webView(_ sender: WebView!, drawFooterIn rect: NSRect) {
   // DO SOMETHING
}

func webView(_ sender: WebView!, drawHeaderIn rect: NSRect) {
   // DO SOMETHING
}
1 Answers

From what I can see you're not specifying to use a header for the pages, but you're just displaying the notice once.

You might want to set the nsprintinfo.headerandfooter attribute to define that a header should be printed, and then move the header text/display text (NOTICE etc.) into that section.

Not sure if the NSView will be beneficial for you here... there's a good section on printing here: Printing Programming Guide for Mac

Related