I am trying to create PDF from UITableView for sharing data to outside.
Problem is what the code I have used is working great on device version below iOS 13 but on version above 13 it only render data which is on screen. Now I am expecting to render whole UITableView for version above 13.x. If anyone have suggestions please let me know.
Thanks in advance :)
Here is my Code:
func createPDFDataFromTableView(tableView: UITableView) -> NSURL {
let priorBounds = tableView.bounds
let fittedSize = tableView.sizeThatFits(CGSize(width:priorBounds.size.width, height:tableView.contentSize.height))
tableView.bounds = CGRect(x:0, y:0, width:fittedSize.width, height:fittedSize.height)
let pdfPageBounds = CGRect(x:0, y:0, width:tableView.frame.width,height:tableView.contentSize.height) //height:self.view.frame.height)
let pdfData = NSMutableData()
UIGraphicsBeginPDFContextToData(pdfData, pdfPageBounds,nil)
var pageOriginY: CGFloat = 0
while pageOriginY < fittedSize.height {
UIGraphicsBeginPDFPageWithInfo(pdfPageBounds, nil)
UIGraphicsGetCurrentContext()!.saveGState()
UIGraphicsGetCurrentContext()!.translateBy(x: 0, y: -pageOriginY)
tableView.layer.render(in: UIGraphicsGetCurrentContext()!)
UIGraphicsGetCurrentContext()!.restoreGState()
pageOriginY += pdfPageBounds.size.height
print(pageOriginY)
}
UIGraphicsEndPDFContext()
tableView.bounds = priorBounds
//try saving in doc dir to confirm:
let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last
self.ticksnew = getCurrentDate()
if annDetail.announTitle != nil
{
FileNameToDelete=annDetail.announTitle
}
let fileName:String!="\(FileNameToDelete).pdf"
let path = dir?.appendingPathComponent(fileName!)
do {
try pdfData.write(to: path!, options: NSData.WritingOptions.atomic)
} catch {
print("error catched")
}
return path! as NSURL
}