iOS經典講解之UIWebView載入PDF不顯示電子簽章解決方案
阿新 • • 發佈:2018-12-31
一、對於iOS 12系統可直接使用WKWebView 載入pdf url 即可,便會顯示電子簽章,WKWebView是從 iOS 8.0 和 OS X 10.10 開始的;
二、對於iOS 12以下版本,有其他的解決方案:
iOS版本 < 12的情況可以使用PDF.js:
PDF.js可以實現在html下直接瀏覽pdf文件,是一款開源的pdf文件讀取解析外掛,非常強大,能將PDF檔案渲染成Canvas。PDF.js主要包含兩個庫檔案,一個pdf.js和一個pdf.worker.js,一個負責API解析,一個負責核心解析。
下載Build,包括build、web兩個資料夾,這是我們需要的:
把檔案複製到工程資料夾,然後拖到工程中,注意不要選Create group
載入例項:
NSString *filePath = @"xxx.pdf"; NSString *viwerPath = [[NSBundle mainBundle] pathForResource:@"viewer" ofType:@"html" inDirectory:@"minified/web"]; NSString *urlStr = [NSString stringWithFormat:@"%@?file=%@#page=1",viwerPath,filePath]; urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]]; [web loadRequest:request];
注意:
1、一定使用UIWebview載入;
2、 跨域載入,大多數情況是載入伺服器遠端pdf文件,此時會載入失敗,找到viewer.js檔案將判斷註釋掉即可;
// if (fileOrigin !== viewerOrigin) {
// throw new Error('file origin does not match viewer\'s');
// }
3、將 viewer.js的 var DEFAULT_URL = 'compressed.tracemonkey-pldi-09.pdf'; 預設路徑刪掉,改為var DEFAULT_URL = '';
4、預設是不顯示電子簽章、簽名的,若需要,將worker.js檔案中的相關程式碼註釋;
if (data.fieldType === 'Sig') {
//WGZ
//warn('unimplemented annotation type: Widget signature');
//this.setFlags(AnnotationFlag.HIDDEN);
}