Before going down this path too far I just want to make sure that some things are properly explained. I don't know your level of expertise so please don't be offended if I say something that is obvious.
First, you say that you have two buttons, "Download" and "Print". We know what you intend for these to do however its important to know and understand that both of these options are "download". Your "Download" button is really "download and (maybe) prompt to save" and your "Print" button is really "download and then if the person has a PDF renderer configured in their browser (which most modern browsers have) then render the PDF otherwise prompt to save". This might be very obvious already to you but I just wanted to make sure it was clear. I personally don't have a PDF renderer bound to my default browser so all PDFs download for me every time.
Second, the server gets to send one and only one "thing" back to the client in response to a request. That "thing" can have meta data (headers) associated with it but there are very few standard meta keys out there that are relevant. One key (content-disposition) is whether this response should be treated, relative to the initiating request, as "inline" or an "attachment". Some browsers treat the latter as a "download and (maybe) prompt to save" but some still will launch an associated application. There's another key (mime type of application/octet-stream
) that some people use to further trick browsers that essentially says "seriously dude, this thing I'm sending is so weird that it would be impossible for you to figure it out so just save it to disk".
This is all very important because there's no "please print this" thing that you can send from the server that's in any standard spec (or any spec that I've ever seen for that matter). Also, the server cannot even say "open this in a new tab or browser window". Both print and window/tab control are client-side features so you need to attack from that angle.
So now on to some options. These options all assume that your clients have modern browsers with PDF renderers built-in and enabled.
One option that you could use would be to wrap your PDF in an iframe
that has some JavaScript that calls window.print()
upon loading. Google used to (and maybe still does) for their calendar. You'll need to test this with your various client rendering devices to make sure it works as you expect.
Another option is to try using PDFObject
to embed the PDF and then use the same JavaScript mentioned above.
One last option is to slightly change your PDF generation code and add JavaScript to your PDF the tries to print the PDF on load. JavaScript support in PDF renderers isn't universal so you'll need to check whether this works with your clients. Also, this setting is "baked" into the PDF that you make, so if someone saves it to disk, every time they open it it will try to print which is very annoying.