0

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

我试图做的事情:

  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
...

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

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

4

1 回答 1

1

因此,按照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()
        )
    }
}
于 2020-03-12T22:28:27.647 回答