Getting Your View to Generate PDF Data

All the drawing commands can be converted into PDF by the AppKit framework. The PDF data can be sent to a printer or to a file. Note that a PDF will always look as good as possible on any device, because it is resolution independent.

You have already created a view that knows how to generate PDF data to describe how it is supposed to look. Getting the PDF data into a file is really quite easy. NSView has the following method:

f​u​n​c​ ​d​a​t​a​W​i​t​h​P​D​F​I​n​s​i​d​e​R​e​c​t​(​r​e​c​t​:​ ​N​S​R​e​c​t​)​ ​-​>​ ​N​S​D​a​t​a​

This method creates a data object and then calls drawRect(_:). The drawing commands that would usually go to the screen instead go into the data object. Once you have ...

Get Cocoa Programming for OS X: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.