// // PdfReaderViewController.swift // LMS // // Created by Suraj Kumar Mandal on 24/08/22. // import UIKit import WebKit import Toast_Swift class PdfReaderViewController: UIViewController { @IBOutlet var navigationBar: UINavigationBar! @IBOutlet var webView: WKWebView! var viewModel = PdfReaderViewModel() var id = String() var docType = String() var name = String() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. navigationBar.topItem?.title = name viewModel.delegate = self getPDF() } func getPDF() { if Reachability.isConnectedToNetwork() { viewModel.getPdfData(fileId: id) } else { Alert.showInternetFailureAlert(on: self) } } func loadWebView(data:String) { if let decodeData = Data(base64Encoded: data, options: .ignoreUnknownCharacters) { if docType == "pdf" { webView.load(decodeData, mimeType: "application/pdf", characterEncodingName: "utf-8", baseURL: URL(fileURLWithPath: "")) } else if docType == "docx" { let urlStr = "data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64," + decodeData.base64EncodedString() let url = URL(string: urlStr)! let request = URLRequest(url: url) self.webView.load(request) } else if docType == "scorm" { let url = URL (string: "http://3.7.239.194/\(id)") let requestObj = URLRequest(url: url!) self.webView.load(requestObj) } else if docType == "jpeg" { let docData = Data(base64Encoded: data, options: .ignoreUnknownCharacters) let jpeg = UIImage(data: docData!) let imageData = jpeg?.jpegData(compressionQuality: 1.0) let base64EncodedImage = imageData?.base64EncodedString() let htmlString = "" //"" webView.loadHTMLString(htmlString, baseURL: nil) } } } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destination. // Pass the selected object to the new view controller. } */ @IBAction func backAction(_ sender: UIBarButtonItem) { self.navigationController?.popViewController(animated: true) } } extension PdfReaderViewController: PdfReaderProtocol { func startLoader() { ActivityIndicator.start() } func stopLoader() { ActivityIndicator.stop() } func showError(error: String) { self.view.makeToast(error) } func pdfBase64(base64: String) { self.loadWebView(data: base64) } }