1

I have the following lines of code to print the webView content.

let directoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let printOpts: NSDictionary = [NSPrintJobDisposition: NSPrintSaveJob, NSPrintSaveJob: directoryURL, NSPrintScalingFactor: 2.0]
let printInfo = NSPrintInfo(dictionary: printOpts as! [String : AnyObject])

printInfo.horizontalPagination = .autoPagination
printInfo.verticalPagination = .autoPagination

let printOperation = NSPrintOperation(view: webView.mainFrame.frameView, printInfo: printInfo)
printOperation.printPanel.options = [.showsOrientation, .showsPaperSize, .showsPreview, .showsPageSetupAccessory, .showsScaling, .showsPrintSelection]
printOperation.run()

The problem is that it sometimes causes the application to crash when the user interacts with the print operation panel.

enter image description here

If I just click on the cancel button, the application can crash. And if I try to set a different scale rate, it can sometimes crash. I don't know why the application will crash. It's always the last line. What am I doing wrong? Thanks.

enter image description here

El Tomato
  • 6,479
  • 6
  • 46
  • 75

2 Answers2

1

There's a bug in the print panel code when .showsCopies is not in the options. It has a use-after-free bug with the Copies-related subviews. It removes them from the view hierarchy, allowing them to be deallocated. However, it keeps a dangling reference to the text field and tries to operate on it in various circumstances. For example, it sometimes tries to make it the first responder.

You basically should just never try to suppress the Copies field.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • Thanks. So you mean, I just need to include showsCopies in the options dictionary? If I ask the G man about 'showsCopies NSPrintOperation, ' I find no literature. – El Tomato Jun 16 '17 at 06:22
  • The application still crashes with 'showsCopies,' occasionally. – El Tomato Jun 16 '17 at 06:29
  • Sorry, had a typo which I've now fixed. It's `.showsCopies` with three "s"s. https://developer.apple.com/documentation/appkit/nsprintpanel.options/1490535-showscopies – Ken Thomases Jun 16 '17 at 06:29
  • There might be other similar bugs. Try just not setting the options, so you use the defaults which Apple has presumably tested more thoroughly. – Ken Thomases Jun 16 '17 at 06:31
  • Thanks. Is there a different approach such that I get access to showsScaling? – El Tomato Jun 16 '17 at 06:32
  • 1
    Well, first see if the crashing stops with the default options. If it does, try, instead of building the options set from scratch, retrieving the default options and just adding `.showsScaling`. – Ken Thomases Jun 16 '17 at 06:34
0

my code for print a pdf from URL for OS X:

let printInfo = NSPrintInfo.shared
let manager = FileManager.default
do{
    let directoryURL = try manager.url(for: .documentDirectory, in:.userDomainMask, appropriateFor:nil, create:true)
    let docURL = NSURL(string:"XX.pdf", relativeTo:directoryURL)

    let pdfDoc =  PDFDocument.init(url: docURL! as URL)

    let page = CGRect(x: 0, y: 0, width: 595.2, height: 1841.8) // A4, 72 dpi

    let pdfView : PDFView = PDFView.init(frame: page)

    pdfView.document = pdfDoc

    let operation: NSPrintOperation = NSPrintOperation(view: pdfView, printInfo: printInfo)
    operation.printPanel.options.insert(NSPrintPanel.Options.showsPaperSize)
    operation.printPanel.options.insert(NSPrintPanel.Options.showsOrientation)

    operation.run()
}catch{

}
Osman
  • 1,496
  • 18
  • 22