Swift Cannot print from NSView “CGContextGetCTM: invalid context 0x0”
up vote
1
down vote
favorite
I'm making a macOS app at the moment, and I'm trying to support printing. I've added NSBox to represent the pages I want to print inside an NSScrollView, so the hierarchy looks like this
NSScrollView
|- NSDocumentView
|- NSBox
|- NSBox

In the ViewController, when the user clicks on print, I run the following code
let printOperation = NSPrintOperation(view: self.previewView, printInfo: printInfo)
if let window = self.view.window {
printOperation.runModal(for: window, delegate: self, didRun: #selector(PrintPreviewViewController.printOperationDidRun(_:success:contextInfo:)), contextInfo: nil)
}
In the NSDocumentView subclass, I've implemented
override func knowsPageRange(_ range: NSRangePointer) -> Bool {
var numberOfPages = 0
for i in 0..<pages.count {
numberOfPages += pages[i].count
}
range.pointee.location = 1
range.pointee.length = numberOfPages
return true
}
override func rectForPage(_ page: Int) -> NSRect {
var pageNumber = 1
for i in 0..<pages.count {
for j in 0..<pages[i].count {
if pageNumber == page {
return pages[i][j].frame
}
pageNumber += 1
}
}
return NSRect(x: 0, y: 0, width: 300, height: 300)
}
When I run, I see the print sheet that all looks correct.

But when I finally confirm print, I get these errors in the console, and a blank sheet.
CGContextGetCTM: invalid context 0x0
CGContextTranslateCTM: invalid context 0x0
I don't know what this means, as everything is displayed correctly inside the NSScrollView, and even the print preview.
I tried to narrow down the issue, I tried making a blank project with just one NSBox inside an NSScrollView, and I was having the same issue. I tried changing the size of NSRect bering returned in rectForPage(), but nothing works. I'm out of ideas.
Thank you all in advance.
swift macos core-graphics nsview nsprintoperation
add a comment |
up vote
1
down vote
favorite
I'm making a macOS app at the moment, and I'm trying to support printing. I've added NSBox to represent the pages I want to print inside an NSScrollView, so the hierarchy looks like this
NSScrollView
|- NSDocumentView
|- NSBox
|- NSBox

In the ViewController, when the user clicks on print, I run the following code
let printOperation = NSPrintOperation(view: self.previewView, printInfo: printInfo)
if let window = self.view.window {
printOperation.runModal(for: window, delegate: self, didRun: #selector(PrintPreviewViewController.printOperationDidRun(_:success:contextInfo:)), contextInfo: nil)
}
In the NSDocumentView subclass, I've implemented
override func knowsPageRange(_ range: NSRangePointer) -> Bool {
var numberOfPages = 0
for i in 0..<pages.count {
numberOfPages += pages[i].count
}
range.pointee.location = 1
range.pointee.length = numberOfPages
return true
}
override func rectForPage(_ page: Int) -> NSRect {
var pageNumber = 1
for i in 0..<pages.count {
for j in 0..<pages[i].count {
if pageNumber == page {
return pages[i][j].frame
}
pageNumber += 1
}
}
return NSRect(x: 0, y: 0, width: 300, height: 300)
}
When I run, I see the print sheet that all looks correct.

But when I finally confirm print, I get these errors in the console, and a blank sheet.
CGContextGetCTM: invalid context 0x0
CGContextTranslateCTM: invalid context 0x0
I don't know what this means, as everything is displayed correctly inside the NSScrollView, and even the print preview.
I tried to narrow down the issue, I tried making a blank project with just one NSBox inside an NSScrollView, and I was having the same issue. I tried changing the size of NSRect bering returned in rectForPage(), but nothing works. I'm out of ideas.
Thank you all in advance.
swift macos core-graphics nsview nsprintoperation
Create a symbolic breakpoint onCGPostError. The stack trace when the breakpoint is hit might help you track down the problem.
– rob mayoff
Nov 9 at 17:03
@robmayoff thanks, I just tried it out, but the breakpoints were all inside of machine codes. I did set an environmental variable to get a backtrace, but it wasn't very helpful, didn't give me any errors to look out for.
– Joseph Williamson
Nov 9 at 17:08
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm making a macOS app at the moment, and I'm trying to support printing. I've added NSBox to represent the pages I want to print inside an NSScrollView, so the hierarchy looks like this
NSScrollView
|- NSDocumentView
|- NSBox
|- NSBox

In the ViewController, when the user clicks on print, I run the following code
let printOperation = NSPrintOperation(view: self.previewView, printInfo: printInfo)
if let window = self.view.window {
printOperation.runModal(for: window, delegate: self, didRun: #selector(PrintPreviewViewController.printOperationDidRun(_:success:contextInfo:)), contextInfo: nil)
}
In the NSDocumentView subclass, I've implemented
override func knowsPageRange(_ range: NSRangePointer) -> Bool {
var numberOfPages = 0
for i in 0..<pages.count {
numberOfPages += pages[i].count
}
range.pointee.location = 1
range.pointee.length = numberOfPages
return true
}
override func rectForPage(_ page: Int) -> NSRect {
var pageNumber = 1
for i in 0..<pages.count {
for j in 0..<pages[i].count {
if pageNumber == page {
return pages[i][j].frame
}
pageNumber += 1
}
}
return NSRect(x: 0, y: 0, width: 300, height: 300)
}
When I run, I see the print sheet that all looks correct.

But when I finally confirm print, I get these errors in the console, and a blank sheet.
CGContextGetCTM: invalid context 0x0
CGContextTranslateCTM: invalid context 0x0
I don't know what this means, as everything is displayed correctly inside the NSScrollView, and even the print preview.
I tried to narrow down the issue, I tried making a blank project with just one NSBox inside an NSScrollView, and I was having the same issue. I tried changing the size of NSRect bering returned in rectForPage(), but nothing works. I'm out of ideas.
Thank you all in advance.
swift macos core-graphics nsview nsprintoperation
I'm making a macOS app at the moment, and I'm trying to support printing. I've added NSBox to represent the pages I want to print inside an NSScrollView, so the hierarchy looks like this
NSScrollView
|- NSDocumentView
|- NSBox
|- NSBox

In the ViewController, when the user clicks on print, I run the following code
let printOperation = NSPrintOperation(view: self.previewView, printInfo: printInfo)
if let window = self.view.window {
printOperation.runModal(for: window, delegate: self, didRun: #selector(PrintPreviewViewController.printOperationDidRun(_:success:contextInfo:)), contextInfo: nil)
}
In the NSDocumentView subclass, I've implemented
override func knowsPageRange(_ range: NSRangePointer) -> Bool {
var numberOfPages = 0
for i in 0..<pages.count {
numberOfPages += pages[i].count
}
range.pointee.location = 1
range.pointee.length = numberOfPages
return true
}
override func rectForPage(_ page: Int) -> NSRect {
var pageNumber = 1
for i in 0..<pages.count {
for j in 0..<pages[i].count {
if pageNumber == page {
return pages[i][j].frame
}
pageNumber += 1
}
}
return NSRect(x: 0, y: 0, width: 300, height: 300)
}
When I run, I see the print sheet that all looks correct.

But when I finally confirm print, I get these errors in the console, and a blank sheet.
CGContextGetCTM: invalid context 0x0
CGContextTranslateCTM: invalid context 0x0
I don't know what this means, as everything is displayed correctly inside the NSScrollView, and even the print preview.
I tried to narrow down the issue, I tried making a blank project with just one NSBox inside an NSScrollView, and I was having the same issue. I tried changing the size of NSRect bering returned in rectForPage(), but nothing works. I'm out of ideas.
Thank you all in advance.
swift macos core-graphics nsview nsprintoperation
swift macos core-graphics nsview nsprintoperation
asked Nov 9 at 16:56
Joseph Williamson
335314
335314
Create a symbolic breakpoint onCGPostError. The stack trace when the breakpoint is hit might help you track down the problem.
– rob mayoff
Nov 9 at 17:03
@robmayoff thanks, I just tried it out, but the breakpoints were all inside of machine codes. I did set an environmental variable to get a backtrace, but it wasn't very helpful, didn't give me any errors to look out for.
– Joseph Williamson
Nov 9 at 17:08
add a comment |
Create a symbolic breakpoint onCGPostError. The stack trace when the breakpoint is hit might help you track down the problem.
– rob mayoff
Nov 9 at 17:03
@robmayoff thanks, I just tried it out, but the breakpoints were all inside of machine codes. I did set an environmental variable to get a backtrace, but it wasn't very helpful, didn't give me any errors to look out for.
– Joseph Williamson
Nov 9 at 17:08
Create a symbolic breakpoint on
CGPostError. The stack trace when the breakpoint is hit might help you track down the problem.– rob mayoff
Nov 9 at 17:03
Create a symbolic breakpoint on
CGPostError. The stack trace when the breakpoint is hit might help you track down the problem.– rob mayoff
Nov 9 at 17:03
@robmayoff thanks, I just tried it out, but the breakpoints were all inside of machine codes. I did set an environmental variable to get a backtrace, but it wasn't very helpful, didn't give me any errors to look out for.
– Joseph Williamson
Nov 9 at 17:08
@robmayoff thanks, I just tried it out, but the breakpoints were all inside of machine codes. I did set an environmental variable to get a backtrace, but it wasn't very helpful, didn't give me any errors to look out for.
– Joseph Williamson
Nov 9 at 17:08
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230160%2fswift-cannot-print-from-nsview-cgcontextgetctm-invalid-context-0x0%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Create a symbolic breakpoint on
CGPostError. The stack trace when the breakpoint is hit might help you track down the problem.– rob mayoff
Nov 9 at 17:03
@robmayoff thanks, I just tried it out, but the breakpoints were all inside of machine codes. I did set an environmental variable to get a backtrace, but it wasn't very helpful, didn't give me any errors to look out for.
– Joseph Williamson
Nov 9 at 17:08