0

Im still new to OSX / Swift 3.1 but Im a little stumped, Ive build an epos system and its work all fine, Im just stuck with printing out the receipt.

I have build a XIB View and the corresponding swift file for it show below

    class Receipt: NSView {

    let currentOrder = NewOrder.instance

    @IBOutlet var view: NSView!

    @IBOutlet weak var facebookString: NSTextField!

    @IBOutlet weak var websiteString: NSTextField!

    @IBOutlet weak var totalString: NSTextField!

    @IBOutlet weak var vatString: NSTextField!

    @IBOutlet weak var subTotalString: NSTextField!

    @IBOutlet weak var CashierName: NSTextField!

    @IBOutlet weak var orderNumber: NSTextField!

    @IBOutlet weak var date: NSTextField!

    @IBOutlet weak var itemList: NSTextField!

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)

        Bundle.main.loadNibNamed("ReceiptView", owner: self, topLevelObjects: nil)

        let contentFrame = NSMakeRect(0, 0, frame.size.width, frame.size.height)
        self.view.frame = contentFrame
        self.addSubview(self.view)
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)
        subTotalString.stringValue = "Hello World"
        view.draw(dirtyRect)
        subTotalString.draw(dirtyRect)
    }

    }

I have added the nib name to the XIB reference and loaded a new Receipt() class and added the view to the NSPrintOperation.

            let order = NewOrder.instance

        let sview = Receipt()

        let printer = NSPrinter(name: "EPSON TM-T88V");
        let printData = NSPrintInfo()
            printData.printer = printer!
        let printerName = printer?.name
        let docSize = NSMakeSize(CGFloat(204.0), CGFloat(200));
        let res = "180x180dpi";
        let speed = "Auto";
        let blank = "Off";
        let paperCut = "DocFeedCut";
        let chashDrwr1 = "After";
        let chashDrwr2 = "Off";
        let buzzerControl = "Off";
        let buzzerPattern = "Internal";
        let buzzerRepeat = "1";

        let data = TMPrintSupport.tmSetJobTicket(printData, printerName: printerName, documentSize: docSize, resolution: res, speed: speed, blank: blank, paperCut: paperCut, chashDrwr1: chashDrwr1, chashDrwr2: chashDrwr2, buzzerControl: buzzerControl, buzzerPattern: buzzerPattern, buzzerRepeat: buzzerRepeat)

        let po = NSPrintOperation(view: sview.view, printInfo: printData)
    //        po.showsPrintPanel = false
        po.canSpawnSeparateThread = true
        po.cleanUp()

        return po.run()

When the print view is shown it is blank and does not contain the view and is blank, I've also test printed and the print is just blank.

Any suggestions welcome, thanks

  • You've got a bunch of code here that doesn't seem to be used at all, like the "TMPrintSupport" (some custom Epson thing?), and most of the "let"s above it. – Ssswift Jun 25 '17 at 17:52
  • 1
    You're also doing some pretty strange things, like subclassing NSView and loading a xib from inside its init(), setting selfframe directly, and passing dirtyRect to the draw() method of subviews (when it's not clear their frame will always be the same as yours). You're defining `init(NSRect)` and `init(NSCoder)` but then calling `init()` -- what do you expect to happen here? – Ssswift Jun 25 '17 at 17:56

0 Answers0