extension UITableView {
public func generateToPDF() -> Data? {
let priorBounds = self.bounds
let fittedSize = self.sizeThatFits(CGSize(width:priorBounds.size.width, height: self.contentSize.height))
self.bounds = CGRect(x:0, y: 0, width:fittedSize.width, height:fittedSize.height)
let pdfPageBounds = CGRect(x:0, y: 0, width: self.frame.width, height:self.frame.height)
let pdfData = NSMutableData()
var pageOriginY: CGFloat = 0
UIGraphicsBeginPDFContextToData(pdfData, pdfPageBounds, nil)
while pageOriginY < fittedSize.height {
UIGraphicsBeginPDFPageWithInfo(pdfPageBounds, nil)
UIGraphicsGetCurrentContext()!.saveGState()
UIGraphicsGetCurrentContext()!.translateBy(x: 0, y: -pageOriginY)
self.layer.render(in: UIGraphicsGetCurrentContext()!)
UIGraphicsGetCurrentContext()!.restoreGState()
pageOriginY += pdfPageBounds.size.height
}
UIGraphicsEndPDFContext()
self.bounds = priorBounds
return pdfData.copy() as? Data
}
}