Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create pdf of tableview all data

I have create pdf of tableview all cells. but i have create the pdf it does not show all data of tableview & also when pdf page height set according to cells data. I dont know how to resolve this issue. I am using this code for create pdf.please help me.

  let _: NSData!
        do {
            let priorBounds = table_view.bounds
            let fittedSize = table_view.sizeThatFits(CGSizeMake(priorBounds.size.width, .infinity))
            table_view.bounds = CGRectMake(0, 0, fittedSize.width, fittedSize.height)
            let pdfPageBounds = CGRectMake(0, 0, 612, 800)
            let pdfData = NSMutableData()
            UIGraphicsBeginPDFContextToData(pdfData, pdfPageBounds, nil)
            do {
                var pageOriginY = 0
                while pageOriginY < Int(fittedSize.height)  {
                    UIGraphicsBeginPDFPageWithInfo(pdfPageBounds, nil)
                    CGContextSaveGState(UIGraphicsGetCurrentContext()!)
                    do {
                        CGContextTranslateCTM(UIGraphicsGetCurrentContext()!, 0, -CGFloat(pageOriginY))
                        table_view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
                    }
                    CGContextRestoreGState(UIGraphicsGetCurrentContext()!)
                    pageOriginY += Int(pdfPageBounds.size.height)
                }
            }
            UIGraphicsEndPDFContext()
            table_view.bounds = priorBounds
            if let documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first {
                let documentsFileName = documentDirectories + "/" + (filename as String)
                debugPrint(documentsFileName)
                pdfData.writeToFile(documentsFileName, atomically: true)
            }
        }
like image 859
Gaurav Gupta Avatar asked Nov 17 '16 05:11

Gaurav Gupta


1 Answers

Try this.

func createPdfFromTableView()
{
    let priorBounds: CGRect = self.tblView.bounds
    let fittedSize: CGSize = self.tblView.sizeThatFits(CGSize(width: priorBounds.size.width, height: self.tblView.contentSize.height))
    self.tblView.bounds = CGRect(x: 0, y: 0, width: fittedSize.width, height: fittedSize.height)
    self.tblView.reloadData()
    let pdfPageBounds: CGRect = CGRect(x: 0, y: 0, width: fittedSize.width, height: (fittedSize.height))
    let pdfData: NSMutableData = NSMutableData()
    UIGraphicsBeginPDFContextToData(pdfData, pdfPageBounds, nil)
    UIGraphicsBeginPDFPageWithInfo(pdfPageBounds, nil)
    self.tblView.layer.render(in: UIGraphicsGetCurrentContext()!)
    UIGraphicsEndPDFContext()
    let documentDirectories = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
    let documentsFileName = documentDirectories! + "/" + "pdfName"
    pdfData.write(toFile: documentsFileName, atomically: true)
    print(documentsFileName)
}
like image 87
RajeshKumar R Avatar answered Nov 07 '22 02:11

RajeshKumar R