Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

118
Visualizações
Opening a html link to read a PDF file on browser is blocked by browser

I can't open a PDF at a tab in a browser from a html link.

<a href="https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf" target="_blank">PDF</a>

Problem:

enter image description here

But, if I open the link directly on the browser (https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf), that will open in the browser without any issue.

How to solve this?

Fiddle Demo

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can get around it with a proxy script (on the same domain):

PHP example

Save code below as test.php, and start server with php -S localhost:1234 test.php. Go to http://localhost:1234.

if(isset($_GET['url']) && strpos($_GET['url'],"http") > -1) {
    header('Content-type: application/pdf');
    echo file_get_contents($_GET['url']);
    exit;
}
?>

<a href="?url=https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf" target="_blank">PDF</a>

Update; pure javascript version

With this code you can download the pdf through a proxy (api.allorigins.win) which avoids Same-origin policy problems.

After that the pdf is saved with window.URL.createObjectURL and opened in a new window.

<script>
function loadPdf(url)
{
    var req = new XMLHttpRequest();
    req.open("GET", "https://api.allorigins.win/raw?url=" + encodeURIComponent(url), true);
    req.responseType = "blob";
    req.onreadystatechange = function ()
    {
        if (req.readyState === 4 && req.status === 200){
            var blob=new Blob([req.response], {type: 'application/pdf'});
            var link=document.createElement('a');
            link.target = "_blank";
            link.href=window.URL.createObjectURL(blob);
            link.click();
        }
    };
    req.send();
}
</script>

<a href="javascript:loadPdf('https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf');">PDF</a>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda