1

I need a PDF to download automatically once a page loads. I didn't think this would be that complicated. I am not a developer. I just work in marketing. So this might be super obvious.

I found some javascript code on this thread and I gave it a try but it redirects the page you're on to the PDF - instead of leaving the user on their current page and just downloading the PDF.

JAVASCRIPT:

 $(function() {
  $('a[data-auto-download]').each(function(){
    var $this = $(this);
    setTimeout(function() {
      window.location = $this.attr('href');
    }, 2000);
  });
});

HTML

<p>The download should start shortly. If it doesn't, click
<a data-auto-download href="/your/file/url">here</a>.</p>

I also tried the iframe method, with the iframe set to display:none but that didn't work. I didn't think it would though because wouldn't a PDF just display in the iframe, since that's what it does in a browser?

Another idea is would the above code work if I created a PHP file like they talk about in this thread?

Simon Franzen
  • 2,628
  • 25
  • 34
  • How file downloads are treated varies from browser to browser and is under the jurisdiction of what settings the user has set for themselves. For instance, redirecting to your PDF file in Firefox may automatically download it (if no PDF reader is available or no auto open settings has been set), while on an iPad open them directly. That said, there are various approaches you can take to circumvent this, but most are hacky at best. See: https://stackoverflow.com/questions/156686/how-to-start-automatic-download-of-a-file-in-internet-explorer?noredirect=1&lq=1 – Coreus Mar 14 '18 at 16:11
  • Is the file on your server ? You could send "Content-Disposition:attachment" for this file and you have a good chance that all browsers will treat this call as an download. – Simon Franzen Mar 14 '18 at 16:18
  • @simonFranzen It is on my server. How do you add content-disposition: attachment to a pdf file? – Amy Montgomery Mar 14 '18 at 16:38
  • @coreus that's the the thread I linked to in my OP, like where i got that JS code. I just feel like their has to be a way to do this. I have used so many websites which will automatically download a pdf to computer. I am sure whatever I come up with won't work on all browsers, but there has to be something that works for most. – Amy Montgomery Mar 14 '18 at 16:41
  • Set content-disposition https://stackoverflow.com/questions/3977159/set-content-disposition-header-to-attachment-only-on-files-in-a-certain-director – Simon Franzen Mar 14 '18 at 16:49
  • @SimonFranzen I read some answers saying just add content-disposition: attachment, and [I did google it]( https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition) but I am really confused. In a link there is the webpage you are on, and then the link to the pdf file. Is Content-disposition a type of file? Do I link to the content-disposition and it will tell it to download the pdf? – Amy Montgomery Mar 14 '18 at 16:52
  • @SimonFranzen I just read the thread you linked. It says ", put the .htaccess file in the highest level folder you want to affect. and those settings will trickle down to sub folders." So I create an .htaccess file and put it in a folder above my pdf on my server and then link to my pdf? – Amy Montgomery Mar 14 '18 at 16:54
  • @AmyMontgomery I added an answer. If you have a .htaccess file in your root folder you can add the lines there. If not, create one, you will need for other things in the future ;) – Simon Franzen Mar 14 '18 at 17:22

2 Answers2

1

Set Content-Disposition in .htaccess or apache settings to deliver the pdf as download

<Location "/uploads/documents/">
    <Files *.pdf>
        ForceType application/octet-stream
        Header set Content-Disposition attachment
    </Files>
</Location>

or in .htaccess for all pdf files an example here:

<IfModule mod_headers.c>
    <FilesMatch "\.(pdf|PDF)$">
        ForceType application/octet-stream
        Header set Content-Disposition "attachment"
        Allow from all
    </FilesMatch>
</IfModule>

place this in your .htaccess in root and the pdf is located under /uploads/documents

On Windows server you could use a small ASP.NET script to serve the files

string filename = "my-download.pdf";
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);
string aaa = Server.MapPath("~/uploads/documents/" + filename);
Response.TransmitFile(Server.MapPath("~/uploads/documents/" + filename));
Response.End();
Simon Franzen
  • 2,628
  • 25
  • 34
1

This is the best way if you want to do this by javascript

    var file_path = 'files/xyz.pdf';
    var a = document.createElement('A');
    a.href = file_path;
    a.download = file_path.substr(file_path.lastIndexOf('/') + 1);
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
aman
  • 125
  • 1
  • 11