How to convert UIView to multi-page PDF file in swift?

I have an application written in Swift and you want to export the entire UIView as a PDF. I found that the code below does it well, but only produces a single-page, cropped PDF. The exported UIView contains a table view that is dynamic, so I need to calculate the number of pages depending on the length of the UIView (which contains scrollView for this purpose). Any help would be greatly appreciated.

Thanks!

 func createPDFFromView(aView: UIView, saveToDocumentsWithFileName fileName: String) { let pdfData = NSMutableData() UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil) UIGraphicsBeginPDFPage() guard let pdfContext = UIGraphicsGetCurrentContext() else { return } aView.layer.renderInContext(pdfContext) UIGraphicsEndPDFContext() if let documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first { let documentsFileName = documentDirectories + "/" + fileName debugPrint(documentsFileName) pdfData.writeToFile(documentsFileName, atomically: true) } } 
+6
source share

All Articles