I am trying to create a PDF of all the text the user enters into the text field of my OS X Cocoa app (swift 3). Unfortunately, I am running into some issues.
Here's the code I'm using:
do {
// set rect to the text view bounds
let rect: NSRect = self.textview.bounds
// search for a suitable location to save file
if let dir = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true).first {
// if we find that spot, add the file name and prepare to write it to there.
let path = NSURL(fileURLWithPath: dir).appendingPathComponent("File.pdf")
// Write that PDF to that path.
try self.view.dataWithPDF(inside: rect).write(to: path!)
} else {
// this never happens
print("uh-uh.")
}
} catch _ {
// this never happens either
print("something went wrong.")
}
This successfully saves the PDF, but what fails is the look of the PDF. The PDF is all one page (how do I make it paginated to letter size?) and only the part of the NSTextView showing to the user becomes part of the PDF, not the whole text view (as needs to happen).
Here's what the resulting PDF looks like:
I want the saved PDF to have the full content of the text view instead of only a part of it, and I need it to be paginated. How can I do that?
EDIT 1: I found this article but it's all Objective-C (and very old too) so that's not so helpful.
EDIT 2: Willeke's comment helped me get the entire text view into the PDF, but it's still only one page. How can I paginate the PDF I create?