Alejandro Garcia Anglada
Alejandro Garcia Anglada

Reputation: 2403

Preview pdf with http headers

I'm trying to preview a pdf in the browser with just this HTML code:

<div class="col-xs-12 clearfix">
    <object width="100%" height="600px" data="<%- model.pdf %>" type="application/pdf">
        <p>It appears you don't have Adobe Reader or PDF support in this web browser. <a href="<%- model.pdf %>">Click here to download the PDF</a>. Or <a href="http://get.adobe.com/reader/" target="_blank">click here to install Adobe Reader</a>.</p>
        <embed width="100%" height="600px" src="<%- model.pdf %>" type="application/pdf" />
    </object>
</div>

Where model.pdf is an url that I generate to request it from there.

The problem:

I'm working in a full javascript/fe project that is consumer of an API. As client, I need a token and some more things to request data from the API that I have to set in the headers of every call.

So what I need is, somehow to set those headers somewhere.

My question:

Is is possible?

Thanks in advance.

Upvotes: 2

Views: 2858

Answers (1)

Ali Mamedov
Ali Mamedov

Reputation: 5256

Maybe PDF.js will be useful for you.

The object structure of PDF.js loosely follows the structure of an actual PDF. At the top level there is a document object. From the document, more information and individual pages can be fetched. To get the document:

PDFJS.getDocument('helloworld.pdf')

For headers try this:

var 
    obj = {};

obj.url = 'url_to_your_file.pdf';
obj.httpHeaders = {
    
    "X-Header": "VALUE"
};
  
PDFJS.getDocument(obj).then(function(pdf) {
  
    // your code
});

You read more about DocumentInitParameters here.

Upvotes: 2

Related Questions