The easiest way to convert the contents of a scroll view to a PDF file is to have the content view as the first subview of the scroll view and use this view to create a snapshot of the scroll view. The code in Swift will look like this:
func generatePDFdata(withView view: UIView) -> NSData { let pageDimensions = view.bounds let outputData = NSMutableData() UIGraphicsBeginPDFContextToData(outputData, pageDimensions, nil) if let context = UIGraphicsGetCurrentContext() { UIGraphicsBeginPDFPage() view.layer.render(in: context) } UIGraphicsEndPDFContext() return outputData }
Then you can use outputData to write to the PDF file:
outputData.write(to: pdfFileUrl, atomically: true)
source share