0

Hey I have an issue where I'm trying to create a button where it automatically downloads a pdf. I saw many examples where it does works by a filepath (root/folder/pdf) but not by url.

Trying this because I'm using wordpress guttenberg and want less technical people to be able to upload a pdf url.

pdf url - https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf

<a class="btn"  href="<?php echo the_field('pdf_url') ?>" download ><span>Download PDF</span></a>

Outputs as

<a class="btn" href="https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf" download><span>Download PDF</span></a>

MPortman
  • 181
  • 1
  • 13
  • What's the extra word `download` doing after the URL? – Dave Apr 22 '20 at 18:17
  • @Dave it suppose to download the file instead of link you to that https://www.w3schools.com/tags/att_a_download.asp – Baracuda078 Apr 22 '20 at 18:18
  • Can you explain how the given html is not what you want? can you share the "btn" css if you are expecting this to behave as a button? – drussey Apr 22 '20 at 18:19
  • Take a look: https://stackoverflow.com/questions/3802510/force-to-open-save-as-popup-open-at-text-link-click-for-pdf-in-html – Felippe Duarte Apr 22 '20 at 18:20
  • Its just a styling class. Right now when clicking the button it just takes you to the pdf. But I want when clicked to download the pdf instead of opening the link. – MPortman Apr 22 '20 at 18:20
  • The link @FelippeDuarte posted may be helpful. What browser are you trying this in? – drussey Apr 22 '20 at 18:22
  • 3
    Is the file on the same server? copy from w3schools: * Chrome 65+ and Firefox only support same-origin download links. – Baracuda078 Apr 22 '20 at 18:23

1 Answers1

0

You need to use "_target" attribute to download the PDF in new tab.

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

If the PDF is still not downloading and is being opened in a new tab, change the browser settings. Visit this blog to see how to change the browser settings: How to change browser download settings for PDF files

AhmerMH
  • 638
  • 7
  • 18