最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Implementing pdf.js in android to read from sd card - Stack Overflow

programmeradmin1浏览0评论

I'm trying to implement a pdf viewer for my android app which displays different pdf files which are present in the SD card.

I am thinking of using the pdf.js library.I used the code sample as posted here: /

However, the library takes the pdf url in the javascript file which is relative address to the folder to which it belongs (/assets/pdfviewer).

<script type="text/javascript">
var url = '../pressed.tracemonkey-pldi-09.pdf';
</script>

How can I redirect it to use a pdf present in a folder in the sdcard ?

Also the filenames of the pdfs are not fixed and I need to change them in the program as per requirement.

Update --------------------------------------------------------------------------------------------------------------------------

I updated the java code like this:

Uri path = Uri.parse(Environment.getExternalStorageDirectory().toString() + "/data/test.pdf");
        webView.loadUrl("file:///android_asset/pdfviewer/index.html?file=" + path);

In the pdffile.js, I modified the following:

From:

<script type="text/javascript">
var url = '../pressed.tracemonkey-pldi-09.pdf';
</script>

To:

var url = getURLParameter('file');


function getURLParameter(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null}

The above javascript code extracts the 'file' parameter from the URL of the 'index.html'

Still does not work. The webview 'chromium' in logcat shows:

I/chromium(1353): [INFO:CONSOLE(106)] "Warning: Unhandled rejection: 
Unexpected server response (0) while retrieving PDF "file:///storage/sdcard0/data/test.pdf".", source: file:///android_asset/pdfviewer/pdf.js (106)"

This seems to be cross server issue. So how can I modify the pdf.js code to read local files without server ?

I'm trying to implement a pdf viewer for my android app which displays different pdf files which are present in the SD card.

I am thinking of using the pdf.js library.I used the code sample as posted here: https://bitbucket/butelo/pdfviewer/

However, the library takes the pdf url in the javascript file which is relative address to the folder to which it belongs (/assets/pdfviewer).

<script type="text/javascript">
var url = '../pressed.tracemonkey-pldi-09.pdf';
</script>

How can I redirect it to use a pdf present in a folder in the sdcard ?

Also the filenames of the pdfs are not fixed and I need to change them in the program as per requirement.

Update --------------------------------------------------------------------------------------------------------------------------

I updated the java code like this:

Uri path = Uri.parse(Environment.getExternalStorageDirectory().toString() + "/data/test.pdf");
        webView.loadUrl("file:///android_asset/pdfviewer/index.html?file=" + path);

In the pdffile.js, I modified the following:

From:

<script type="text/javascript">
var url = '../pressed.tracemonkey-pldi-09.pdf';
</script>

To:

var url = getURLParameter('file');


function getURLParameter(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null}

The above javascript code extracts the 'file' parameter from the URL of the 'index.html'

Still does not work. The webview 'chromium' in logcat shows:

I/chromium(1353): [INFO:CONSOLE(106)] "Warning: Unhandled rejection: 
Unexpected server response (0) while retrieving PDF "file:///storage/sdcard0/data/test.pdf".", source: file:///android_asset/pdfviewer/pdf.js (106)"

This seems to be cross server issue. So how can I modify the pdf.js code to read local files without server ?

Share Improve this question edited Jul 24, 2014 at 15:33 Shantanu Paul asked Jun 18, 2014 at 17:48 Shantanu PaulShantanu Paul 70510 silver badges27 bronze badges 2
  • 2 Did you find a solution? I faced with the same issue – Mr Kohn Doew Commented Jun 28, 2014 at 11:52
  • Yes, see my question details as well as my answer. – Shantanu Paul Commented Jul 25, 2014 at 16:55
Add a ment  | 

1 Answer 1

Reset to default 3

Solved the issue. The app was missing READ_EXTERNAL_STORAGE permissions.

The code can be found at: https://github./pauldmps/Android-pdf.js Its Apache V2 license, so feel free to use in your app.

发布评论

评论列表(0)

  1. 暂无评论