Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

It is possible to display pdf received bytes from service in web view in Android

I am receiving bytes of data like

JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQp4nNVZyY7jNhC991foHCBOVXERBQQBJFu+D9BAfiALkEOAzGV+f15Ra2shqaQz6IFhWzJp8tX+WKIbV19e/qmo+pFw6VnwWTf6+fn36tcfqr/joL4+//nSvb44fwtVze7mq9ffqp+eXDFVr3/8TDy+hAxZcuSpxl3AXYOrFvcdrpju+H7gyuG7pydmeHL4I7OwwZdlvdHRhv0vr3+99K8vnw5BkL3JCYjpdSfPgMGBG2zm5uVbAGhx4fHucH/H+5HazEMlTdlmQG655ycTWdGBTli1AdXgApqYJZ7gbH8Xs/mfHSCKaqUVj18zuvGBb+4S3BA3rqORsOU5NBjTMa/n446SYGoqNZSEuH0nDZaGgegprW4hHcb9BGMGd5fBjIPnXNOQC/i8oKG30OgOGMvdBGg9xy+zyOssmDU5zho0Ngna1qVmlQ7msTMwEpgp6qoWw608YMhRmwIDcwcgfQyVa1o0HpmiLAZr3gf/DE8hDNuNEHrpkxuLLTWf5pLFdyAb5Fz50Grr5IYkOUnlqZZNLeIautUZ1NNrcLqV9xtYCKrrY9JSCZroeNFeuHvgOzMb0cuH8SSYC3uzSYKvm5stA7/Kt/dSUYAtqBMeh7yGeBKcz3nDBK2VbgfwLi3KFsIXvwd5cFBjQh8AltwUn6XmnHwkINQ6OGQnD9XKNQ1t/50EZ93NXAMXDdBCEWOl3moluZ0p9I2YA5+DIZILMpfiP43wC6o1m1WmmCDWYiy9MVgDCXqMJGjKWCiHYlaN/5Re556JZMOZTCkl2KbZ0Y67AoOYJqZpJHLjYvLuBqgH4xSr+izMfob+glI6ior7UaAktLrOVvW3MZewkqb7spiz3mWJ2BX30CKhxU+reBcrsxk4j8RAiGOaaHfzVo6hm8xkrcQZOJxJkhTd2V31/yjOYE2pM5TGP7hz

this bytes are related to pdf.
and String cannot able to store this much of data. So I am using stringbuffer to store this. But stringbuffer also cannot able to store. so how to store this received data.
how to convert this bytes and show in web view.

Update:

I am using the following code.

byte[] decodedString = Base64.decode(ByteCode.toString());
wv.loadData(decodedString.toString(), "application/pdf", "utf-8");

where ByteCode is a type StringBuffer holding the response bytecode from service.

like image 662
Boggarapu Avatar asked Mar 19 '14 07:03

Boggarapu


People also ask

How to show PDF in Android WebView?

This example demonstrate about How to show pdf in android webview. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken web view to show pdf.

Is it possible to display a PDF from a byte array?

I’m hitting a service that generates a pdf based on data sent it and returns the pdf in a byte array. Is there a way to use this plugin to display the pdf? ← How to print a pdf? Yeah, it’s possible. The ‘source’ input can handle with string | Uint8Array | PDFDocumentProxy. In my application i am using an Uint8Array.

How can I view PDFs in my browser?

You can use pdfObject to view pdfs in your browser. Just create a div with some id and insert the byte array that you get into that div. PDFObject.embed (<byte array>, "#pdfObjectViewer"); You need to download the pdfObject script and include it in your project from their site for this to work. or you can use this CDN.


2 Answers

It is not possible to preview pdf document in Android webview. It requires third-party library.

build.Gradle

compile 'com.github.barteksc:android-pdf-viewer:2.7.0'

dialog_pdf_viewer

<?xml version="1.0" encoding="utf-8"?>

<!--
  ~ Copyright (c) 2017.
  ~ Samet Öztoprak
  ~ All rights reserved.
  -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/dialog_pdf_viewer_close"
        style="@style/ExitButtonImageViewStyle"
        android:src="@drawable/popup_exit" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/white"
        android:orientation="vertical">

        <com.github.barteksc.pdfviewer.PDFView
            android:id="@+id/pdfView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

    <View style="@style/HorizontalLine" />

    <com.pozitron.commons.customviews.ButtonFont
        android:id="@+id/dialog_pdf_viewer_button"
        style="@style/ButtonPrimary2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="15dp"
        android:text="@string/agreed" />

</LinearLayout>

DailogPDFViewer.java

public class DialogPdfViewer extends Dialog {
    PDFView pdfView;
    byte[] decodedString;

    public interface OnDialogPdfViewerListener {
        void onAgreeClick(DialogPdfViewer dialogFullEula);

        void onCloseClick(DialogPdfViewer dialogFullEula);
    }

    public DialogPdfViewer(Context context, String base64, final DialogPdfViewer.OnDialogPdfViewerListener onDialogPdfViewerListener) {
        super(context);

        setContentView(R.layout.dialog_pdf_viewer);
        findViewById(R.id.dialog_pdf_viewer_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onDialogPdfViewerListener.onCloseClick(DialogPdfViewer.this);
            }
        });

        findViewById(R.id.dialog_pdf_viewer_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onDialogPdfViewerListener.onAgreeClick(DialogPdfViewer.this);
            }
        });

        decodedString = Base64.decode(base64.toString(), Base64.DEFAULT);

        pdfView = ((PDFView) findViewById(R.id.pdfView));
        pdfView.fromBytes(decodedString).load();

        setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
                    onDialogPdfViewerListener.onCloseClick(DialogPdfViewer.this);
                }
                return true;
            }
        });

    }
}
like image 90
Samet ÖZTOPRAK Avatar answered Oct 21 '22 21:10

Samet ÖZTOPRAK


From your comments, it appears that your data is not too long for a string, merely too long for logging to display the string. (Logging has a limit of around 4000 characters).

You seem to be working along the right lines

  • you read the data in to a string
  • you then base64 decode that data

However, trying to just load that data into a web view is not supported on most (if not all) versions of Android.

I suggest that instead you

  • write the data out to a file
  • issue an Intent specifying a FileProvider content URI so that the user's preferred PDF viewer will be used to view the file.
like image 45
zmarties Avatar answered Oct 21 '22 22:10

zmarties