
加載pdf文件一般我們會使用UIWebView、WKWebView來加載pdf,但是當(dāng)pdf內(nèi)含有簽名以及電子章的時(shí)候,電子簽章和簽名將無法顯示出來。原因是:在PDF文件中看到的簽名是簽名字段小部件的外觀。字段小部件是特定的PDF注釋。iOS中的默認(rèn)PDF呈現(xiàn)引擎(由UIDocumentInteractionController,QuickLook框架等使用)僅顯示主頁面內(nèi)容,它不顯示PDF頁面上的注釋。這時(shí)候就需要我們使用其他第三方PDF渲染引擎了。
pdf.js在 HTML5 平臺上展示 PDF 文檔。支持web、iOS、Android端(android 手機(jī)微信端不支持)。
1.創(chuàng)建iOS工程,將minified復(fù)制到工程目錄中。

2.拖拽復(fù)制的文件目錄到工程中,選擇folder references(親測選擇groups沒有效果,原因未知):

3.新建PdfLoadWebView繼承自UIWebView,添加如下代碼:
- (void)loadPDFFile:(NSString*)filePath {
_filePath = filePath;
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]];
[self loadRequest:request];
}
在控制器里添加如下代碼:
webView = [[PdfLoadWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:webView];
NSString *pdfFilePath = [[NSBundle mainBundle] pathForResource:@"git搭建" ofType:@"pdf"];
[webView loadPDFFile:pdfFilePath];
運(yùn)行后效果如圖:

注釋:
1.因?yàn)轫?xiàng)目中我只需要展示pdf,所以就將對pdf的操作按鈕隱藏掉了。如果需要按鈕將minified->web->viewer.html 中
<div class="toolbar" style="display: none;">
代碼修改成
<div class="toolbar">

2.項(xiàng)目中需要用到pdf滾到到底部或者頭部,所以我在viewer.html寫了兩個(gè)js方法以供oc進(jìn)行調(diào)用。
viewer.html文件中
<script type="text/javascript">
function lastPage() {
document.getElementById("lastPage").click();
}
function firstPage() {
document.getElementById("firstPage").click();
}
在PdfLoadWebView.m中進(jìn)行oc調(diào)用js方法
[self stringByEvaluatingJavaScriptFromString:@"lastPage()"];
因?yàn)槲覍?shí)在頁面加載完成后立即進(jìn)行頁面跳轉(zhuǎn)。所以加了一個(gè)延時(shí)操作
現(xiàn)在加載的是本地pdf,加載網(wǎng)絡(luò)pdf還需要繼續(xù)研究。
DEMO
iOS展示pdf簽名時(shí)遇到的問題及解決辦法
iOS使用pdf.js打開PDF文件
iOS 加載PDF問題無法顯示電子章問題