Isaac Wasserman
Isaac Wasserman

Reputation: 1541

How do you use pdf.js to get text content of client stored pdf?

I am working on a web app that needs to read a pdf with pdf.js. I have a file input on my page that chooses the file. I have the document.getElementById("...").value as the pdf's url. When I run it, it says:

"XMLHttpRequest cannot load c:\fakepath\vocab_list_15_8th_grade.pdf. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource. awe:1 Unhandled promise rejection Promise {[[PromiseStatus]]: "rejected", [[PromiseValue]]: UnknownErrorException}"

I imagine this is because the file is stored locally. How do I get around this error?

Upvotes: 1

Views: 800

Answers (1)

Josh Haberman
Josh Haberman

Reputation: 4210

This doesn't really have anything to do with pdf.js per se. pdf.js just happens to be trying to load a file using XMLHttpRequest.

The problem is that you gave pdf.js a path to a local file (c:\fakepath\vocab_list_15_8th_grade.pdf). Your web browser won't let a web page load the local file for security reasons, even if the web page is also local. The way to get around this is just to run a web server locally that contains both your web app and your PDF.

If you have Python installed you can do this really easily by running:

$ python -m SimpleHTTPServer

...from the directory you want to serve from.

But it looks like you're on Windows (judging from the C:\ in your path). I don't know the easiest way to run a simple web server on Windows without having Python installed.

Upvotes: 2

Related Questions