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


Print Preview



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.



Print sheet



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.










share|improve this question






















  • 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















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


Print Preview



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.



Print sheet



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.










share|improve this question






















  • 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













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


Print Preview



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.



Print sheet



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.










share|improve this question













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


Print Preview



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.



Print sheet



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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 16:56









Joseph Williamson

335314




335314












  • 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


















  • 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
















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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

How to pass form data using jquery Ajax to insert data in database?

National Museum of Racing and Hall of Fame

Guess what letter conforming each word