简体   繁体   中英

android, WebView application/pdf ~ Blank Display

I'm attempting to display a PDF using WebView on android. The PDF data is downloaded via an API, then once encapsulated in the intent:

    WebView webView = (WebView) findViewById(R.id.webView);
    Log.d("DEV", bIntent.getString("Data"));
    webView.loadData(bIntent.getString("Data"), "application/pdf", "UTF-8");

I have the exact same setup, except implemented on iOS which is working fine. The LogCat output (see below) does give out information, however reading through it doesn't suggest or prompt me to any issues or errors.

D/JSENGINE﹕ qualcomm.jsengine.version:C.2-patch35-git:7b7ad6f
D/HostStatisticManager﹕ netstack: DNS Host Prioritization is: ON, Version: 5.0.1
I/﹕ netstack: LIB_MGR - Lib loaded: libdnshostprio.so
I/﹕ netstack: STAT_HUB - Succeeded to load plugin: libdnshostprio.so
E/﹕ netstack: LIB_MGR - Error loading lib spl_proc_plugin.so
E/﹕ netstack: STAT_HUB - Failed to load plugin: spl_proc_plugin.so
I/﹕ netstack: LIB_MGR - Lib loaded: pp_proc_plugin.so
I/﹕ netstack: STAT_HUB - Succeeded to load plugin: pp_proc_plugin.so
E/﹕ netstack:  STAT_HUB - App com.company.simonaddicott.controlpanel_1 isn't supported
V/chromium_net﹕ external/chromium/net/host_resolver_helper/host_resolver_helper.cc:66: [0512/144513:INFO:host_resolver_helper.cc(66)] DNSPreResolver::Init got hostprovider:0x5f7d700c
V/chromium_net﹕ external/chromium/net/base/host_resolver_impl.cc:1510: [0512/144513:INFO:host_resolver_impl.cc(1510)] HostResolverImpl::SetPreresolver preresolver:0x5f6499f0
D/﹕ external/chromium/net/socket/tcp_fin_aggregation_factory.cc: libtcpfinaggr.so successfully loaded
D/﹕ external/chromium/net/socket/tcp_fin_aggregation_factory.cc,: TCP Fin Aggregation initializing method was found in libtcpfinaggr.so
I/QCNEA﹕ |CAC| CAS is enabled
I/QCNEA﹕ |CAC| [CNE CLIENT STATE MACHINE] transition NOT_CONNECTED_NOT_ATTEMPTED -> CONNECTING
D/QCNEA﹕ |CAC| Connected to server socket: 81
I/QCNEA﹕ |CAC| [CNE CLIENT STATE MACHINE] transition CONNECTING -> CONNECTED_PENDING_PERM_RESPONSE
D/QCNEA﹕ |CAC| In monitor thread, performing select
D/TCPFinAggregation﹕ netstack: TCPFinAggregation is 1, Version 5.0.1
D/TCPFinAggregation﹕ system property net.tcp.fin.aggregation.wait was set, value: 20
D/TCPFinAggregation﹕ system property net.tcp.fin.aggregation.close was set, value: 300
D/TCPFinAggregation﹕ netstack: CloseUnusedSockets is ON, (TCPFinAggregation), Version 5.0.1
D/QCNEA﹕ |CAC| readCallback: read len:12, ret:0, errno:0
I/QCNEA﹕ |CAC| [CNE CLIENT STATE MACHINE] transition CONNECTED_PENDING_PERM_RESPONSE -> NOT_CONNECTED_ACCESS_DENIED
I/QCNEA﹕ |CAC| client permission denied.
D/QCNEA﹕ |CAC| readCallback: read len:0, ret:0, errno:0
E/QCNEA﹕ |CAC| readCallback: end of stream
V/QCNEA﹕ |CAC| Dispatching Latency Service Status Update [status:SERVICE_STOPPED]
V/QCNEA﹕ |CAC| Dispatching Bitrate Service Status Update [status:SERVICE_STOPPED] V/QCNEA﹕ |CAC| Clients reset
D/QCNEA﹕ |CAC| Monitor loop is terminating
D/TCPFinAggregation﹕ Failed to get network status! received ret: -6
D/Socket_Pool﹕ netstack: CloseUnusedSockets is ON
D/Socket_Pool﹕ netstack: system net.statistics value: 0
D/Socket_Pool﹕ netstack: CloseUnusedSockets is ON
D/Socket_Pool﹕ netstack: system net.statistics value: 0
D/﹕ external/chromium/net/http/http_getzip_factory.cc: libgetzip.so successfully loaded
D/﹕ external/chromium/net/http/http_getzip_factory.cc,: GETzip initializing method was found in libgetzip.so
D/netstack﹕ netstack: Request Priority is ON
D/﹕ netstack: Getzip is: ON, Version: 5.0.1
D/TilesManager﹕ Starting TG #0, 0x5f7d0170
D/TilesManager﹕ new EGLContext from framework: 5e7a9fd0
D/GLWebViewState﹕ Reinit shader
D/GLWebViewState﹕ Reinit transferQueue 

I'll also attach the XML in case an issue may be in there, however it is unlikely

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.company.simonaddicott.controlpanel_1.PDFView">

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

I'm attempting to display a PDF using WebView on android

WebView does not display PDF files on its own. At best, it can do so with the assistance of some third-party JavaScript (pdf.js) or site (Google Docs). There are plenty of third-party libraries for rendering PDFs as well, which may or may not use WebView under the covers.

Moreover, a PDF file is not a string, but rather a binary format, and so loadData() would not work in any case.


Lollipop now provides a native library for reading PDF files, however this doesn't help anyone using > API 21 .

The solution found here was to utilize mozilla's JS pdfviewer, and point the webview to that.

http://mozilla.github.io/pdf.js

WebView webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://mozilla.github.io/pdf.js/web/viewer.html");

Majorty of other solutions involved Google Docs as a proxy for viewing the PDF. This solution wasn't suitable here due to data protection/sensitivity of the PDF data in question.

With the JS pdf viewer one can install server side & continue to serve PDF to the app directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM