Open a popup containing the result of the ASPX postback.

I have an ASPX page with many fields that generate PDF documents when I click the Export to PDF button.

Now, I would like to have a "print PDF" button in JavaScript that does something like this:

w = window.open(?); w.print(); w.close(); 

where is the "?" will execute the same answer as my Export to PDF button.

+7
javascript pdf-generation
source share
3 answers

If you need to send (back) your form to a new window, you can try to change the shape of the target to fake, for example:

 var form = $("form"); form.attr("target", "__foo"); 

Submit a form.

 form.submit(); 

And delete the target (setitmeout (, 1) - place the event at the end of js "event-queue", in our case, after sending the form):

 setTimeout(function () { form.removeAttr("target"); }, 1); 

In addition, before submitting, you can try to open a window with the __foo identifier for a more stylish design, and the form will be sent (postback) in this window instead of a new one:

  var wnd = window.open('', '__foo', 'width=450,height=300,status=yes,resizable=yes,scrollbars=yes'); 

But I have no idea how to handle the provided window and catch the onload or jquery event. If you can do this, exchange a workaround and call wnd.print (); You can play with an iframe inside this wnd and maybe you will find a solution.

Updated:

Try looking at this prototype [tested in Chrome]:

 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.0.min.js"></script> <script type="text/javascript"> function PrintResult() { var wnd, checker, debug; debug = true; // create popup window wnd = window.open('about:blank', '__foo', 'width=700,height=500,status=yes,resizable=yes,scrollbars=yes'); // create "watermark" __loading. wnd.document.write("<h1 id='__loading'>Loading...</h1>"); // submit form to popup window $("form").attr("target", "__foo"); setTimeout(function() { $("form").removeAttr("target"); }, 1); if (debug) { $("#log").remove(); $("body").append($("<div id='log'/>")); } // check for watermark checker = setInterval(function () { if (debug) $("#log").append('. '); try { if (wnd.closed) { clearInterval(checker); return; } // if watermark is gone if (wnd.document == undefined || wnd.document.getElementById("__loading") == undefined) { if (debug) $("#log").append(' printing.'); //stop checker clearInterval(checker); // print the document setTimeout(function() { wnd.print(); wnd.close(); }, 100); } } catch (e) { // ooops... clearInterval(checker); if (debug) $("#log").append(e); } }, 10); } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:Button runat="server" ID="ReportButton" OnClick="ReportRenderClick" Text="Export to PDF" OnClientClick="PrintResult()"/> <asp:Button runat="server" Text="Just a button."/> </div> </form> </body> </html> 

And here is the .cs file:

 public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void ReportRenderClick(object sender, EventArgs e) { Response.Clear(); Thread.Sleep(2000); Response.ContentType = "application/pdf"; Response.WriteFile("d:\\1.pdf"); //Response.ContentType = "image/jpeg"; //Response.WriteFile("d:\\1.jpg"); //Response.Write("Hello!"); Response.End(); } } 
+2
source share

In the question tag, you have the asp.net tag, so I think you have access to some ASP.NET server technology.

I would suggest doing it like this:

  • Create an HttpHandler or ASP.NET MVC action that returns a FileContentResult
  • On your page, use this code to download and print the file (it actually found it here , pasting it for future use!)

    <a href = "/path/to/file.pdf" onClick = "window.print (); return false"> Click here to download the print version </a>

There are some good guidelines for writing the back end:

+1
source share

Open a PDF window with an IFrame, and you can do this:

Parent frame content

 <script> window.onload=function() { window.frames["pdf"].focus(); window.frames["pdf"].print(); } </script> <iframe name="pdf" src="url/to/pdf/generation"></iframe> 

Inspired from this one https://stackoverflow.com/a/464829/

+1
source share

All Articles