How to create PDF from webview in android?

Viewed 15808

I am displaying webpage in webview. Now how to create PDF from webview ?

For Example : webview loads URL is "www.google.co.in". Now how to save this webpage as image and create pdf ??

any help would be appreciated

3 Answers

if can not want return path

private fun createWebPrintJob(webView: WebView) {
    val printManager = this.getSystemService(Context.PRINT_SERVICE) as PrintManager
    val printAdapter = webView.createPrintDocumentAdapter()
    val jobName = getString(R.string.app_name) + " Print Test"
    printManager.print(
        jobName, printAdapter,
        PrintAttributes.Builder().build()
    )
}

if can want return path of PDF

try this

lib :

implementation 'com.github.pramodkr123:ConvertWebViewToPdfDemo:1.0.4'

Kotlin CODE:

    private fun sentEmailAndSavePDF() {
    val directory =
        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM + "/MDP-PDF/")
    val min = 1000
    val max = 9999
    val random = Random().nextInt(max - min + 1) + min
    val fileName = "MDP_$random.pdf"

    PdfView.createWebPrintJob(
        this@WebViewActivity,
        binding.webview,
        directory,
        fileName,
        object : PdfView.Callback {
            override fun success(p0: String?) {

                Log.e(TAG, "success: ")
                   //p0  is your pdf PATH 
            }

            override fun failure() {

            }

        })
}
Related