There are lots of libraries that are available online but it takes lots of space and time to render you can see these articles if you want to load pdf using third-party libraries Load PDF from URL in Android. But today we are going to load PDFs without using any third-party libraries the main purpose of that is it reduce the APK size and it is so easy to implement we don’t need any type of dependencies for the execution of the program. A sample video is given below to get an idea about what we are going to do in this article.
Note: This Android article covered in both Java and Kotlin languages.
Step-by-Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Step 2: We have created a basic WebView and Set width and height to match_parent and added one unique id to it
Step 3: To Open a pdf without Using any Libraries first upload your pdf in google drive and copy the link of that pdf file
and also change access.
Step 4: We’ll be using html code to load our data in the WebView.
package com.ayush.gfgapp;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.appcompat.app.AppCompatActivity;
public class pdf2 extends AppCompatActivity {
private WebView pdfView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
// Set the layout for this activity
setContentView(R.layout.activity_pdf);
// Initialize the WebView by finding it in the layout
pdfView = findViewById(R.id.webView);
// URL of the PDF you want to display
// Setting up the WebView with the PDF URL
setupWebViewWithUrl(pdfView, pdfUrl);
}
// This function configures the WebView to display the PDF.
private void setupWebViewWithUrl(WebView webView, String url) {
if (webView != null ) {
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled( true );
webSettings.setLoadWithOverviewMode( true );
webSettings.setUseWideViewPort( true );
// Configure a WebViewClient to handle navigation events
webView.setWebViewClient( new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
// Return false to allow the WebView to handle the URL
return false ;
}
});
// Configure a WebChromeClient (optional)
webView.setWebChromeClient( new WebChromeClient() {});
// Generate HTML content to embed the PDF
String htmlContent = getPDFHtml(url);
// Load the HTML content into the WebView
webView.loadData(htmlContent, "text/html" , "utf-8" );
}
}
// This function generates the HTML content to embed the PDF.
private String getPDFHtml(String url) {
return "<!DOCTYPE html>\n" +
"<html>\n" +
"<head>\n" +
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\">\n" +
" <style>\n" +
" body, html {\n" +
" margin: 0;\n" +
" height: 100%;\n" +
" overflow: hidden;\n" +
" }\n" +
" iframe {\n" +
" position: absolute;\n" +
" top: 0;\n" +
" left: 0;\n" +
" width: 100%;\n" +
" height: 100%;\n" +
" border: none;\n" +
" }\n" +
" </style>\n" +
"</head>\n" +
"<body>\n" +
" <iframe src=\"" + url + "\" allow=\"autoplay\"></iframe>\n" +
"</body>\n" +
"</html>" ;
}
} |
package com.ayush.gfgapp
import android.os.Bundle
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
class pdf : AppCompatActivity() {
// Declaring a variable of type WebView
private lateinit var pdfView: WebView
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_pdf)
// Initialize the WebView by finding it in the layout
pdfView = findViewById(R.id.webView)
// URL of the PDF you want to display(Make Sure to add Preview)
// Setting up the WebView with the PDF URL
setupWebViewWithUrl(pdfView, pdfUrl)
}
// This function configures the WebView to display the PDF.
private fun setupWebViewWithUrl(webView: WebView?, url: String) {
webView?.let {
// Enable JavaScript in the WebView
it.settings.javaScriptEnabled = true
it.settings.loadWithOverviewMode = true
it.settings.useWideViewPort = true
// Configure a WebViewClient to handle navigation events
it.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
// Return false to allow the WebView to handle the URL
return false
}
}
// Configure a WebChromeClient (optional)
it.webChromeClient = object : WebChromeClient() {}
// Generate HTML content to embed the PDF
val htmlContent = getPDFHtml(url)
// Load the HTML content into the WebView
it.loadData(htmlContent, "text/html" , "utf-8" )
}
}
// This function generates the HTML content to embed the PDF.
private fun getPDFHtml(url: String): String {
return "" "
<!DOCTYPE html>
<html>
<head>
<meta name= "viewport" content= "width=device-width, initial-scale=1, user-scalable=no" >
<style>
body, html {
margin: 0 ;
height: 100 %;
overflow: hidden;
}
iframe {
position: absolute;
top: 0 ;
left: 0 ;
width: 100 %;
height: 100 %;
border: none;
}
</style>
</head>
<body>
<iframe src= "$url" allow= "autoplay" ></iframe>
</body>
</html>
"" "
}
} |
Output: