I’ve seen a few questions like this around SO but couldn’t find anything that’s right for me. The chain of events that I would like to occur is as follows:
- User clicks an ASP.NET button control
- This fires that button’s onclick event, which is a function foo() in the C# codebehind
- foo() calls some other (unimportant) function which creates a PDF which ends up saved to the server’s disk. That function returns the path to the PDF
- Without any other user interaction, once the PDF is generated, the print dialog box opens in the user’s browser to print that PDF
What do I need to do to accomplish step 4? Ideally, it would be something I can call in foo(), passing in the path to the PDF, that will trigger the print dialog box in the user’s browser (printing the PDF and not the page the from which the onclick fired).
I think that I might be able to forward to the URL of the PDF document, and embed some Javascript in the PDF that automatically prints it, but I would rather not – I don’t necessarily want to print the PDF every time it’s opened (in a browser). Any other good way to do this?
The solution I arrived at was this:
Create a new ASP.NET web form (I called mine BinaryData.aspx) to serve as a placeholder for the PDF. In the code behind, the only method should be Page_Load, which looks like:
The PDF is passed in to the page through the Session variable named “fileLocation”. So, all I have to is set that variable, and then call
Response.Redirect("BinaryData.aspx").It doesn’t automatically print, but it triggers the download of the PDF without leaving the current page (which is good enough for me).