Jump to content
Luc

Download pdf file directly when page opens in browser

Recommended Posts

Hey guys,

EDIT: I have been looking on the web to find a way to automatically download a pdf when a page opens, without user having to click a link.
However, no solution found (at least which i can get to work...)

Help is much appreciated!!

Thanks guys!

 

Edited by Luc
On second hand, it would be better to automatically download the pdf instead of viewing in browser.

Share this post


Link to post
Share on other sites

You need to go to a php page with header set for viewing in browser similar to https://www.w3schools.com/php/func_http_header.asp force download script

<?php
header("Content-type:application/pdf");

// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='downloaded.pdf'");

// The PDF source is in original.pdf
readfile("original.pdf");
?>

But replacing

'header("Content-Disposition:attachment;filename='downloaded.pdf'");'

with

'header("Content-Disposition: inline; filename="filename.pdf");'

You can use link to send filename to this file through querystring and read through $_GET['....']; So one php page for all viewable only pdf files.

Share this post


Link to post
Share on other sites

Hey dsonesuk,

Thanks for your reply. I tried to apply this.

in php file where i want to show the pdf in the browser i do the following (session is started at top of the page):

<header>

?php
$pdf=$_SESSION['pdf']; 
$location="pdf/$pdf.pdf";

header("Content-type:application/pdf");

// It will be called downloaded.pdf
header("Content-Disposition: inline; filename='downloaded.pdf'");

// The PDF source is in original.pdf
readfile("$location");

?>
</header>

But I guess i have to write something too where I tell him to print my pdf in my body, but what?

Sofar I get this: http://prnt.sc/f06szs 

I must be doing something wrong....

Share this post


Link to post
Share on other sites

Lose all html, the header() code should be at very top on newline after php opening  tag, there should be no space or empty newline break before php opening tag.

The link will go to this page with necessary information about file to view, the header() info along with readfile() will inform the page to open file directly in browser, so no html is required.

NOTE: if browser option is to 'ask what to do' for this type of file (.pdf), it will still download instead, if download is selected.

Edited by dsonesuk

Share this post


Link to post
Share on other sites

Thanks dsonesuk!

Since you mentioned that the browser option is to ask what to do and that it then gets downloaded, on second thought (sorry!!) I think its best to let users download it instead. However, now a user is directed to a php page and has to click a link before the file downloads. Would it be possible that the file "download itself" at the moment the page opens in the browser, without the user clicking the link?

Thanks for your help sofar!

 

Share this post


Link to post
Share on other sites

It really depends on the users browser setting for pdf file type, most browsers by default will use pdf browser plugin specifically for viewing in browser, but if the user changes from default to use browser plugin, to 'always ask' it will show 'download' or 'open with' which could be browser plugin or operating system Adobe reader program. Since it depends on browser settings, you can still use open pdf directly in browser php script for browsers using pdf plugin, then they will presented with alternative to 'save' or 'open with' with an alternative pdf application option.

Note: the 'downloaded' part file can be called anything' even original filename, it just renames filename to 'downloaded.pdf' even if original filename was 'test.pdf'.

Edited by dsonesuk
  • Like 1

Share this post


Link to post
Share on other sites

That piece of code given above would theoretically do the thing you want yet browser won't allow doing so for sure. I tried to implement such a thing and every time a user made it to a required page the browser blocked the action, regardless of what it actually was: Chrome, Firefox, Edge. IE maybe doesn't do so but there are not so many persons keeping using it. More likely if your page will automatically redirect to the website where pdf is stored, e.g. this https://form-cd-401s.pdffiller.com/ it's actually an editor, paid one but the very thing is, when the user gets redirected to the page they immediately go to the preview of the document, unlike Dropbox and other cloud storages

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×