Search code examples
javascriptpdfcontent-type

How to set content type to open pdf in new tab using javascript


I am using javascript. I have a pdf file stored on server which i need to open in new tab using javascript. I have tried the below code

function openPDF() {
  var urlToPdfFile = "http://www.demosite.com/pdf/demo.pdf"; //something like this
  window.open(urlToPdfFile);
}

But I keep getting error like below

Unknown content type requested.

URL: '/pdf/demo.pdf'.

Requested extension: '.pdf'.

This because I see the content-type is html in the request.

how can I open PDF in new tab using javascript, by providing the content-type...?

Thanks


Solution

  • Try this code, it works:

    function openPDF(urlToPdfFile) {
      window.open(urlToPdfFile, 'pdf');
    }
    
    openPDF('http://www.demosite.com/pdf/demo.pdf');