1. 程式人生 > >iOS開發載入PDF顯示電子簽章這點事

iOS開發載入PDF顯示電子簽章這點事

1. 使用iOS自帶的控制元件載入PDF簽章檔案QLPreviewController,問題是在低版本手機下無法顯示簽章

extension JYClauseListViewModel:QLPreviewControllerDelegate,QLPreviewControllerDataSource{

    //跳轉到展示PDF的控制器

    func showQLPreviewController() -> QLPreviewController{

        let qlPreviewVC = QLPreviewController.init()

        qlPreviewVC.delegate = self

        qlPreviewVC.dataSource = self

        return qlPreviewVC

    }

    func numberOfPreviewItems(in controller: QLPreviewController) -> Int {

        return 1

    }

    func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {

        let url = NSURL.init(fileURLWithPath: self.pdfPath!)

        //        let url = NSURL.init(fileURLWithPath: Bundle.main.path(forResource: "jylook", ofType: "PDF")!)

        return url as QLPreviewItem

    }    

}

 2.使用原生三方庫MuPDF載入PDF,可相容低版本顯示電子簽章,問題是打包後體積會增大20M左右

下載連結https://download.csdn.net/download/liwei5bao/10781357 使用方法JYMuPDFManage.sharedInstance().showPDF(self, filePath: pdfPath, fileName: self.clauseListViewModel.contractName ?? "")

3.使用原生三方庫TrustSignPDFDS載入PDF,可相容低版本顯示電子簽章,問題是打包後體積會增大10M左右

使用方法 let pdfVC:TrustSignPDFDSController = TrustSignPDFDSController.init(filePath: pdfPath, fileName: self.clauseListViewModel.contractName ?? "")

self.navigationController.pushViewController(pdfVC, animated: true)

4.使用pdf.js三方庫載入PDF,可相容低版本顯示電子簽章,打包後體積會增大1.2M左右

封裝控制器

import UIKit

class JYPDFTestViewController: BaseController,UIWebViewDelegate {

    var titleStr: String?

    var mainWebView:UIWebView?

    /// 網路連線或者本地資源

    var urlStr:String = "" 

    override func viewDidLoad() {

        super.viewDidLoad()

        mainWebView = UIWebView()

        mainWebView?.delegate = self

        mainWebView?.frame = self.view.bounds

        mainWebView?.scalesPageToFit = true

        self.view.addSubview(mainWebView!)

        self.title = titleStr

        let viewPath:String = Bundle.main.path(forResource: "viewer", ofType: "html", inDirectory: "pdfjs/web") ?? ""

        var urlStr2 = "\(viewPath)?file=\(urlStr)#page=1"

        urlStr2 = urlStr2.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? "https://"

        let requestTwo = URLRequest.init(url: URL.init(string: urlStr2) ?? URL.init(string: "https://")!)

        mainWebView?.loadRequest(requestTwo)

    }

    override func viewDidLayoutSubviews() {

        super.viewDidLayoutSubviews()

        if JYCommonObj.isIphoneX() {

            let frame = view.bounds

            mainWebView?.frame = frame

            mainWebView?.mj_h = frame.size.height - kAreaBottomHeight

        }else {

            mainWebView?.frame = view.bounds

        }

    }

    deinit {

        JYAPPLog("deinit")

    }

}

使用方法

 let pdfHtml = PDFTestViewController()

 pdfHtml.titleStr = "標題"

 pdfHtml.urlStr = "網路連線或者本地資源地址"

 self.navigationController.pushViewController(pdfHtml, animated: true)