I want to force a pdf to be downloaded on mobile/tablet device to avoid open it in a new tab.
I tried with the download
attribute:
<a href="file.pdf" download target="_blank">
It works on desktop but not on mobile.
I also tried with PHP header
:
header('Content-Type: application/pdf');
header('Content-Transfer-Encoding: Binary');
header('Content-Disposition: attachment; filename="file.pdf"');
readfile('https://my-website.com/file.pdf');
But same, it works on desktop but not on mobile.
Any way to do this?
2
Answers
I did it in Javascript using
blob
:I think you’re doing it as best as you can. If the
<a>
tag tells the browser to download it as well as yourContent-Type
andContent-Disposition
headers, then the browser just doesn’t want to download it.You could try to confuse the browser by sending a
Content-Type
value that the browser doesn’t know, then it might fall back to download as the default behavior, but I wouldn’t do that. It reminds me of theUser-Agent
header, which is nowadays useless exactly because people were misusing it to confuse other parties until it lost all relevance.