Skip to content
Advertisement

How can I simply add a downloadable PDF file to my page with vanila javascript? [closed]

I want to create such a button on my portfolio website so that if someone clicks this button he/she can see my resume by pdf viewer and also download this pdf resume. how can I do it by vanilla javascript?

Advertisement

Answer

You could convert your PDF content to base64 string.

And then you could do something like this.

  const content = 'data:application/pdf;base64,<base64 PDF content string>';
  const linkSource = content;
  const downloadLink = document.createElement('a');
  const fileName = 'name.pdf';
  downloadLink.href = linkSource;
  downloadLink.download = fileName;
  downloadLink.click();
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement