参考:https://blog.csdn.net/Android_Cll/article/details/131641229
https://cloud.tencent.com/developer/article/2301730
Android项目新增js:/app/src/main/assets/wwwroot/index.js 我新建了一个wwwroot放里面了。自己看着办。
var url = location.search.substring(1); PDFJS.cMapUrl = 'https://unpkg.com/[email protected]/cmaps/'; PDFJS.cMapPacked = true; var pdfDoc = null; function createPage() { var div = document.createElement("canvas"); document.body.appendChild(div); return div; } function renderPage(num) { pdfDoc.getPage(num).then(function (page) { var viewport = page.getViewport(2.0); var canvas = createPage(); var ctx = canvas.getContext('2d'); canvas.height = viewport.height; canvas.width = viewport.width; page.render({ canvasContext: ctx, viewport: viewport }); }); } PDFJS.getDocument(url).then(function (pdf) { pdfDoc = pdf; for (var i = 1; i <= pdfDoc.numPages; i++) { renderPage(i) } });
同样目录,再新增一个index.html
<html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=4.0,user-scalable=no"/> <title>Document</title> <style type="text/css"> canvas { width: 100%; height: 100%; border: 1px solid black; } </style> <script src="https://unpkg.com/[email protected]/build/pdf.min.js"></script> <script type="text/javascript" src="index.js"></script> </head> <body> </body> </html>
webview加载pdf
package com.jay.myappstudy; import android.app.Activity; import android.content.Context; import android.os.Build; import android.os.Bundle; import android.webkit.WebSettings; import android.webkit.WebView; public class WebViewPdfActivity extends Activity { private WebView mWv1; private Context ctx = this; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_web_view_pdf); mWv1 = findViewById(R.id.wv1); WebSettings webSettings = mWv1.getSettings(); // 允许javascript 设置支持JS就自动打开IndexedDB存储机制 webSettings.setJavaScriptEnabled(true); webSettings.setUseWideViewPort(true); //将图片调整到适合WebView的大小 webSettings.setLoadWithOverviewMode(true); webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小 webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL); webSettings.setUserAgentString("User-Agent"); //webSettings.setLightTouchEnabled(true);//设置用鼠标激活被选项 webSettings.setBuiltInZoomControls(true);//设置支持缩放 webSettings.setDomStorageEnabled(true);//设置DOM缓存,当H5网页使用localStorage时,一定要设置 webSettings.setDatabaseEnabled(true); //WebSettings.LOAD_NO_CACHE 设置去缓存,防止加载的为上一次加载过的 webSettings.setCacheMode(WebSettings.LOAD_DEFAULT); webSettings.setSupportZoom(true);//设置支持变焦 mWv1.setHapticFeedbackEnabled(false); //webSettings.setPluginState(WebSettings.PluginState.ON); webSettings.setAllowFileAccess(true); webSettings.setAllowContentAccess(true); // 设置是否允许通过 file url 加载的 Javascript 可以访问其他的源(包括http、https等源) webSettings.setAllowUniversalAccessFromFileURLs(true); // 支持通过JS打开新窗口 webSettings.setAllowFileAccessFromFileURLs(true); webSettings.setDefaultTextEncodingName("UTF-8"); webSettings.setJavaScriptCanOpenWindowsAutomatically(true); webSettings.setGeolocationEnabled(true); webSettings.setAppCacheEnabled(false); mWv1.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); //触摸焦点起作用 mWv1.requestFocus(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //http与https的方法 webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); } mWv1.loadUrl("file:///android_asset/wwwroot/viewpdf.html?http://192.168.9.9:8090/1.pdf"); } }
标签:WebSettings,mWv1,WebView,var,PDF,Android,true,webSettings,android From: https://www.cnblogs.com/xsj1989/p/17800056.html