views:

1606

answers:

3

I have a web application that is dynamically loading PDF files for viewing in the browser. Currently, it uses "innerHTML" to replace a div with the PDF Object. This works.

But, is there a better way to get the ID of the element and set the "src" or "data" parameter for the Object / Embed and have it instantly load up a new document? I'm hoping the instance of Adobe Acrobat Reader will stay on the screen, but the new document will load into it.

Here is a JavaScript example of the object:

document.getElementById(`divPDF`).innerHTML = `<OBJECT id='objPDF' DATA="'+strFilename+'" TYPE="application/pdf" TITLE="IMAGING" WIDTH="100%" HEIGHT="100%"></object>`;

Any insight is appreciated.

+1  A: 

I am not sure if this will work, as I have not tried this out in my projects.

(Looking at your JS, I believe you are using jQuery. If not, please correct me)

Once you have populated the divPDF with the object you might try the code below:

$("objPDF").attr({
    data: "dir/to/newPDF"
});

Again, I am not sure if this will work for your particular needs but if you attach this code to an event handler you can switch out the data of the object.

You could also wrap it in a function to be used over and over again:

function pdfLoad(dirToPDF) {
    $("objPDF").attr({
        data: dirToPDF
    });
}
Lark
A: 

If the handler for the PDF is acrobat (it doesn't have to be), it exposes a JS interface that is documented here:

http://www.adobe.com/devnet/acrobat/pdfs/js_api_reference.pdf

See if you can call openDoc(urlToPdf) on document.getElementById('objPDF') -- even if this works, it only works when Acrobat is being used to handle 'application/pdf'

Lou Franco
A: 

@lark A slight correction:

$('#objPDF').attr('data','dirToPDF');

The # specifies the objPDF is an ID and not an element name. Though I still don't know if this will work.

@Tristan Take a look at the jQuery Media plugin. It mentions support for PDF as well, though I have never used it.

Shinhan