Delete pdf file after opening (using Javascript) in another window.

I have this JavaScript code:

if(ac == "p") {
			if (spr == 1) {
				str = fn;
			} else {
				str = fnnp;	
			}
			window.open(str);
			return false;	
		}

Open in new window


Both fn & fnnp are previously defined strings like, for example: "https://www.abcxyz.com/folder/file_name.pdf.

Is there a way to DELETE the file (file_name.pdf) as soon as the new window is opened?

We have to assume the user may print or download the pdf or DO NOTHING but look at it.

Thank you

Thanks
Richard KortsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
Is there a way to DELETE the file (file_name.pdf) as soon as the new window is opened?

it means the PDF file is resided at server end, so you can't delete it from local or when the file is previewing on web browser.

to not interrupting the user experience and prevent any unforeseen errors, i would suggest to create another server end (background running) process that doing the job of file deletion. hope it make sense.
0
Richard KortsAuthor Commented:
Can you be a little more specific about how to do that?

Thanks
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
a backend process could be a task scheduled executable or services (like can be compiled in whatever programming language, such as Java, .NET etc) that running at server end, and the task of it is just to "clean up" a folder or particular file in which the file is being targeted and downloaded.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Richard KortsAuthor Commented:
Also, what if I put a timer delay in the JavaScript to wait, say 5 seconds, before I send to a php file to delete the pdf file?

Under normal circumstances, the file in the new window shows very quickly. 1 to 2 seconds.

Thanks
0
Richard KortsAuthor Commented:
I have thought of a cron job (the server language is php), but to make it effective, if would have to run like every 10 minutes.

Thanks
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
what if I put a timer delay in the JavaScript to wait, say 5 seconds, before I send to a php file to delete the pdf file?

I have thought of a cron job (the server language is php), but to make it effective, if would have to run like every 10 minutes.

These is possible too.
0
Chinmay PatelChief Technical NinjaCommented:
So once the user has downloaded the file in his/her local cache, we want to delete it from the server. If a local copy is extracted then it is fine? Also how about using PDF Security so that file cannot be printed or even better a PDF viewer that will render the file but not let user download the original? Or am I reading something wrong in your requirements?
0
lenamtlCommented:
Hi,

You can use a script like HTML2PDF https://github.com/spipu/html2pdf
so when generating the PDF you can set the output for PDF to open in browser, the file is not saved on the server so no need to delete it.
See output https://github.com/spipu/html2pdf/blob/master/doc/output.md
0
Richard KortsAuthor Commented:
I didn't want to get into all this, but here is the full issue.

The pdf file is saved to a folder on the server; we want to continue that. That folder is protected by a username & password, so a Google search cannot locate contents.

The php prgram has an Include that includes the code using fpdf to create the pdf. The php program, when rendered in the browser has several buttons, one is "Print Summary". When that is clicked, the original Javascript shows the file using window.open(x). When it does that, under SOME (not all) circumstances, it displays the attached dialog box. We want to eliminate that as it will confuse users.

My thought was to save the pdf file also in a temp folder. I'm thinking now of every 15 minutes, or some very short interval, have a cron job run on the server that scrubs the temp folder. Maybe it looks only at those not created in the last minute or 2?

That is sort of loosey goosey. Is there a better solution?

Thank you.
2018-09-05-15_40_57-Mozilla-Firefox.png
0
lenamtlCommented:
This is confusing because you want to keep the file on server and you want to also delete it...
Could elaborate why you want to keep the file on the server and in why you want to delete it?

I usually only generate the PDF and don't keep the file on the server to save space except if the content never change and I want to reuse it else where.
Do you reuse this file at any moment?
Because what I understand the file get generated each time and get saved each time on the sever for nothing as it will be overwritten

About the dialog it is related to the PDF / folder protection it will be displayed the first time and won't be displayed as long as the browser temp file get cleared / or browser session end
0
Richard KortsAuthor Commented:
We want to keep these files because for occasional unknown reasons, the customer claims they did it & they can't find, it, etc.

It's for backup purposes.

Richard
0
lenamtlCommented:
Ok but why do you you need to delete the file or make the file no longer available to the user...
Why you need this timer....
That won't remove the dialog as the dialog will appear at least once...
0
Richard KortsAuthor Commented:
lenamtl,

I want the file in TWO folders; one password protected, the other not.

It is the NON password protected the user will see.

The cron is to eliminate the files in the NON protected folder ASAP to prevent search from finding it.

Richard
0
lenamtlCommented:
Some suggestions

What you could do is when user generated the file, this open the file in browser + save the file in a folder or better in DB ( blob ) for example so no file in folder only in DB

If you don't want to save the file in DB..
You can have a meta tag to noindex nofollow noarchive on your application code page to prevent robots to index the whole application files and folders plus add an empty index.html in the  folder to prevent user browsing the files/folder + some .htaccess & robots.txt for extra protection.

<Directory "uploaddir/">
deny from all
</Directory>

Open in new window


You can also want to encrypt the files for a better security.
Maybe adding password protection to the PDF when creating.
Or maybe save the file in cloud (dropbox)

Having the file in the unprotected zone is not safe, let say for a reason the cron job does not run...
What will happen if other user try to generate a file during the cron job...

Also the dialog will still appear when saving the file in protected folder.
0
Richard KortsAuthor Commented:
To lenamtl,

This sounds good (file in db as blob). But I am not clear how I can open it as a pdf for user to view in a new window.

When the user pushes "Print Summary", how do I open the file in a new window in the users browser?

Thanks!!
0
lenamtlCommented:
This usually one of the output param from the script you use
http://www.fpdf.org/en/doc/output.htm

In your case you will need an extra output to save it also elsewhere

The way I do it is to put everything in one PHP file including the PDF JS code
so the button is an ahref that point to an url including the ID with a target="_blank"  to that page then it generate the PDF
0
Richard KortsAuthor Commented:
Back to the beginning, the problem is READING the file, which produces the unwanted message (Javascript Alert)
0
lenamtlCommented:
About the Login message ..
Yes you will have the login message because it's try to access the protected folder to save the file...
To get rid of this message you will need to save the file in DB not in protected folder.

other message like where to save the file will appear depending of the browser settings
user can set it to save not to open in browser or decide to select a folder where to save the file when saving it.
0
lenamtlCommented:
Another way to disable folder browsing using .htaccess put the file in you folder /upload
Options -Indexes

Open in new window

0
Chris StanyonWebDevCommented:
I've been involved with this topic in another, related question that gets to the heart of the matter. The bottom line is that to acheive the desired results, the file is simply served through a delivery script - no need for duplicate files, no need to delete files, no need for blobs, cron jobs, timers, or password protected folders.

Simply store the files outside of the public folder, and retrieve them using a simple PHP script. Any security checks can be managed there (simple SESSION variable for example), so Google (or any unauthorised user) can't gain access to the files - all access has to go through the script. Even the filenames don't need to be exposed.

If anyone's interested, the other related question is here
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PDF

From novice to tech pro — start learning today.