1

I currently have a UITableViewController that displays information I would like to have the option of saving as a PDF and printing later.

Currently I have the following function, but I cannot figure out where it is saving the file, hw can I make this easily accessible from the Files App under Downloads:

   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)
    }
John
  • 965
  • 8
  • 16
  • you can't. The downloads folder is located outside your app bundle. You can allow the user to select which folder he wants to save it but you can not save anything outside your app bundle. – Leo Dabus Feb 02 '20 at 06:29
  • possible duplicate of https://stackoverflow.com/a/46457518/2303865 – Leo Dabus Feb 02 '20 at 06:29
  • How can I access this file written inside my apps bundle, I can’t seem to find it anywhere? I appreciate your help – John Feb 02 '20 at 15:12

0 Answers0