2

Currently, I have a controller action that outputs a PDF (as a Response.OutputStream.Write()) and this is working just as it should.

However, I am interested in outputting another script section along with the PDF to "automatically print" (or simply perform a window.print();) on the PDF.

Is this possible, or is there another method to solve this issue that I may not be aware of?

Controller Action:

public ActionResult PrintPDF(string ID)
{
     //Population of Model

     //Output Result
     return PdfResult(model);
}

PDF Result:

var buffer = byteArrayStream.toByteArray();
response.OutputStream.Write(buffer, 0, buffer.Length);

//Is it possible to output something like the following:
response.Output.Write("<script type='text/javascript'>window.print();</script>");
Rion Williams
  • 74,820
  • 37
  • 200
  • 327
  • 1
    Rather than focusing on the server side, what do you want the client to receive? Think about what the contents of the response should be. – Jon Skeet Oct 03 '11 at 14:18
  • @Jon, That's what I was trying to focus on. I just wasn't sure if there was any way to include the necessary JavaScript in addition to the response. I'm sure making a View will solve the issue, I just wanted to know if there was another way. – Rion Williams Oct 03 '11 at 14:39
  • @Jon, Sorry I seemed to have avoided your question. I would like the client to be able to view the actual PDF and be prompted to print it. – Rion Williams Oct 03 '11 at 14:42

1 Answers1

3

You will most likely not be able to mix PDF-data with JavaScript, so what you need to is embed the PDF-file using the <embed>-tag and then use javascript to print whatever is inside the <embed>-tag.

Here's some information that someone else got working. Basicly this is the code that is outputted (from the previous source but edited a little bit):

<html>
    <body>
        <embed id="pdfToPrint" src ="@ViewData.PDFUrl" width="550" height="550"
        name="whatever">
        <script>
            var x = document.getElementById("pdfToPrint");
            x.click();
            x.setActive();
            x.focus();
            x.print();              
        </script>
    </body>

</html>
Filip Ekberg
  • 36,033
  • 20
  • 126
  • 183
  • According to the source in Chrome it is being output inside of an tag, which is what got me thinking about the possibility of including JavaScript in there. I am sure this could be accomplished by simply using a View and the embed tag, I just thought I would explore other areas. – Rion Williams Oct 03 '11 at 14:40
  • If you see it from the browsers perspective: The browser needs to know what kind of data is sent ( content type ) in order for it to display it properly. I don't think there's a cross browser support for embedding PDF-data and JavaScript in a single output stream without using the embed-tag, does that make sense? – Filip Ekberg Oct 03 '11 at 14:43
  • Absolutely. I just thought I would explore this as an option to see if it was possible. – Rion Williams Oct 03 '11 at 14:45
  • I attempted to use this, however I don't know if it works anymore (the Print functionality). I have attempted using JavaScript to print it, with little results. Apparently the embed doesn't like the print() command. – Rion Williams Oct 03 '11 at 15:49
  • Hmm that is weird. Have you seen this post? http://stackoverflow.com/questions/687675/can-a-pdf-files-print-dialog-be-opened-with-javascript – Filip Ekberg Oct 03 '11 at 16:27
  • 1
    Yes, I went through that one as well. I managed to find a solution, the easiest of which was to simply load the PDF into an iframe as opposed to an embed or object. I could then simply use the iframe.contentWindow.print() function. – Rion Williams Oct 03 '11 at 16:33