0

I want to display the PDF I got from an HTTP request. Now I will tell you in steps how I came to the link I have.

First I get a PDF from an HTTP request in the following format as a string:

%PDF-1.4
%����
2 0 obj
<</Length 474/Filter/FlateDecode>>stream
x����n�0��y��l������Jz��^lZ
...

Then I encoded it (base64) with this code:

byte[] pdf = pdfCode.getBytes();
String link = Base64.encodeToString(pdf, Base64.DEFAULT);

And now I have something like this:

VBERi0xLjQKJe+/ve+/ve+/ve+/vQoyIDAgb2JqCjw8L0xlbmd0aCA0NzQvRmlsdGVyL0ZsYXRl
RGVjb2RlPj5zdHJlYW0KeO+/ve+/ve+/ve+/vW7vv70wEO+/ve+/vXnvv73vv71s77+977+977+9
77+977+977+9Snrvv73vv71ebFrvv73vv73vv
...

What next, how to get a link from this that I can show?

Michael A.
  • 45
  • 8
  • "First I get a PDF from an HTTP request in the following format as a string" -- a PDF is not a string. PDF is a binary format. "how to get a link from this that I can show?" -- show... where? If you want to display a PDF on an Android device, that's not how you do it. – CommonsWare Mar 18 '20 at 23:05
  • I showed you above how I got the PDF, whatever it is (that code), it is in string format, since retrofit2 immediately convert it into a string. I want to display it in webview – Michael A. Mar 18 '20 at 23:08
  • "whatever it is (that code), it is in string format" -- then it may not be a valid PDF. "since retrofit2 immediately convert it into a string" -- then perhaps you misconfigured your Retrofit interface, or you should be using OkHttp and download it to a file. "I want to display it in webview" -- `WebView` has no built-in ability to display a PDF. – CommonsWare Mar 18 '20 at 23:13
  • I have this problem for a few months and I skip it every time because whenever I ask a question everyone corrects me but in the end no one gives a specific solution – Michael A. Mar 18 '20 at 23:16
  • For your specific question here, there is no solution. The best solution for most cases of displaying a PDF is to [download it as binary data to a file](https://stackoverflow.com/a/29012988/115145), then use `FileProvider` and `ACTION_VIEW` to let the user view the PDF in their preferred PDF app. – CommonsWare Mar 18 '20 at 23:21

0 Answers0