【问题标题】:Generate multipage PDF UIView生成多页 PDF UIView
【发布时间】:2019-09-03 09:27:42
【问题描述】:

我想创建self.view 的多页PDF。我关注viewControllertableView 现在我想创建视图的pdf。

这是我所做的,但它只创建单页 pdf

func pdfDataWithTableView(tableView: UITableView) {
    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
    }

    UIGraphicsEndPDFContext()
    tableView.bounds = priorBounds
    var docURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL
    docURL = docURL.appendingPathComponent("myPassBook.pdf")
    pdfData.write(to: docURL as URL, atomically: true)
    share()
}

【问题讨论】:

    标签: swift uitableview pdf uiview


    【解决方案1】:

    保存按钮调用下方函数

    func createImg() -> UIImage {
            guard tblView.numberOfSections > 0, tblView.numberOfRows(inSection: 0) > 0 else {
                let errorImage = UIImage(named: "Error Image")
                return errorImage!
            }
            UIGraphicsBeginImageContextWithOptions(CGSize(width: tblView.contentSize.width, height:  tblView.contentSize.height), false, 0.0)
            let context = UIGraphicsGetCurrentContext()
            let previousFrame = tblView.frame
            tblView.frame = CGRect(x: tblView.frame.origin.x, y:  tblView.frame.origin.y, width: tblView.contentSize.width, height: tblView.contentSize.height)
            // Draw view in that context
            tblView.layer.render(in: context!)
            tblView.frame = previousFrame
            let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
            UIGraphicsEndImageContext()
            createPDFDataFromImage(image: image)
            return image;
        }
    

    从图像创建 PDF 并分享

    func createPDFDataFromImage(image: UIImage){
            let pdfData = NSMutableData()
            let imgView = UIImageView.init(image: image)
            let imageRect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
            UIGraphicsBeginPDFContextToData(pdfData, imageRect, nil)
            UIGraphicsBeginPDFPage()
            let context = UIGraphicsGetCurrentContext()
            imgView.layer.render(in: context!)
            UIGraphicsEndPDFContext()
    
            //try saving in doc dir to confirm:
            let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last
            let path = dir?.appendingPathComponent("file.pdf")
    
            do {
                try pdfData.write(to: path!, options: NSData.WritingOptions.atomic)
            } catch {
                print("error catched")
            }
            let activityViewController = UIActivityViewController(activityItems: [pdfData] , applicationActivities: nil)
            activityViewController.popoverPresentationController?.sourceView = self.view
            self.present(activityViewController, animated: true, completion: nil)
    
        }
    

    【讨论】:

      猜你喜欢
      • 2014-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-04
      • 2013-08-01
      • 1970-01-01
      • 2018-10-18
      相关资源
      最近更新 更多