Reputation: 837
There is a webview in the app and a "Save" button on top of the webview.
After clicking the "Save" button currenturl will be saved to a TableView on another ViewController. Everything works fine.
Now I would like to also save the web page as PDF to the same TableView and can open it after clicking. How can I do that?
The following is codes for saving currenturl. savedURLs is the array to save all urls on another ViewController.
import UIKit
class BrowserViewController: UIViewController {
@IBOutlet weak var browser: UIWebView!
@IBAction func saveButtonPressed(sender: AnyObject) {
if let currentURL = browser.request?.URL?.absoluteString {
savedURLs.append(currentURL)
NSUserDefaults.standardUserDefaults().setObject(currentURL, forKey: "currenturl")
NSUserDefaults.standardUserDefaults().setObject(savedURLs, forKey: "savedURLs")
self.performSegueWithIdentifier("toSavingVC", sender: self)
}
}
Upvotes: 2
Views: 2127
Reputation: 4107
Here is the Swift code for converting webPage to image, then you can convert it to PDF:
let sizevid = CGSizeMake(1024, 1100)
UIGraphicsBeginImageContext(sizevid);
webview.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let viewImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
var imageData = NSData()
imageData = UIImagePNGRepresentation(viewImage)!
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let pathFolder = String(String(format:"%@", "new.png"))
let defaultDBPathURL = NSURL(fileURLWithPath: documentsPath).URLByAppendingPathComponent(pathFolder)
let defaultDBPath = "\(defaultDBPathURL)"
imageData.writeToFile(defaultDBPath, atomically: true)
// After converting it in to an image you can easily convert it as pdf.
Upvotes: 3