WebViewController.swift 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //
  2. // WebViewController.swift
  3. // PDFGenerator
  4. //
  5. // Created by Suguru Kishimoto on 2016/03/23.
  6. //
  7. //
  8. import UIKit
  9. import WebKit
  10. import PDFGenerator
  11. class WebViewController: UIViewController {
  12. @IBOutlet fileprivate weak var webView: UIWebView!
  13. override func viewDidLoad() {
  14. super.viewDidLoad()
  15. let req = NSMutableURLRequest(url: URL(string: "http://www.yahoo.co.jp")!, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 60)
  16. webView.loadRequest(req as URLRequest)
  17. }
  18. override func didReceiveMemoryWarning() {
  19. super.didReceiveMemoryWarning()
  20. }
  21. @IBAction func generatePDF() {
  22. do {
  23. let dst = NSHomeDirectory() + "/sample_tblview.pdf"
  24. try PDFGenerator.generate(webView, to: dst)
  25. openPDFViewer(dst)
  26. } catch let error {
  27. print(error)
  28. }
  29. }
  30. fileprivate func openPDFViewer(_ pdfPath: String) {
  31. let url = URL(fileURLWithPath: pdfPath)
  32. let storyboard = UIStoryboard(name: "PDFPreviewVC", bundle: nil)
  33. let vc = storyboard.instantiateInitialViewController() as! PDFPreviewVC
  34. vc.setupWithURL(url)
  35. present(vc, animated: true, completion: nil)
  36. }
  37. /*
  38. // MARK: - Navigation
  39. // In a storyboard-based application, you will often want to do a little preparation before navigation
  40. override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  41. // Get the new view controller using segue.destinationViewController.
  42. // Pass the selected object to the new view controller.
  43. }
  44. */
  45. }