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

187
Visualizações
Next.js download PDF functions open 2 tabs

I'm attempting to trigger a PDF download when landing on a page with next.js. When doing so it's opening another tab with the PDF which i'm fine with, but i'd like to open the PDF on another tab. So I added it with link.target ="_blank" on page load, there are 2 PDF tabs that are open now.

I've attempted get only 1 tab with the PDF to open but I am unable to. Is this an issue with next.js? How can I get my function to only 1 PDF tab.

I think the issue may be related to the server side render? If so how can I only call this function once?

Here is my download file function:

  function downloadFile(filePath) {
    if(process.browser) {

      var link = document.createElement('a');
      link.href = filePath;
      link.target = "_blank"
      link.download = filePath.substr(filePath.lastIndexOf('/') + 1);
      link.click();
    }
  }
  
  downloadFile("http://www.africau.edu/images/default/sample.pdf");

Here is my full next js page:

const Download= () => {

  function downloadFile(filePath) {
    if(process.browser) {

      var link = document.createElement('a');
      link.href = filePath;
      link.target = "_blank"
      link.download = filePath.substr(filePath.lastIndexOf('/') + 1);
      link.click();
    }
  }
  
  downloadFile("http://www.africau.edu/images/default/sample.pdf");

  return (
    <Layout>
      <div> download pdf</div>
    </Layout>
  );
};

export default Download;
about 4 years ago · Juan Pablo Isaza
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