I have a process that creates a PDF. I want these PDF’s to be temporary and short lived. I want to be able to perform the following when the user clicks a button:
string CreatePDF()//returns fileName.pdf
PromptUserToDownloadPDF()
DeletePDF(fileName.pdf)
I want to avoid having to create a cleanup procedure and deal with any race conditions that arise from users concurrently creating PDF’s while running cleanup.
In winforms, I would synchronously prompt a user to download a file. How can I do a similar task in web?
UPDATE
Please note that I am using a 3rd party app to create the PDF’s (Apache FOP). Basically I (will) have a function that invokes the command line:
C:>fop “inputfile” “output.pdf”
So, in memory is not an option…that is unless I could somehow do like….
string CreatePDF()//returns fileName.pdf
string RecreatePDFInMemory()
DeletePDF(fileName.pdf)
PromptUserToDownloadPDF()
Something like this:
Since this creates the PDF in memory, you don’t need to worry about cleanup.
Edit for OP’s edit:
From within
CreatePDF, You can usePath.GetTempFileNameto create a temp file and execute “fop” to write to that file. Delete that file immediately before returning thebyte[]. I recommend doing this delete inside of afinallyblock. However, “Fop” does support having its output piped to stdout. Having theCreatePDFfunction grab that is probably cleaner.