PdfReaderViewController.swift 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // PdfReaderViewController.swift
  3. // LMS
  4. //
  5. // Created by Suraj Kumar Mandal on 24/08/22.
  6. //
  7. import UIKit
  8. import WebKit
  9. import Toast_Swift
  10. class PdfReaderViewController: UIViewController {
  11. @IBOutlet var navigationBar: UINavigationBar!
  12. @IBOutlet var webView: WKWebView!
  13. var viewModel = PdfReaderViewModel()
  14. var id = String()
  15. var docType = String()
  16. var name = String()
  17. override func viewDidLoad() {
  18. super.viewDidLoad()
  19. // Do any additional setup after loading the view.
  20. navigationBar.topItem?.title = name
  21. viewModel.delegate = self
  22. getPDF()
  23. }
  24. func getPDF() {
  25. if Reachability.isConnectedToNetwork() {
  26. viewModel.getPdfData(fileId: id)
  27. } else {
  28. Alert.showInternetFailureAlert(on: self)
  29. }
  30. }
  31. func loadWebView(data:String) {
  32. if let decodeData = Data(base64Encoded: data, options: .ignoreUnknownCharacters) {
  33. if docType == "pdf" {
  34. webView.load(decodeData, mimeType: "application/pdf", characterEncodingName: "utf-8", baseURL: URL(fileURLWithPath: ""))
  35. } else if docType == "docx" {
  36. let urlStr = "data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64," + decodeData.base64EncodedString()
  37. let url = URL(string: urlStr)!
  38. let request = URLRequest(url: url)
  39. self.webView.load(request)
  40. } else if docType == "scorm" {
  41. let url = URL (string: "http://3.7.239.194/\(id)")
  42. let requestObj = URLRequest(url: url!)
  43. self.webView.load(requestObj)
  44. } else if docType == "jpeg" {
  45. let docData = Data(base64Encoded: data, options: .ignoreUnknownCharacters)
  46. let jpeg = UIImage(data: docData!)
  47. let imageData = jpeg?.jpegData(compressionQuality: 1.0)
  48. let base64EncodedImage = imageData?.base64EncodedString()
  49. let htmlString = "<html><head><style>img{width:100%; height:100%;}</style></head><body><img src='data:image/jpeg;base64,\(base64EncodedImage!)' /></body></html>"
  50. //"<html><body><img src='data:image/jpeg;base64,\(base64EncodedImage!)' /></body></html>"
  51. webView.loadHTMLString(htmlString, baseURL: nil)
  52. }
  53. }
  54. }
  55. /*
  56. // MARK: - Navigation
  57. // In a storyboard-based application, you will often want to do a little preparation before navigation
  58. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  59. // Get the new view controller using segue.destination.
  60. // Pass the selected object to the new view controller.
  61. }
  62. */
  63. @IBAction func backAction(_ sender: UIBarButtonItem) {
  64. self.navigationController?.popViewController(animated: true)
  65. }
  66. }
  67. extension PdfReaderViewController: PdfReaderProtocol {
  68. func startLoader() {
  69. ActivityIndicator.start()
  70. }
  71. func stopLoader() {
  72. ActivityIndicator.stop()
  73. }
  74. func showError(error: String) {
  75. self.view.makeToast(error)
  76. }
  77. func pdfBase64(base64: String) {
  78. self.loadWebView(data: base64)
  79. }
  80. }