设置NSView以在macOs下打印

时间:2018-02-02 09:34:15

标签: swift macos printing nsview

我设法设置了一个允许我从我的macOs app打印出一个列表的功能。 我已经设置了一个包含HTML

的字符串的函数
   func makePDF(markup: String, amount: Int) {
    let directoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
    let printOpts: [NSPrintInfo.AttributeKey: Any] = [NSPrintInfo.AttributeKey.jobDisposition: NSPrintInfo.JobDisposition.save, NSPrintInfo.AttributeKey.jobSavingURL: directoryURL]
    let printInfo = NSPrintInfo(dictionary: printOpts)
    printInfo.horizontalPagination = NSPrintInfo.PaginationMode.autoPagination
    printInfo.verticalPagination = NSPrintInfo.PaginationMode.autoPagination
    printInfo.topMargin = 20.0
    printInfo.leftMargin = 20.0
    printInfo.rightMargin = 20.0
    printInfo.bottomMargin = 20.0

    let view = NSView(frame: NSRect(x: 0, y: 0, width: 560, height: (60 + 36 * amount)))

    if let htmlData = markup.data(using: String.Encoding.utf8) {
        let attrStr = NSAttributedString(html: htmlData, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:
            String.Encoding.utf8.rawValue], documentAttributes: nil)

        let frameRect = NSRect(x: 0, y: 0, width: 560, height: (60 + 36 * amount))
        let textField = NSTextField(frame: frameRect)
        textField.attributedStringValue = attrStr!
        view.addSubview(textField)

        let printOperation = NSPrintOperation(view: view, printInfo: printInfo)
        printOperation.showsPrintPanel = true
        printOperation.showsProgressPanel = true
        printOperation.run()

    }
}

但是我在设置正确的页面和设置页面方面遇到了麻烦。另一件事是我没有设法使用带边框的HTML(忽略border属性)

是否有一些最佳做法可以设置一个适合下一页没有溢出边框的打印页面(US-Letter或DIN A4)?

如何设法在我的HTML表格中打印出边框?

由于

0 个答案:

没有答案