I have an application in which I am using Mozilla PDF for viewing PDFs.
Mozilla PDF uses web view to load PDFs.
The PDF files are large, and are all images actually.
The application works fine, but the PDF loads extremely slow on all Samsung devices. The application works fine on other devices, but is so slow on Samsung that it's really useless.
I have the following settings for the web view:
settings.setJavaScriptEnabled(true);
settings.setRenderPriority(RenderPriority.HIGH);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
I also have set a WebChromeClient.
Does anyone knows why am I facing this issue? And only on Samsung devices? Any workarounds? Any help would be greatly appreciated.
Here’s a WebView
setup to display PDFs using PDF.js. It works fine on all devices (that are running at least API 19
), including old tiny about-to-die Samsungs.
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setSupportMultipleWindows(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
{
webView.getSettings().setAllowFileAccessFromFileURLs(true);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
{
webView.getSettings().setMediaPlaybackRequiresUserGesture(true);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
}
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setDomStorageEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient());
// should enable pinch to zoom....
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
String webViewUrl;
try
{
/*
Try encoding the PDF file URL. I think i’ve read that this helps with
load/rendering speed?
*/
webViewUrl = "file:///android_asset/pdfjs/web/viewer.html?file=" + URLEncoder
.encode(pdfUri.toString(), "UTF-8");
}
catch (UnsupportedEncodingException e)
{
// It works without encoding it (at least in my case) so just display it with the string as is.
Log.e("Error encoding PDF file URL, will display without encoding.", e);
webViewUrl = "file:///android_asset/pdfjs/web/viewer.html?file=" + pdfUri.toString();
}
webView.loadUrl(webViewUrl);
Not sure if this will help, but thought I’d post a full WebView
setup to support PDF.js, which I’ve never run into speed issues with. Good luck.
Note: Might also be a good idea to turn hardware acceleration on in your application’s manifest. This is only available on API 14
and above. By now this shouldn’t be an issue (if it is, my heart goes out to you).
<application
android:name="com.pdfjs.webview.app.McAwesome"
//...
android:hardwareAccelerated="true">
</application>