-
Add your UIViewController which has your TableView with your custom design .
-
Connect your TableView & Add Delegate.
-
In file viewController inherit UIDocumentInteractionControllerDelegate & Add delegate method :
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController { return self }
-
Add button to test create pdf of your table view
-
Add this method for used with your file name :
func pdfDataWithTableView(tableView: UITableView)-> String { 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 tableView.contentOffset = CGPoint(x: 0, y: pageOriginY) // move "renderer" } UIGraphicsEndPDFContext() tableView.bounds = priorBounds var docURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL docURL = docURL.appendingPathComponent("Report.pdf") pdfData.write(to: docURL as URL, atomically: true) return docURL.path }
-
in Your button action call this & send your tableView wil represent your pdf :
let pdf_file = UIDocumentInteractionController(url: URL(fileURLWithPath: self.pdfDataWithTableView(tableView: self.tableView))) pdf_file.delegate = self pdf_file.presentPreview(animated: true)
-
Notifications
You must be signed in to change notification settings - Fork 0
AyaBaghdadi/PDF_from_UITableView
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
About
No description or website provided.
Topics
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published