pl_stan
pl_stan

Reputation: 11

android - html to pdf with kotlin webview and PdfDocument()

I have a HTML file and I'm trying convert it to its PDF version, using WebView and PdfDocument.

What I've tried to do:

  1. Showing the HTML in layout - this works OK.
    private fun createDocument(uri: Uri) {
        val document = PdfDocument()
        val pageInfo = PageInfo.Builder(595, 842, 1).create()
        val page = document.startPage(pageInfo)
        val rect = Rect()
        rect.set(0, 0, 595, 842)

        val view = TextView(this)
        view.text = "ANY TEXT"

        // formatting output
        view.setBackgroundColor(getColor(R.color.colorAccent))
        val widthSpec = View.MeasureSpec.makeMeasureSpec(rect.width(), View.MeasureSpec.EXACTLY)
        val heightSpec = View.MeasureSpec.makeMeasureSpec(rect.height(), View.MeasureSpec.EXACTLY)
        view.measure(widthSpec, heightSpec)
        view.layout(0, 0, rect.width(), rect.height())
        view.draw(page.canvas)
        document.finishPage(page)
        try {
            contentResolver.openFileDescriptor(uri, "w")?.use {
                FileOutputStream(it.fileDescriptor).use {
                    document.writeTo(it);
                    document.close();
                }
            }
        } catch...
  1. Then tried outputting TextView to PDF - this works OK as well.
setContentView(R.layout.activity_show)

val towebView = findViewById<WebView>(R.id.webView)
towebView.webViewClient = object : WebViewClient() {
   override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
       view?.loadUrl(url)
       return true
   }
}
towebView.loadUrl("file:///android_asset/protokol.html")
  1. I've then tried combining point 1 & 2; however this gives me a blank PDF:
    private fun createDocument(uri: Uri) {
        val document = PdfDocument()
        val pageInfo = PageInfo.Builder(595, 842, 1).create()
        val page = document.startPage(pageInfo)
        val rect = Rect()
        rect.set(0, 0, 595, 842)

        val view = WebView(this)
        view.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                view?.loadUrl(url)
                return true
            }
        }
        view.loadUrl("file:///android_asset/protokol.html")

      //formatting output
...

The only change I have is the background colour.

Any ideas on what might be wrong with this code?

Upvotes: 0

Views: 1704

Answers (1)

pl_stan
pl_stan

Reputation: 11

So following CommonsWare's instructions I got simpler way of achieving same result, which looks this way:

private var mWebView: WebView? = null

private fun createDocument() {
    val view = WebView(this)
    view.webViewClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            view?.loadUrl(url)
            return true
        }
        override fun onPageFinished(view: WebView, url: String) {
            createWebPrintJob(view)
            mWebView = null
        }
    }
    view.loadUrl("file:///android_asset/protokol.html")
    mWebView = view
}

private fun createWebPrintJob(webView: WebView) {
    (this?.getSystemService(PRINT_SERVICE) as? PrintManager)?.let { printManager ->
        val jobName = "${getString(R.string.app_name)} Document"
        val printAdapter = webView.createPrintDocumentAdapter(jobName)
        printManager.print(
            jobName,
            printAdapter,
            PrintAttributes.Builder().build()
        )
    }
}

Upvotes: 1

Related Questions