Draw tables to a PDF

Hi, I need to bring some tables to paper. I found the outdated tutorial " How To Create a PDF with Quartz 2D in iOS 5" Part1|Part2 and I am wondering if it is still the way to go in terms of building up a table? I know there is a updated guide but"how to build a table" is not covered

@cranimax Thanks very much for your question!

When you say ā€œbuilding up a tableā€, are you trying to print a UITableView to pdf?

I found the following example code that may help:

extension UIView {
    
    // Export pdf from Save pdf in drectory and return pdf file path
    func exportAsPdfFromView() -> String {
        
        let pdfPageFrame = self.bounds
        let pdfData = NSMutableData()
        UIGraphicsBeginPDFContextToData(pdfData, pdfPageFrame, nil)
        UIGraphicsBeginPDFPageWithInfo(pdfPageFrame, nil)
        guard let pdfContext = UIGraphicsGetCurrentContext() else { return "" }
        self.layer.render(in: pdfContext)
        UIGraphicsEndPDFContext()
        return self.saveViewPdf(data: pdfData)
        
    }
    
    // Save pdf file in document directory
    func saveViewPdf(data: NSMutableData) -> String {
        
        let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        let docDirectoryPath = paths[0]
        let pdfPath = docDirectoryPath.appendingPathComponent("viewPdf.pdf")
        if data.write(to: pdfPath, atomically: true) {
            return pdfPath.path
        } else {
            return ""
        }
    }
}

Now using above extension you can export to PDF of any UIView and it will save PDF file in directory and return itā€™s path.

I hope this helps :slight_smile:

All the best.

This topic was automatically closed after 166 days. New replies are no longer allowed.