I have an app, written in Swift and want to export an entire UIView
as a PDF. I found the code below does this well, but only produces a single-paged, cropped PDF. The exportable UIView
contains a tableview which is dynamic, so I need to calculate the number of pages depending on the length of the UIView
(which contains a scrollView for this purpose). Any help would be massively 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)
}
}