2

我正在创建 webview 来打印数据。根据我的要求,页面的一半包含标题标签,其余部分以具有表格内容的 uitableview 开头。

如果 tableview 内容更多,那么我需要将这些内容继续到下一页。

func createPDF(_ currentWebView: UIWebView)
{        
    let date = Date()
    let calendar = Calendar.current
    let dateComponents: DateComponents = (calendar as NSCalendar).components([.month, .day, .year], from: date)
    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = DateFormatter.Style.medium
    let dateFormatter1 = DateFormatter()
    dateFormatter1.dateFormat = "MM/dd/yy"
    let strOrderDate = NSString(format: "Date: " + dateFormatter1.string(from: date) as NSString)
    let ID = UserDefaults.standard.value(forKey: "ID") as! String

    lblValue.text = ""
    lblValue.text = "Value: " + ID
    lblTotalItems.text = "Total items: " + (totalNumberofItems as String)

    lblDate.text = strOrderDate as String
    tblContainer.tableFooterView = UIView()
    tblContainer.reloadData()

    for eachView: UIView in self.view.subviews
    {
        if(eachView.isKind(of: UILabel.self) || eachView.isKind(of: UIView.self) )
        {
            if(eachView != self.view && eachView != currentWebView)
            {
                currentWebView.addSubview(eachView)
            }
        }
    }

   let heightStr: NSString = currentWebView.stringByEvaluatingJavaScript(from: "document.body.scrollHeight")! as NSString
   let height : Int = heightStr.integerValue
   let screenHeight: CGFloat = currentWebView.bounds.size.height;
   let pages = Int(ceil(screenHeight/CGFloat(height)))

    let frame: CGRect = currentWebView.frame
    var currentContext: CGContext = UIGraphicsGetCurrentContext()!

    for i: Int in 0..<pages
    {
        if (Int(CGFloat(i+1) * screenHeight) > height)
        {
            var f: CGRect = currentWebView.frame
            f.size.height = f.size.height - CGFloat((CGFloat(i + 1) * screenHeight) - CGFloat(height));
            currentWebView.frame = f
        }
        UIGraphicsBeginPDFPage();
        currentContext = UIGraphicsGetCurrentContext()!
        currentWebView.scrollView.setContentOffset(CGPoint(x: 0, y: screenHeight * CGFloat(i)) , animated: false)
        currentWebView.layer.render(in: currentContext)
    }
    currentWebView.frame = frame
}

我的问题是:如果 tableview 内容更多且可滚动,我想在第二页中也显示这些内容,但这没有发生。

Tableview 在同一页面上被删减。

任何人对此都有任何想法..请帮忙!

谢谢

4

1 回答 1

0

我希望您在查找要生成的页数时感到困惑。请尝试以下代码来计算页面。

   let heightStr: NSString = currentWebView.stringByEvaluatingJavaScript(from:       "document.body.scrollHeight")! as NSString
   let height : Int = heightStr.integerValue
   let screenHeight: CGFloat = currentWebView.bounds.size.height
// ******** Change is here. *******
   let pages = Int(ceil(CGFloat(height)/screenHeight))

希望这会奏效。快乐编码:)

于 2019-04-30T02:08:22.407 回答