GithubHelp home page GithubHelp logo

android-pdf.js's Introduction

Noitice: I have stopped devlopment of this project (as pdf.js is unstable and not fit enough for production level) and moved to PlugPDF SDK. I will not reply to any questions or issues from now on. Feel free to fork the repo and make it better.

Android-pdf.js

Mozilla pdf.js implementation for android.

This project is a fork of http://www.bitbucket.org/butelo/pdfviewer. All credits goes to the original author.

This project is modified in order to open any pdf file from external sd card of the device into the webview in the app. Feel free to include the code in your project.

Usage:

    Uri path = Uri.parse(Environment.getExternalStorageDirectory().toString() + "/data/test.pdf");
    webView.loadUrl("file:///android_asset/pdfviewer/index.html?file=" + path);
    
    Update the 'path' variable to point to a valid pdf in the android filesystem.

License:

Copyright 2014 Mozilla Foundation

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

android-pdf.js's People

Contributors

pauldmps avatar

Stargazers

 avatar ZhangKe avatar Riski Wahyu  avatar  avatar Deepak Ghimire avatar Benjamin Cheng avatar  avatar  avatar Michael Angelo avatar Sakhawat Hossain avatar iFly丶 avatar misswang avatar Sylvain Marroufin avatar jzamir avatar  avatar zf avatar  avatar TuZanHua avatar  avatar 李希杰 avatar Vinay Kumar Sharma avatar Ricardo Rover avatar James avatar Luiz Felipe avatar DuckyDog avatar Gonçalo Palaio avatar  avatar Roger (Alex S) avatar  avatar Marcus Motill avatar Mohamed Abdul Rahim avatar Jeyan Burns-Oorjitham avatar fxwan avatar  avatar José Delgado avatar Arthur Wang avatar Yufeng Wu avatar Jan Weitz avatar Andrey Ilyukhin avatar  avatar trietbui85 avatar Andrew Orlov avatar Ryan Lothian avatar Macro Zhang avatar 行走的土豆 avatar footearth avatar Artur Rybak avatar Zhi Hong Liang avatar Robert Angeli avatar Erik Haugrud avatar Stephan Kromberg avatar ziem avatar Ali avatar  avatar Jaison Brooks avatar Galaad Linosfil avatar 袁浩东 avatar David Ryan avatar Niko avatar Jawaring avatar  avatar Jorge avatar Max Muth avatar Jeff Wright avatar

Watchers

Himanshu avatar James Cloos avatar  avatar Erdem YILMAZ avatar Jeff Wright avatar  avatar  avatar  avatar

android-pdf.js's Issues

Always starts at first page of PDF

Hi,

I load the PDF like this:
pdfView.loadUrl("file://" + getFilesDir() + "/index.html?file=" + path);

Then I do this:
pdfView.loadUrl("javascript:onGotoPage(" + currentPage + ")");

I have written my own function in customview.js:
function onGotoPage(pageNo) {
if (pageNo < 1 || pageNo > pdfDoc.numPages) {
return;
}
pageNum = pageNo;
queueRenderPage(pageNum);
}

But it always displays the first page.
If I call this function from an event, like a button press, it works fine. But when I call this function the first time just after the file has been loaded, it doesn't work and displays the first page.

I think it has something to do in javascript? I have not much experience in java script!

Showing "Page: /" on white page

I've added the lines:

fileOutputStream.write(line.getBytes());
fileOutputStream.close();

after the line:

FileOutputStream fileOutputStream = openFileOutput("index.html", Context.MODE_PRIVATE);

And only see a white page with "Page: /" displaying.

Here's some more context of the code:

webView.setWebChromeClient(new WebChromeClient());
        Uri path = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "Info-1.pdf");//Uri.parse(Environment.getExternalStorageDirectory().toString() + "/data/test.pdf");

        try {
            InputStream ims = getAssets().open("pdfviewer/index.html");
            String line = getStringFromInputStream(ims);
            if(line.contains("THE_FILE")) {
                line = line.replace("THE_FILE", path.toString());

                FileOutputStream fileOutputStream = openFileOutput("index.html", Context.MODE_PRIVATE);
                fileOutputStream.write(line.getBytes());
                fileOutputStream.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }




        webView.loadUrl("file://" + getFilesDir() + "/index.html");

I don't understand what I'm doing wrong.

Problems passing parameters into WebView on older versions of Android

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.

blurry

I tried your code on a tablet, the page renders not full screen, and when I enlarge it, the text gets blurry. Seems to be no way to fix that.

Android - pdf. js works on local pdf file

I have to open the local pdf file which reside in my android asset folder, can I able to open the local pdf files using this? If it's possible how I can call my local pdf file path.
Path= ???????
webView.loadUrl("file:///android_asset/pdfviewer/index.html?file=" + path);
Thanks!

Does not work on Android 4.3

Hi,
I tested with android 4.3 and I got the following error:

08-19 11:56:57.691: I/Web Console(7597): Warning: Unhandled rejection: RangeError: ArrayBuffer length minus the byteOffset is not a multiple of the element size.
08-19 11:56:57.691: I/Web Console(7597): at Object.MurmurHash3_64_update as update

With Android 4.4 works.
Could you check and correct this error? I would be grateful.
Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.