简体   繁体   English

Pdf 渲染器 API Android 来自 URL

[英]Pdf Renderer API Android From URL

I am looking into the PDF renderer API native to Google Android development.我正在研究PDF 渲染器 API原生于 Google Android 开发。 I see the following code example in the documentation:我在文档中看到以下代码示例:

 // create a new renderer
 PdfRenderer renderer = new PdfRenderer(getSeekableFileDescriptor());

 // let us just render all pages
 final int pageCount = renderer.getPageCount();
 for (int i = 0; i < pageCount; i++) {
 Page page = renderer.openPage(i);

 // say we render for showing on the screen
 page.render(mBitmap, null, null, Page.RENDER_MODE_FOR_DISPLAY);

 // do stuff with the bitmap

 // close the page
 page.close();
 }

 // close the renderer
 renderer.close();

I think this example uses from File Object. How I can get this API to work with a URL from a webserver, such as a document from a website?我认为这个例子使用的是文件 Object。我如何才能让这个 API 与来自网络服务器的 URL 一起工作,例如来自网站的文档? How can I load a PDF natively in an Android app that does not require a download of the file onto the local storage?如何在不需要将文件下载到本地存储的 Android 应用程序中本地加载 PDF? Something like how you can run the Google docs viewer to open the PDF in webview - but I cannot take that approach because the Google docs viewer is blocked in the environment I am in.类似于如何运行 Google 文档查看器以在 webview 中打开 PDF - 但我不能采用这种方法,因为 Google 文档查看器在我所在的环境中被阻止。

how I can get this API to work with URL from a webserver? 我如何才能使用此API来处理来自网络服务器的URL?

Download the PDF from the server to a local file. 将PDF从服务器下载到本地文件。 Then, use the local file. 然后,使用本地文件。

The purpose of what I am trying to learn is how to load pdf natively in android app that does not require a download of the file onto the local storage 我想学习的目的是如何在Android应用程序中本地加载pdf,不需要将文件下载到本地存储

AFAIK, you cannot use PdfRenderer that way. AFAIK,你不能用那种方式使用PdfRenderer It needs a seekable FileDescriptor , and the only way that I know of to create one of those involves a local file. 它需要一个可搜索的FileDescriptor ,而我知道创建其中一个的唯一方法是使用本地文件。

You cannot use Pdf Renderer to load URL. 您无法使用Pdf渲染器加载URL。 But your can make use of Google Docs in your webview to load URL without downloading the file... 但您可以在网页浏览中使用Google文档加载网址而无需下载文件...

webView.loadUrl("https://docs.google.com/gview?embedded=true&url=" + YOUR_URL);

I would first download the pdf and then show it in a pdfView我会先下载 pdf 然后在 pdfView 中显示

 private fun downloadPdf(): File? {


        val client = OkHttpClient()
        val request = Request.Builder().url(urlString)
            .addHeader("Content-Type", "application/json")
            .build()
        val response = client.newCall(request).execute()

        val inputStream: InputStream? = response.body?.byteStream()
        
        val pdfFile = File.createTempFile("myFile", ".pdf", cacheDir)
        inputStream?.readBytes()?.let { pdfFile.writeBytes(it) }

        return pdfFile


    } 

and then do something like this:然后做这样的事情:

CoroutineScope(IO).launch {
                        val pdfDownloaded = downloadPdf()
                        if (pdfDownloaded != null) {
                            pdfView.fromFile(pdfDownloaded)
                        }

                        withContext(Main) {
                            pdfView.visibility = View.VISIBLE
                            hideProgress()
                            pdfView.show()
                        }

                    }

here这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM