Trying to add functionality to be able to air Print a UITableView

159 Views Asked by At

I have a (to-do style) list app. the data is laid out in a diffable dataSource tableview with custom cells. I have a functioning dropdown menu and I'm trying to add a print button so the user can print out the list if they'd like (seems standard in a lot of list apps).

So far I have the following in the button's action:

            let printController = UIPrintInteractionController.shared
            printController.delegate = self
            
            let printInfo = UIPrintInfo(dictionary: nil)
            printInfo.outputType = .general
            printInfo.jobName = "Print TableView"
            printController.printInfo = printInfo
            
            let tableViewPDF = self?.tableView.exportAsPdfFromTable()
            printController.printingItem = tableViewPDF
            
            let formatter = UIPrintFormatter()
            formatter.perPageContentInsets = UIEdgeInsets(top: 50, left: 50, bottom: 50, right: 50)
            printController.printFormatter = formatter
            
            printController.present(animated: true, completionHandler: nil)

and the delegate:

     extension ListTableViewController: UIPrintInteractionControllerDelegate {
  func printInteractionControllerParentViewController(_ printInteractionController: UIPrintInteractionController) -> UIViewController? {
    return self
   }
  }

As you can see I am trying to convert the tableview into a PDF and print that (something else might be better perhaps?). The .exportAsPdfFromTable() method on tableView is a UITableView extension as follows:

extension UITableView {

// Export pdf from UITableView and save pdf in drectory and return pdf file path
func exportAsPdfFromTable() -> String {
    
    let originalBounds = self.bounds
    self.bounds = CGRect(x:originalBounds.origin.x, y: originalBounds.origin.y, width: self.contentSize.width, height: self.contentSize.height)
    let pdfPageFrame = CGRect(x: 0, y: 0, width: self.bounds.size.width, height: self.contentSize.height)
    
    let pdfData = NSMutableData()
    UIGraphicsBeginPDFContextToData(pdfData, pdfPageFrame, nil)
    UIGraphicsBeginPDFPageWithInfo(pdfPageFrame, nil)
    guard let pdfContext = UIGraphicsGetCurrentContext() else { return "" }
    self.layer.render(in: pdfContext)
    UIGraphicsEndPDFContext()
    self.bounds = originalBounds
    // Save pdf data
    return self.saveTablePdf(data: pdfData)
}

// Save pdf file in document directory
func saveTablePdf(data: NSMutableData) -> String {
    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    let docDirectoryPath = paths[0]
    let pdfPath = docDirectoryPath.appendingPathComponent("tablePdf.pdf")
    if data.write(to: pdfPath, atomically: true) {
        return pdfPath.path
    } else {
        return ""
    }
}

}

The result is the print controller being presented with a blank page. Any ideas on what I'm missing?

0

There are 0 best solutions below