How to Open PDF From URL in Android Without Any Third Party Libraries? Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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 ImplementationStep 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 fileand also change access. Step 4: We'll be using html code to load our data in the WebView. Java 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 String pdfUrl = "https://drive.google.com/file/d/1JunydomffuJgX74LrK_PjM2YAOFYCbxCKd/preview"; // 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>"; } } Kotlin 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) val pdfUrl = "https://drive.google.com/file/d/1JunydoffmuJgX74LrK_PjM2YAOFYCbxCKd/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: Comment More infoAdvertise with us Next Article How to Open PDF From URL in Android Without Any Third Party Libraries? ayu5hkarn Follow Improve Article Tags : Java Kotlin Android Geeks Premier League Geeks Premier League 2023 +1 More Practice Tags : Java Similar Reads How to Load PDF from URL in Android? Most of the apps require to include support to display PDF files in their app. So if we have to use multiple PDF files in our app it is practically not possible to add each PDF file inside our app because this approach may lead to an increase in the size of the app and no user would like to download 5 min read How to Load Any Image From URL Without Using Any Dependency in Android? Many applications display images from the internet using third-party APIs like Glide and Picasso to load images. This means that such applications partly depend on these services to keep themselves working fine. To make the application better, one should write their own code rather than depending on 3 min read Load PDF From URL in Android with Kotlin PDF View is most of the applications to display the PDF files within the application to display the pdf within our own application rather than redirecting to another application. If we want to display multiple pdf files within our application we have to host these files and then access these files w 4 min read How to Play Video from URL in Android? In this article, you will see how to play a video from a URL on Android. For showing the video in our Android application we will use the VideoView widget. The VideoView widget is capable of playing media files, and the formats supported by the VideoView are 3gp and MP4. By using VideoView you can p 3 min read How to Use PRDownloader Library in Android App? PRDownloader library is a file downloader library for android. It comes with pause and resumes support while downloading a file. This library is capable of downloading large files from the internet and can download any type of file like image, video, pdf, apk and etc. It provides many features that 11 min read How to Use Universal Image Loader Library in Android? UIL (Universal Image Loader) is a similar library to that of Picasso and Glide which performs loading images from any web URL into ImageView of Android. This image loading library has been created to provide a powerful, flexible, and customizable solution to load images in Android from Server. This 4 min read How to Load Image From URL in Android using Jetpack Compose? Most of the applications in android use images for displaying some useful information within the applications. Images are considered as easy to convey information to the user. An android application uses more than 100 different types of images. So it is not practically possible to add all the images 4 min read How to Upload PDF Files in Firebase Storage in Android? Firebase is a mobile and web application development platform. It provides services that a web application or mobile application might require. Firebase provides secure file uploads and downloads for the Firebase application. This article explains how to build an Android application with the ability 3 min read Fresco Image Loading Library in Android with Example Fresco is one of the famous image loading libraries from URLs in Android. It is a powerful library for displaying and managing images from URLs. This library can load images from Users' devices, servers, and other local sources. The most important feature of this library is to show a placeholder ima 3 min read How to Build PDF Downloader App in Android with Kotlin? If you are creating apps for students or for educational purposes then you need to add some PDF files for displaying some data inside our app. These PDF files are updated on regular basis. A sample video is given below to get an idea about what we are going to do in this article. In this article, we 3 min read Like