Why? In Android Open PDF using WebView Not a good idea.

Hitesh Dhamshaniya
2 min readDec 19, 2020

If you are thinking to open PDF using web-view, think again and drop the idea it will cost you many bad comments and reviews :)

When an android developer tries to search a solution to show PDF in an android easy way, one can find the solution to show PDF in webview, it seems an easy solution to implement. However, It is not a good solution. so, please change the mind and find another solution. I had implemented that in one of my projects and I get in trouble when it goes live.

Then I found a tiny library to show PDF into an android application. Here we will see how to implement that into our project. Below is the latest Gradle when I am writing this post. Always check for the updated version whenever we are going to implement it. First of all, we will add its Gradle dependency to our app module.

implementation 'com.pdfview:pdfview-android:1.0.0'

Then we add pdf view to our layout XML file as below.

<com.pdfview.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/_10sdp" />

We will load PDF from online, we need to write a function to get that pdf from URL, below is the number of function which will help to do so.

@SuppressLint("SetJavaScriptEnabled")
private fun initUI() {
var pdfUrl = "https://kotlinlang.org/docs/kotlin-docs.pdf"
val extras = intent.extras
extras?.let {
if (it.containsKey(Actions.KEY_EXTRA_PDF_INVOICE)) {
pdfUrl = it.getString(Actions.KEY_EXTRA_PDF_INVOICE)!!
}
}

CoroutineScope(Dispatchers.Main).launch {
val filePath = withContext(Dispatchers.IO) {
download(pdfUrl)
}
loadPDFFile(filePath)
}
}

private fun loadPDFFile(filePath: File?) {
if (filePath != null && filePath.exists()) {
mPDFFile = filePath
//pdfView.fromFile(filePath).load()
pdfView.fromFile(filePath).show()
}
progressView.visibility = View.GONE
}

private fun download(url: String): File? {
try {
// Get File Name from URL
val fileName = url.split("/").last()
val rootDir = applicationContext.cacheDir
val localFile = File(rootDir, fileName)
// Create File
localFile.createNewFile()
downloadPDFFile(url, localFile)
return localFile
} catch (ex: Exception) {
ex.printStackTrace()
return null
}
}

/**
* @param url: String
* @param localFile: File
*/
private fun downloadPDFFile(url: String, localFile: File): Boolean {
try {
// create url and connect
val connection: URLConnection = URL(url).openConnection()
connection.connect()
// download the file
val inputStream = BufferedInputStream(connection.getInputStream())
inputStream.use { input ->
val outputStream = localFile.outputStream()
outputStream.use { output ->
val buffer = ByteArray(4 * 1024) // buffer size
while (true) {
val byteCount = input.read(buffer)
if (byteCount < 0) break
output.write(buffer, 0, byteCount)
}
output.flush()
}
}
return true
} catch (ex: IOException) {
ex.printStackTrace()
}
return false
}

As we do not download it in external sd card, so we do need to ask write permission.

Thank you for reading..

--

--