1

How can I generate tableView data as PDF and send through email? I already tried with below code, but I didn't get anything.I want export tableView Data in the format as PDF with email. Can anyone help me ?

   func pdfDataWithTableView(tableView: UITableView) {
    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: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
    }
    UIGraphicsEndPDFContext()
    tableView.bounds = priorBounds
    var docURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL
    docURL = docURL.appendingPathComponent("myDocument.pdf")
    pdfData.write(to: docURL as URL, atomically: true)
    sendMail()
}
Abdul Hoque Nuri
  • 1,105
  • 1
  • 9
  • 18
  • I don't think your code will be able to generate whole pdf data from tableview, as I see its taking snapshots and saving them, for that if all the cells aren't dequeued you wont be able to get the whole table list. I guess better would be to use Core Graphics to create your layout and fill up your pdf table with your datasource model. – Amber K Dec 21 '18 at 05:13
  • how can i get whole tableview data as PDF and every row and column i want to separate it ?.can you help with code ? – Vadlapalli Masthan Dec 21 '18 at 05:15
  • I already gave you the way, don't expect me to do your work? – Amber K Dec 21 '18 at 05:17

0 Answers0