In the documentation, we are shown to use ?file=" + path. This works fine on 4.4. On 4.1.1, it does not work. In my researching, it would seem that other versions might not accept this either. It is a flaw in WebView, not this project. I'm listing information here for anyone targeting such a version, so feel free in closing this as an issue.
In summary of what I show below, I generate the index.html file on-the-fly, using the current index.html as a template, in order to get the filename of the PDF I want to show passed into the JavaScript.
I wanted to pass information from the WebView directly into the JavaScript, but since pdf.js seems to load immediately, I didn't see a way to load the HTML and then pass the information. If pdf.js could be called, then we could do this more cleanly. Maybe we can, but this was quicker for me to resolve.
Currently, there's a pdffile.js in this project which has the following which reads that "file" parameter:
var url = getURLParameter('file');
I removed that file and instead added this to index.html:
<script type="text/javascript">
var url = 'THE_FILE';
</script>
Then, in the activity (or fragment in my case), I read the index.html file:
InputStream ims = getActivity().getApplicationContext().getAssets().open("pdfviewer/index.html");
etc.
As I'm reading it, I do this:
if(line.contains("THE_FILE")) {
line = line.replace("THE_FILE", path.toString());
}
thereby replacing the string "THE_FILE" with the uri of the PDF file.
I write the file to internal storage:
FileOutputStream fileOutputStream = getActivity().getApplicationContext().openFileOutput("index.html", Context.MODE_PRIVATE);
Finally, I direct my WebView to that file:
webView.loadUrl("file://" + getActivity().getApplicationContext().getFilesDir() + "/index.html");
Since we're reading from internal storage, in order to find the .js files, we need to change their paths. Here is an example:
<script type="text/javascript" src="file:///android_asset/pdfviewer/pdf.js"></script>
After removing pdffile.js, there are 3 such lines which need changed.
In the documentation, we are shown to use ?file=" + path. This works fine on 4.4. On 4.1.1, it does not work. In my researching, it would seem that other versions might not accept this either. It is a flaw in WebView, not this project. I'm listing information here for anyone targeting such a version, so feel free in closing this as an issue.
In summary of what I show below, I generate the index.html file on-the-fly, using the current index.html as a template, in order to get the filename of the PDF I want to show passed into the JavaScript.
I wanted to pass information from the WebView directly into the JavaScript, but since pdf.js seems to load immediately, I didn't see a way to load the HTML and then pass the information. If pdf.js could be called, then we could do this more cleanly. Maybe we can, but this was quicker for me to resolve.
Currently, there's a pdffile.js in this project which has the following which reads that "file" parameter:
var url = getURLParameter('file');
I removed that file and instead added this to index.html:
<script type="text/javascript">var url = 'THE_FILE';</script>Then, in the activity (or fragment in my case), I read the index.html file:
InputStream ims = getActivity().getApplicationContext().getAssets().open("pdfviewer/index.html");etc.
As I'm reading it, I do this:
if(line.contains("THE_FILE")) {line = line.replace("THE_FILE", path.toString());}thereby replacing the string "THE_FILE" with the uri of the PDF file.
I write the file to internal storage:
FileOutputStream fileOutputStream = getActivity().getApplicationContext().openFileOutput("index.html", Context.MODE_PRIVATE);Finally, I direct my WebView to that file:
webView.loadUrl("file://" + getActivity().getApplicationContext().getFilesDir() + "/index.html");
Since we're reading from internal storage, in order to find the .js files, we need to change their paths. Here is an example:
<script type="text/javascript" src="file:///android_asset/pdfviewer/pdf.js"></script>After removing pdffile.js, there are 3 such lines which need changed.