一日月、公司項(xiàng)目使用到電子合同,用的是加載pdf或油,合同就需要簽字和蓋章,開始用的是WKWebView加載驰唬,后來(lái)發(fā)現(xiàn)iOS11及以下版本用WKWebView時(shí)不顯示電子簽名和簽章顶岸,那iOS11及以下版本就需要特殊處理,這里用的是UIWebView 加載pdf.js
二叫编、pdf.js可以實(shí)現(xiàn)在html下直接瀏覽pdf文檔蜕琴,是一款開源的pdf文檔讀取解析插件,非常強(qiáng)大宵溅,能將PDF文件渲染成Canvas凌简。PDF.js主要包含兩個(gè)庫(kù)文件,一個(gè)pdf.js和一個(gè)pdf.worker.js恃逻,一個(gè)負(fù)責(zé)API解析雏搂,一個(gè)負(fù)責(zé)核心解析
pdf.js下載地址
** 把文件復(fù)制到工程文件夾藕施,然后拖到工程中,注意不要選Create group**
加載方法
- (void)afnetworking {
if (!self.uiwebView) {
self.uiwebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, MainWidth - AutoSizeScaleX(60), MainHeight- SJ_StatusBarAndNavigationBarHeight)];
}
[self.view addSubview:self.uiwebView];
[self downloadFileWithUrlString:self.pdfUrl completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *viwerPath = [[NSBundle mainBundle] pathForResource:@"viewer" ofType:@"html" inDirectory:@"pdfjs/web"];
NSString *urlStr = [NSString stringWithFormat:@"%@?file=%@#page=1",viwerPath,filePath];
urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
[self.uiwebView loadRequest:request];
});
}];
}
- (void)downloadFileWithUrlString:(NSString *)urlString completionHandler:(void (^ _Nullable)(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error))completionHandler {
if (!urlString) {
return;
}
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] progress:nil destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
return [[[NSFileManager defaultManager] URLForDirectory:(NSCachesDirectory) inDomain:(NSUserDomainMask) appropriateForURL:targetPath create:YES error:nil] URLByAppendingPathComponent:[NSString stringWithFormat:@"%ld.pdf", (long)[[NSDate date] timeIntervalSince1970]]];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
completionHandler(response, filePath, error);
}];
[downloadTask resume];
}
注意
一凸郑、有個(gè)問(wèn)題就是pdf.js的加載緩慢裳食,可能需要大概10s
二、一定使用UIWebview加載
三芙沥、跨域加載诲祸,大多數(shù)情況是加載服務(wù)器遠(yuǎn)程pdf文檔,此時(shí)會(huì)加載失敗而昨,找到viewer.js文件將判斷注釋掉即可
// if (origin !== viewerOrigin && protocol !== 'blob:') {
// throw new Error('file origin does not match viewer\'s');
// }
四救氯、將 viewer.js的 var DEFAULT_URL = 'compressed.tracemonkey-pldi-09.pdf'; 默認(rèn)路徑刪掉,改為var DEFAULT_URL = '';
五歌憨、默認(rèn)是不顯示電子簽章着憨、簽名的,若需要务嫡,將worker.js文件中的相關(guān)代碼注釋甲抖;
if (data.fieldType === 'Sig') {
//WGZ
// data.fieldValue = null;
//
// _this3.setFlags(_util.AnnotationFlag.HIDDEN);
}
六、如果需要修改加載頁(yè)面的布局樣式心铃,或者隱藏下載按鈕等准谚,把viewer.html文件中相關(guān)代碼注釋掉,或者修改相關(guān)屬性即可 去扣。 注:嘗試修改了viewer.html文件代碼氛魁,但達(dá)不到我想要的效果
參考文章