Android

[Android, 안드로이드] 웹뷰(WebView)에서 PDF 불러오는 방법

웹뷰에서 PDF 링크를 불러왔으나 무한로딩에 빠졌다. 무언가 문제가 있음을 직감했다. 설마 지원을 안하는 것은 아니겠지??? 구글링을 시작했다. PDF를 불러오지 못 한 다면 PDF파일을 앱에 포함시켜 apk를 생성해야하는 당황스러운 상황에 처했다.

웹뷰(WebView)에서 PDF 불러오는 방법

안타깝게도 Android는 WebView에서 즉시 PDF보기를 지원하지 않는다. 다행히 Google에는 Google 문서 도구를 사용하여 PDF를 볼 수 있도록 해준다. 기본적으로 Google Doc 페이지에 PDF를 즉시 삽입하고 로드한다. 코드는 다음과 같다.

[먹히지 않는 코드] URLEncoder를 사용하여 utf-8로 인코딩하였지만 실패하였다.

String url = null;
try {
	url = URLEncoder.encode("595754dd-31f9-4417-973c-237775ec6615/resource/01eff3c4-4893-4cbd-8a82-a7fbc5a8cae9/download/booklet-2019.pdf", "utf-8");
} catch (UnsupportedEncodingException e) {
	e.printStackTrace();
}
String path = "https://open.test.com/dataset/" + url;
webView.loadUrl(path);

[성공한 코드] 구글doc사이트에 url를 던져서 읽어올 수 있다.

String pdfUrl = "https://open.test.com/dataset/595754dd-31f9-4417-973c-237775ec6615/resource/01eff3c4-4893-4cbd-8a82-a7fbc5a8cae9/download/booklet-2019.pdf";
String url = "http://docs.google.com/gview?embedded=true&url=" + pdfUrl;
webView.loadUrl(url);

[전체 코드]

 
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import androidx.appcompat.app.AlertDialog;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
 
public class WebviewActivity extends BasicActivity {
    private WebView webView;
    private WebSettings webSettings;
    private ProgressBar mProgressBar;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        webView = findViewById(R.id.webView);
        mProgressBar = findViewById(R.id.progress1);


        findViewById(R.id.close_text).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                findViewById(R.id.close_text).setBackgroundResource(R.drawable.menu_list_selector);
                makeVibrate();
                finish();
            }
        });

        webView.setWebViewClient(new WebViewClientClass()); // new WebViewClient()); //클릭시 새창 안뜨게
        webView.setWebChromeClient(new WebChromeClient());  //웹뷰에서 크롬이 실행가능하도록
        webSettings = webView.getSettings(); //세부세팅 설정
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportMultipleWindows(false);// 새창띄우기 허용여부
        webSettings.setJavaScriptCanOpenWindowsAutomatically(false);// 자바스크립트 새창뛰우기 (멀티뷰) 허용여부
        webSettings.setLoadWithOverviewMode(true); //메타태크 허용여부
        webSettings.setUseWideViewPort(true);//화면 사이즈 맞추기 허용여부
        webSettings.setSupportZoom(true);//화면 줌 허용여부
        webSettings.setBuiltInZoomControls(true); //화면 확대 축소 허용여부
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); //컨텐츠 사이즈 맞추기
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); //브라우저 캐시 허용여부
        webSettings.setDomStorageEnabled(true); //로컬저장소 허용여부

        //안드로이드 android webview 에서 https 보안에서 컨텐츠(이미지, 텍스트)들이 error, 막히거나, 안보일때 방법
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
//        {
//            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
//        }
 
 		//pdf링크 불러오기
        String pdfUrl = "https://open.test.com/dataset/595754dd-31f9-4417-973c-237775ec6615/resource/01eff3c4-4893-4cbd-8a82-a7fbc5a8cae9/download/booklet-2019.pdf";
        String url = "http://docs.google.com/gview?embedded=true&url=" + pdfUrl;
        webView.loadUrl(url); 

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {//뒤로가기 버튼 이벤트
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {//웹뷰에서 뒤로가기 버튼을 누르면 뒤로가짐
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }



    //웹뷰에서 홈페이지를 띄웠을때 새창이 아닌 기존창에서 실행이 되도록 아래 코드를 넣어준다.
    private class WebViewClientClass extends WebViewClient {//페이지 이동
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);

            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            mProgressBar.setVisibility(ProgressBar.VISIBLE);
            webView.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onPageCommitVisible(WebView view, String url) {
            super.onPageCommitVisible(view, url);
            mProgressBar.setVisibility(ProgressBar.GONE);
            webView.setVisibility(View.VISIBLE);
        }

//        @Override
//        public void onPageFinished(WebView view, String url) {
//            findViewById(R.id.progress1).setVisibility(View.GONE);
//        }

        @Override
        public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(WebviewActivity.this);
            String message = "SSL Certificate error.";
            switch (error.getPrimaryError()) {
                case SslError.SSL_UNTRUSTED:
                    message = "The certificate authority is not trusted.";
                    break;
                case SslError.SSL_EXPIRED:
                    message = "The certificate has expired.";
                    break;
                case SslError.SSL_IDMISMATCH:
                    message = "The certificate Hostname mismatch.";
                    break;
                case SslError.SSL_NOTYETVALID:
                    message = "The certificate is not yet valid.";
                    break;
            }
            message += " Do you want to continue anyway?";

            builder.setTitle("SSL Certificate Error");
            builder.setMessage(message);
            builder.setPositiveButton("continue", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    handler.proceed();
                }
            });
            builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    handler.cancel();
                }
            });
            final AlertDialog dialog = builder.create();
            dialog.show();
        }
    }
 
}

[activity_webview.xml]

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/color1">
    <LinearLayout
        android:id="@+id/top_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="5dp"
        android:gravity="right"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/close_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/baseline_cancel_24"
            android:text="@string/cont_13"
            android:background="@drawable/menu_list_selector"
            android:textColor="@color/color_gray"/>

    </LinearLayout>
    <LinearLayout
        android:id="@+id/topLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginBottom="70dp"
        android:gravity="center|top"
        android:background="@drawable/bg_round_radius"
        android:layout_below="@+id/top_layout"
        android:orientation="vertical">

        <WebView
            android:id="@+id/webView"
            android:layout_margin="5dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorWhite" />
    </LinearLayout>

    <ProgressBar
        android:id="@+id/progress1"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

[AndroidManifest.xml]

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="app.test">
	
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:name=".common.MyApplication"
        android:allowBackup="false"
        android:fullBackupContent="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        
        <activity android:name=".IntroActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".WebviewActivity"
            android:exported="false"
            android:theme="@style/AppTheme.NoActionBar" />    
    </application>

</manifest>            

[REFERENCE]

kylewbanks.com/blog/Loading-PDF-in-Android-WebView

[관련자료]

stackoverflow.com/questions/30461392/android-pdf-not-loading-in-browser-and-webview

www.tutorialspoint.com/how-to-display-a-pdf-document-into-an-android-webview

Leave a Reply

error: Content is protected !!