Automatically saving PDF's in Browsers - php

I have a interesting question for you all.
My client wants a way to automatically save a PDF that is opened up in Chrome. Currently he achieves this by either clicking on the Save button in Acrobat (in the browser), or by right clicking, thus bringing up the context menu and clicking Save As --> PDF. He would like this process automated.
I did a lot of searching and I cannot find a solution to this question.
So I ask all of you, is there a way to automatically save a PDF that is embedded in a HTML page?
I'm assuming I would have to do some screen scraping to get the HTML page, which would include the link with the PDF but I'm not sure where to go from there.
Any help will be greatly appreciated.
Thank you very much, and have a great day.

Web pages require user interaction before writing files to the user's hard drive. This is a security measure to keep any ol' web page from putting files on your disk.
It is possible to make file Save As dialog popup when the user clicks on something and ready to save a particular file to the disk, but in some browsers the user will still have to finish the operation. In other browsers, downloaded files can be pre-configured to go to a particular "downloads" directory and the user does not have to take another step after the browser initiates the save.
If, when the browser requests a file, the response is given a header:
Content-Disposition: attachment; filename="xxx.pdf"
The browser will try to save it. I don't know what happens if the web server does this as one request of many in a web page. The way I've seen this used is the user clicks a download button and the browser requests a specific URL that indicates to the server that the user wants to save the file and then, and only then, the web server includes this header.

You can do this with htaccess. Add the following to your .htaccess file.
AddType application/octet-stream .pdf

Related

Browser cache downloadable file

Say i store pdf files in the database (not important). The users of the application visit a page periodically to grab a stored PDF and print it - for adhesive labels btw.
I am annoyed at the thought of their downloads directory filling up with duplicates of the same document over time since they will download the PDF every time they need to print the labels.
Is there a way to instruct the browser to cache this file? Or any method of relative linking to the users file system possibly? All users will be on Chrome/Firefox using Windows 7 Pro.
Etags will help you to do this. If the file hasn't been updated since the client last downloaded, the server will send a 304 "not modified" response, instead of the file.
If your files are dynamically generated, you will need to manually implement etag generation in PHP rather than relying on the web server.
http://www.php.net/manual/en/function.http-cache-etag.php
I've found a useful solution to my problem.
From the comments on my question, we concluded it would work best to utilize the browser's built in PDF/DOC renderer and download anything else that isn't recognized.
I read this standard: https://www.rfc-editor.org/rfc/rfc6266
This is the solution (header):
Content-Disposition: inline; filename=something.pdf
Instead of attachment, I've used "inline" in order to utilize the browser when necessary.
Most browsers will do this automatically based on the URL. If the URL for a particular PDF blob is constant, the browser will not re-download it unless the server responds that it has changed (by way of HTTP fields).
You should therefore design your site to have "permalinks" for each resource. This could be achieved by having a resource-ID of some sort in the URL string.
As others have said in comments, a server cannot guarantee that a client does ANYTHING in particular; all you can offer are suggestions that you hope most browsers will treat similarly.

Save webpage to server folder

Is there any way to save a external web page to folder on server but with all webpage elements (JS files,images,css... etc.). Like you can do at browser with option save-complete-page but I need this to save with php on my server folder. And when include this folder to show the page as original. Maybe with curl or some php function ... ???
HOW TO DO THAT. please HELP!
p.s.I doing this for good reason not for stealing content!
and when I finising with operation and function I will empty the folder.
Are you saying that you want to visit a php site as a client (whether in a browser or via wget/curl/etc) and then save all related server-side files?
Without access to the server, that is not possible. The server-side content (e.g. the PHP pages and possibly some other parts of the site) are interpretted by the server before you as a client see any part of the site. You need server-side access to the files in order to see what is there before the code is interpretted.

Integrating a download manager to PHP script

I'm using PHP and my script outputs a list of links for files that can be downloaded by the user. To download each file, as a user I would have to copy the url and paste it into something like Free Download Manager.
I would like to improve the user experience and have a "Download" button that would handle or initiate the download process.
I'm thinking either have code written in PHP to act as a download manager, or tie the "Download" button to the functionality of a firefox or such add-on that act as a download manager. Does anyone have good suggestions for the sort of thing I'm trying to do?
Update:
Let's say that:
- my script presents the user with a list of files that can be downloaded.
- next to each file, there's a checkbox, then at the bottom a button that says "download selected".
If this is the setup I have, if I use force download, then clicking the "download selected" button will force dl 12 files at the same time, so not exactly like a download manager. I'm thinking this probably requires something that takes both PHP and Firefox behavior into account.
You can use php header() to force download for single file per time and multiple times.
Some links for you to reference from.
http://w-shadow.com/blog/2007/08/12/how-to-force-file-download-with-php/
http://www.ryboe.com/tutorials/php-headers-force-download
Another good example from php.net: readfile()

PHP Download resume with hidden url link for large file

I'm sorry to bother you with my issues, but i'm facing a a problem that I have some trouble to fix.
I have a website with a login restricted area.
Once the user is logged he can access to the files of my company (big file)
But to avoid the link to be spread all over the internet when a user want to download the file, located on an external url: he clicks on a url which will contain the name of the file crypted in md5 , which redirect on a php script which is going to generate in php, headers with the download using fsockopen.
However this does not support resume of download which is not very practical when we are downloading somes files of 2 or 3 gb or when you are using a downloader.
How can I do to enable resume ?
I have seen some php scripts using fread method , however i don't think it would be a good idea in my case, because for big files, it could make lag the server.. when you do a progressive fread on a 2gb files, good luck for the process when they are 30 poeple downloading the file in the meantime.
If you use fopen() and fseek() like this, you're essentially doing the same as any Webserver does to reply HTTP-RANGE requests.
You could make it so that it doesn't allow the file to be downloaded unless they are logged in.
So instead of providing them with a direct link to the file, they get a link like foo.com/download.php?myfile.ext
And the download.php would check the session before providing the user with the file download.

How to store processed file in users desktop?

I have created a website.In that scaling an image option is created.. Now i want to store that scaled image in users desktop..But its saving in code existing folder..
Please help me by sending php script to store that file in desktop
If your website is going to actually live on the web instead of on people's computers locally then you can't directly save it to their computer. You can serve it to them with php as a file download by setting the proper mime-type and content headers (using header()) followed by the file itself, or better yet offer the user a download link so they can choose to download it themselves.
If your website is going to be used locally (a little odd, but it could happen), then you can use fopen(), fwrite() and fclose() in php to work with local files.
I don't think it is possible to do this without asking for user intervention on where to save the processed file. If you think about it, it would be a significant security flaw if a web server could arbitrarily save files to user desktops!
The best you could do is set the content header of the generated file to have a content disposition of attachment and then the browser will prompt the user where to save the file.

Categories