0

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?

Sanda
  • 1,357
  • 19
  • 29
pl_stan
  • 11
  • 4
  • You could [use `createPrintDocumentAdapter()` on `WebView`](https://developer.android.com/reference/android/webkit/WebView?hl=en#createPrintDocumentAdapter(java.lang.String)) and bypass most of this code. – CommonsWare Mar 12 '20 at 13:58
  • Nice. That is truly much simpler. – pl_stan Mar 12 '20 at 19:32
  • You might want to answer your own question and show what you wound up with for an implementation. Regardless, I'm glad to hear that you got it working! – CommonsWare Mar 12 '20 at 19:32

1 Answers1

1

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()
        )
    }
}
pl_stan
  • 11
  • 4