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
}
}
下载链接https://download.csdn.net/download/liwei5bao/10781357
使用方法JYMuPDFManage.sharedInstance().showPDF(self, filePath: pdfPath, fileName: self.clauseListViewModel.contractName ?? "")
下载链接https://download.csdn.net/download/liwei5bao/10781396
使用方法 let pdfVC:TrustSignPDFDSController = TrustSignPDFDSController.init(filePath: pdfPath, fileName: self.clauseListViewModel.contractName ?? "")
self.navigationController.pushViewController(pdfVC, animated: true)
下载地址https://download.csdn.net/download/liwei5bao/10781434
首先感谢https://blog.csdn.net/loving_ios/article/details/83023873这个作者,实现过程中发现在GitHub上下载最新版本pdf.js无法加载本地资源,下载之前的版本会出现渲染慢的问题,不管你是本地资源还是网络资源都很慢,试了几个版本后发现https://github.com/mozilla/pdf.js/tree/v1.10.100这个版本,加载网络资源和本地资源都可以加载速度也很快,签章也能显示,修改了PDF展示样式和加载PDF电子签章相关配置,可直接拖入项目使用
封装控制器
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)