Generate PDF with Swift

前端 未结 1 1668
渐次进展
渐次进展 2021-01-30 11:59

I would like to create a PDF from a UITableView in Swift. Ill found some tutorials for Objective C, and tried it out but there is still no file generated by this code.



        
相关标签:
1条回答
  • 2021-01-30 12:40

    Here is my approach. Use a template view. It has all the static texts and images. And then for every page in pdf use this template view. Here is the function that I use:

    func renderAsPDF(demandEntry: ParsedDemandEntry, inView view: UIView) -> NSData? {
        let entries = demandEntry.demands
        let pageCount = Int(ceil(Double(entries.count) / Double(demandCountForPage)))
        if pageCount != 0 {
            let views = (1...pageCount).map { (pageNumber: Int) -> UIView in
                let pdfPageView = createTemplatePageViewWithParsedEntry(demandEntry, inView: view)
    
                let pageRange = ((pageNumber - 1) * demandCountForPage)..<(min(pageNumber * demandCountForPage, entries.count))
                let entriesForPage = Array(entries[pageRange])
    
                addEntries(entriesForPage, toView: pdfPageView)
    
                pdfPageView.removeFromSuperview()
    
                return pdfPageView
            }
    
            return toPDF(views)
        } else {
            return nil
        }
    }
    

    The ParsedDemandEntry is my model object. The view parameter is a container view to prepare pdf view in it. This is necessary because I use auto layout to position all labels in pdf view. Without a super view layout process won't work.

    Let's walk into function. First I get entries from model object. Think these are rows that needs to populate in pdf. After that I calculate how many pages I need. Then the loop begins. In every loop I create a tamplate view. (pdfPageView). And then fill it with entries. (addEntries(_:toView:) function call).

    After the loop I give all views to toPDF function. It creates NSData that represents pdf. Here is how toPDF function look like:

    private func toPDF(views: [UIView]) -> NSData? {
    
        if views.isEmpty {
            return nil
        }
    
        let pdfData = NSMutableData()
        UIGraphicsBeginPDFContextToData(pdfData, CGRect(x: 0, y: 0, width: 612, height: 792), nil)
    
        let context = UIGraphicsGetCurrentContext()
    
        for view in views {
            UIGraphicsBeginPDFPage()
            view.layer.renderInContext(context)
        }
    
        UIGraphicsEndPDFContext()
    
        return pdfData
    }
    

    It is fairly simple. First I create pdf context. And then loop through views array. For each view It renders view into pdf context.

    I hope this will help.

    0 讨论(0)
提交回复
热议问题