Reputation: 4738
I have a UIScrollView which I want to export to a PDF file. The scroll view contains another view (content view) which actually contains the visible content:
scrollView : UIScrollView
-> contentView : UIView
-> view1 : UIView
...
-> view2 : UIView
...
...
The code for the PDF export is:
func generatePDFData() -> NSData {
// I first tried to use the scroll view but the result was the same
let bounds = contentView.bounds
let pdfData = NSMutableData()
UIGraphicsBeginPDFContextToData(pdfData, bounds, nil)
let pdfContext = UIGraphicsGetCurrentContext()
CGContextSetInterpolationQuality(pdfContext, kCGInterpolationMedium)
CGContextSetRenderingIntent(pdfContext, kCGRenderingIntentDefault)
CGPDFContextBeginPage(pdfContext, nil)
// without the following two settings, the result remains the same
CGContextScaleCTM(pdfContext,1.0,-1.0)
CGContextTranslateCTM(pdfContext,0.0,-bounds.size.height)
contentView.layer.renderInContext(pdfContext)
UIGraphicsEndPDFContext()
return pdfData
}
I then use the data to generate a PDF file. The resulting file has a size of about 120 KB, so I expect it to contain some stuff (the view does not comprise any images). However, when I try to open it, I just get a blank page.
Upvotes: 1
Views: 567
Reputation: 10286
It looks like your only problem is beginning new pdf page in a context. Replace CGPDFContextBeginPage(pdfContext, nil)
with UIGraphicsBeginPDFPage()
and everything should work.
Upvotes: 1
Reputation: 4176
Use
contentView.drawViewHierarchyInRect(bounds, afterScreenUpdates: false)
Upvotes: 0