Does the PDF contain only the last page of text? That's what I expect to see from the code you showed.
You are creating a new context each time you go through the for loop.
let context = CGContext(consumer: consumer, mediaBox: &mediaBox, nil)!
When you call beginPDFPage
and endPDFPage
to create a new PDF page, you end up adding only one page to the context because there's a new context each time you go through the loop. Instead of creating X number of pages, you create X number of contexts with one page each.
Declare the context before the for loop. Now the new pages you add inside the for loop will be added to the same context.
You should also call closePDF
after the for loop. With your current code, you're saving a PDF every time through the loop. Wait until you create all the pages before saving the PDF.
There may be other problems with the code. If you are still getting only a single page, set a breakpoint and step through the code line by line, making sure the variables contain the data you expect. The following article may help you if you still have problems:
Creating PDFs with Core Text and Quartz