手機應用中pdf展示使用非常廣泛, 一些pdf由于特殊的內容比如文字、電子簽章必須使用復雜的解析器來解析,當使用MultiPdf 這個庫加載,會使得包變得非常龐大, 這里我們考慮使用pdf.js 來解析pdf.
引用非常簡單,只需要把js相關文件放到assets文件夾中即可,然后通過webview來加載。android 使用H5加載非常緩慢。這里pdfUrl是本地路徑。,這里要注意路徑問題viewer.html的路徑。
mWebView.loadUrl("file:///android_asset/pdfjs/web/viewer.html?file=" + pdfUrl)
使用過程中會有兩個問題:
電子簽章(electronic signature):
解決辦法:pdf.worker.js注意注釋這一行代碼
data.readOnly = _this2.hasFieldFlag(_util.AnnotationFieldFlag.READONLY);if (data.fieldType === 'Sig') {
// _this2.setFlags(_util.AnnotationFlag.HIDDEN);}return _this2;
部分機型出現文字混亂問題:
解決辦法:pdf.worker.js 加載文字這個地方增加動態碼 Math.floor(Math.random()*9999).toString()
(0, _util.assert)(fontID, 'The "fontID" must be defined.');font.loadedName = 'g_' + Math.floor(Math.random()*9999).toString() + this.pdfManager.docId + '_f' + fontID;font.translated = fontCapability.promise;