0

I have a ReportViewer on my asp.net page. Client wants next functionality: Custom print when you click print button > report opens in new tab/window in browser as pdf and automatically popups print dialog.

What I did is save rendered pdf from report viewer is session and open new tab in browser: Warning[] warnings;

    string[] streamids;
    string mimeType;
    string encoding;
    string extension;
    byte[] myBytes;
    string reportName = hfReportName.Value;

    myBytes = rvReport.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings);

    Session["myBytes"] = myBytes;
    Session["mimeType"] = mimeType;
    Session["fileName"] = reportName + "_" + ddlOffGroup.SelectedItem + "." + extension;

    Response.Buffer = true;
    Response.Clear();
    Response.Write("<script>");
    Response.Write(String.Format("window.open('{0}')", ResolveUrl("PrintPage.aspx")));
    Response.Write("</script>");

Then in PrintPage.aspx I have this:

protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["myBytes"] == null || Session["mimeType"] == null || Session["fileName"] == null)
                Response.Redirect("ErrorPage.aspx?errStr=No print content found. Sorry");
            else
            {
                Response.Buffer = true;
                Response.Clear();
                Response.ContentType = Session["mimeType"].ToString();
                Response.AddHeader("content-disposition", "inline; filename=" + Session["fileName"].ToString());
                Response.BinaryWrite((byte[])Session["myBytes"]);
                Response.Flush();
                //Response.End();
            }
        }

Basically, reports opens fine, but I can't figure out how to show print Dialog (the same as Ctrl + P functionality). I've tried to add this on the page:

<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () { 
            window.print();
        });
    </script>

But nothing happens when page is loaded. Any suggestions on that? Thanks!

Bryuk
  • 3,295
  • 10
  • 45
  • 74

1 Answers1

0

It looks like javascript error. Are you sure that there are no javascript-errors in browser? Does JQuery .js file in specified location?

Probably, javascript doesn't work because the content-type of the page is not text/html. Try to remove

Response.ContentType = Session["mimeType"].ToString();
Taryn
  • 242,637
  • 56
  • 362
  • 405
Olga Golubeva
  • 307
  • 2
  • 8
  • I was trying to remove all response code from page_load event and it works fine. Seems like response.flush does something... – Bryuk Jun 25 '14 at 20:10
  • Probably, javascript doesn't work because the content-type of the page is not text/html. Try to remove Response.ContentType = Session["mimeType"].ToString(); – Olga Golubeva Jun 25 '14 at 20:20
  • If I remove Response.ContentType = Session["mimeType"].ToString(); then I get download dialog instead of showing pdf on the page – Bryuk Jun 25 '14 at 20:48
  • When we add pdf content to Response.BinaryWrite - it replaces all ours code in aspx page to its own pre-defined code. So, javascript calling is no logner exists on our page. You can check this using "Web Developer" plugin in FireFox by selecting option "View generated source". May be this topic will be more helpful for you: http://stackoverflow.com/questions/205180/how-to-print-a-pdf-from-the-browser – Olga Golubeva Jun 26 '14 at 19:43