in reply to Deleting a Temp File

Um... if this is a cgi script that you are quoting from, it seems like it would make sense for the "PRINT THIS PAGE" link to point back to this same cgi script, with a specific parameter that means "send the printable data back to the client".

In other words, you shouldn't need to create a temporary file at all (and therefore would not need to worry about how to delete the temp file). Add one more behavior mode to the current script, which will extract the "printable" content of your source data file (just like you're doing now), and print that to STDOUT (i.e. back to the client browser) instead of to a temp file.

Decide on a cgi parameter that will activate this new mode, and put that (along with any other relevant params) into the href for the "PRINT" link, instead of a file name.