android - html 使用 kotlin webview 和 PdfDocument() 转换为 pdf

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

我有一个 HTML 文件,我正在尝试将其转换为 PDF 版本,使用 WebViewPdfDocument.

我尝试过的事情:

  1. 在布局中显示 HTML - 这工作正常。
    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. 然后尝试将 TextView 输出到 PDF - 这也可以。
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. 然后我尝试结合第 1 点和第 2 点;然而这给了我一个空白 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
...

我唯一的改变是背景颜色。

关于这段代码可能有什么问题有什么想法吗?

所以按照 CommonsWare 的说明,我得到了实现相同结果的更简单方法,看起来是这样的:

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()
        )
    }
}