티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

How can I display a pdf document into a Webview?

pdf 문서를 웹뷰에 표시하려면 어떻게 해야 합니까?

 문제 내용 

I want to display pdf contents on webview. Here is my code:

저는 웹뷰에 pdf 콘텐츠를 표시하고 싶습니다. 제 코드는 다음과 같습니다.
WebView webview = new WebView(this); 
setContentView(webview);
webview.getSettings().setJavaScriptEnabled(true); 
webview.loadUrl("http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf");

 

I am getting a blank screen. I have set internet permission also.

빈 화면이 나오고 있어요. 저는 인터넷 사용 권한도 설정했습니다.

 

 

 

 높은 점수를 받은 Solution 

You can use Google PDF Viewer to read your pdf online:

Google PDF 뷰어를 사용하여 온라인으로 PDF를 읽을 수 있습니다.
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true); 
String pdf = "http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";
webview.loadUrl("https://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);

 

 

 가장 최근 달린 Solution 

Actually all of the solutions were pretty complex, and I found a really simple solution (I'm not sure if it is available for all sdk versions). It will open the pdf document in a preview window where the user is able to view and save/share the document:

실제로 모든 솔루션이 상당히 복잡했으며, 정말 간단한 솔루션을 찾았습니다(모든 SDK 버전에서 사용할 수 있는지는 잘 모르겠습니다). 사용자가 문서를 보고 저장/공유할 수 있는 미리보기 창에서 PDF 문서가 열립니다.
webView.setDownloadListener(DownloadListener { url, userAgent, contentDisposition, mimetype, contentLength ->
     val i = Intent(Intent.ACTION_QUICK_VIEW)
     i.data = Uri.parse(url)
     if (i.resolveActivity(getPackageManager()) != null) {
            startActivity(i)
     } else {
            val i2 = Intent(Intent.ACTION_VIEW)
            i2.data = Uri.parse(url)
            startActivity(i2)
     }
})

(Kotlin)

(코틀린)

 

 

 

출처 : https://stackoverflow.com/questions/2655972/how-can-i-display-a-pdf-document-into-a-webview

반응형
댓글
공지사항
최근에 올라온 글