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!