I am creating webview to print the data. As per my requirement half of the page contains header labels and remaining starts with uitableview which has table contents.
If tableview contents are more then i need to continue those contents to next page.
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
}
My problem is : If tableview contents are more and scrollable, i want to display those content also in second page but that is not happening.
Tableview is getting cut on the same page.
Any body has any idea about that.. Please help!
Thanks
I hope you are messing with finding the number of pages to generate. Please try the following code to calculate the page.
Hope this will work. Happy Coding :)