Andres
Andres

Reputation: 2023

print ReportViewer from custom button not toolbar

Ok I've spent a few hours(4 to be exact) looking for a solution to this. I've found a couple of results but none work so far. :(

Issue: I have a reportviewer in a usercontrol and I hid the toolbar and created my own. Now I added a button that is supposed to print, but can't seem to get it to work. I'll take any solutions you all may have for me. But it has to be a button no the default bar that comes with the report.

Here's my code:

<rsweb:reportviewer 
ID="rvReports" 
runat="server" 
Height="600px"
Width="600px"
ShowToolBar="False"
SizeToReportContent="True" AsyncRendering="false" />

<asp:ImageButton ID="btnprint" runat="server" ImageUrl="../img/print.png" 
     OnClientClick="PrintReport();" />

Javascript:

<script type="text/javascript">
function PrintReport() {
    var viewerReference = $find("rvReports");
    var reportArea = viewerReference.get_reportAreaContentType();
        if (reportArea == Microsoft.Reporting.WebFormsClient.ReportAreaContent.ReportPage) {
            $find("rvReports").invokePrintDialog();
        }

 } 
</script>

the error I get with this is:

Uncaught TypeError: Cannot call method 'get_reportAreaContentType' of null

Just in case I added a Jquery Library cause I thought that was it but nothing doing.. BTW I got that javascript from here as it was one of the answers on another stackoverflow questions.

Upvotes: 0

Views: 4683

Answers (3)

user1997546
user1997546

Reputation: 1

Use clientId not serverId in the $find function:

$find(<%=rvReports.ClientID%>)

Upvotes: 0

Andres
Andres

Reputation: 2023

I ended up using ITextSharp for this, it was quite simple. Add a hidden iframe and in codebehind this:

Warning[] warnings;
        string[] streamids;
        string mimeType;
        string encoding;
        string extension;

        byte[] bytes = rvReports.LocalReport.Render("PDF", null, out mimeType,
                       out encoding, out extension, out streamids, out warnings);

        FileStream fs = new FileStream(HttpContext.Current.Server.MapPath("output.pdf"), FileMode.Create);
        fs.Write(bytes, 0, bytes.Length);
        fs.Close();

        //Open exsisting pdf
        Document document = new Document(PageSize.LETTER_LANDSCAPE, 0, 0, 0, 0);
        PdfReader reader = new PdfReader(HttpContext.Current.Server.MapPath("output.pdf"));
        //Getting a instance of new pdf wrtiter
        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(
           HttpContext.Current.Server.MapPath("Print.pdf"), FileMode.Create));
        document.Open();
        PdfContentByte cb = writer.DirectContent;

        int i = 0;
        int p = 0;
        int n = reader.NumberOfPages;
        Rectangle psize = reader.GetPageSize(1);

        //float width = psize.Width;
        //float height = psize.Height;

        //Add Page to new document
        while (i < n)
        {
            document.NewPage();
            p++;
            i++;

            PdfImportedPage page1 = writer.GetImportedPage(reader, i);
            cb.AddTemplate(page1, 0, 0);
        }

        //Attach javascript to the document
        PdfAction jAction = PdfAction.JavaScript("this.print(true);\r", writer);
        writer.AddJavaScript(jAction);
        document.Close();

        //Attach pdf to the iframe
        frmPrint.Attributes["src"] = "Print.pdf";

and that did it, guess just had to keep digging inside google..lol

Upvotes: 1

Alexei Levenkov
Alexei Levenkov

Reputation: 100630

From the error it look like $find("rvReports") returns collection with no controls and as result get_reportAreaContentType reports an error. Check in script debugger what is result of the $find("rvReports") call and see if element you expect is present in the view.

Note I think selector should be "#rvReports"...

Upvotes: 0

Related Questions