0

On an html page I have an <object> that hosts a pdf. I would need to access the binary data of the pdf via Javascript, but I cannot figure out how to accomplish that. I get access to the object element itself but cannot think of a method for getting the data in it.

Is it possible at all?

2
  • 1
    Just one word .. WHY ? why are you using javascript for this ? why do you want to do this at all ? Commented Jan 6, 2012 at 12:05
  • I am running an algorithm which takes the pdf the user is currently reading in the browser as input. Commented Jan 6, 2012 at 13:04

1 Answer 1

2

You can not get the binary from an object tag, but you can make an AJAX request to the server and get it as ArrayBuffer by using the new responseType attribute:

var http = new XMLHttpRequest();

http.open("get", "somefile.pdf", true);
http.responseType = "arraybuffer";

http.onload = function(e)
{
    if(http.response)
    {
        // http.response contains the file
    }
};

http.send(null);

Note that this method only works in newer browsers and is obviously restricted by the Same-Origin-Policy.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the explanation. But I tried to read the data from the object tag because of Same-Origin-Policy problems. As you may already recognized I am very new to JS and AJAX. What I want to accomplish is to be able to get access to any pdf file the browser would reach by user interaction. Would there be a way you could think of? Tanks, Armin
There is no way to get around this policy; it would be useless otherwise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.