3

I have looked around a lot and i cant seem to find a way that works for me. I am creating a website, and I have two buttons, a Download button (which works fine) and a Print button that should print the PDF (or even just open the PDF in adobe with the print dialog open).

The biggest difference between my question and many others is that I'm not trying to create a new document. My PDF is generated using a template PDF that I have created already.

I just need a way to print the document. I cannot save it on the server because I want the client to be able to print it. I have tried a MemoryStream, but its not working(granted I probably didnt write it correctly, the code is below). The header is grabbed from a different page.

using (var ms = new MemoryStream())
{
    Response.ContentType = "application/octet-stream";
    Response.AppendHeader("Content-Disposition", header);   /

    Response.Buffer = true;
    Response.Clear();
    var bytes = ms.ToArray();
    PdfReader r = new PdfReader(template);         
    using (PdfStamper ps = new PdfStamper(r, Response.OutputStream))    
    {
        AcroFields af = ps.AcroFields;
        ...
        ps.FormFlattening = true;
    }
    Response.OutputStream.Write(bytes, 0, bytes.Length);
    Response.OutputStream.Flush();

Again, I want to be able to have the client print this PDF that is generated once they click the Print button.

Filburt
  • 17,626
  • 12
  • 64
  • 115
Krytix
  • 67
  • 2
  • 8
  • I don't think that it is possible to do this directly - they may still have to open the print dialog themselves. Parameters that can be passed to adobe reader are listed here: http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdf_open_parameters.pdf No print amongst them I'm afraid. – Paddy Apr 30 '15 at 15:36
  • @Paddy would there be a way to open the PDF in a new tab, and then print that instead? – Krytix Apr 30 '15 at 15:50

1 Answers1

9

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.

Community
  • 1
  • 1
Chris Haas
  • 53,986
  • 12
  • 141
  • 274