Printing MVC report page

Posted by: marcelo on 13 September 2018, 4:37 am EST

    • Post Options:
    • Link

    Posted 13 September 2018, 4:37 am EST

    How to print a MVC report page?

  • Posted 13 September 2018, 2:45 pm EST

    Hello,

    You can use the Print method of “HTML5” viewer as follow to print the report.

    
    viewer.print()
    
    

    Also, you can refer to the following documentation link:

    http://help.grapecity.com/activereports/webhelp/AR12/webframe.html#HTML5WorkingwithViewerUsingJavascript.html

    Thanks,

  • Posted 14 September 2018, 11:45 am EST - Updated 30 September 2022, 8:57 am EST

    Thanks for your answer.

    How can I insert a printer button in the red square using pure javascript and print the report?

    Could you provide me a sample?

    In fact, I’m using ExtJs, but if the example is in javascript that’s enough.

  • Posted 14 September 2018, 11:48 am EST

    PS: I’m not using html5 viewer.

  • Posted 14 September 2018, 11:51 am EST

  • Posted 16 September 2018, 5:38 pm EST

    Hello,

    Please refer to the attached sample.

    Thanks,

    WebApplication5.zip

  • Posted 17 September 2018, 10:27 am EST

    Thanks for the sample.

    The button appears and make calls to defined action , but when I try to export to PDF, the report is empty.

    What I’m doing wrong?

    I tried to put it in different actions and in the same action, and in both cases the report returns empty.

    I’m using asp mvc with webforms engine not razor engine and using PageReport, not SectionReport.

     public ActionResult ReportViewer(bool? print)
            {
                if (print ?? false)
                {
                    //try to use same action. Doesn't work
                    var rpt = new PageReport();
                    rpt.Run();
                    var export = new PdfExport();
                    var memStream = new MemoryStream();
                    export.Export(rpt.Document, memStream);
                    return new System.Web.Mvc.FileStreamResult(memStream, "application/pdf");
                }
    
                var report = default(PageReport);
    	
    		//some code to load report
    
                ViewBag.Report = report;
    
                return View();
            }
    
            public ActionResult PrintReport()
            {
                //or use a different acttion, doesn't work too
                var rpt = new PageReport();
                rpt.Run();
                var export = new PdfExport();
                var memStream = new MemoryStream();
                export.Export(rpt.Document, memStream);
                return new System.Web.Mvc.FileStreamResult(memStream, "application/pdf");
            }
    
  • Posted 17 September 2018, 10:12 pm EST

    Hello,

    You pageReport object is empty that’s why the report is empty. Could you please try after load the Page Report definition like below:

    var rpt = new PageReport(new FileInfo(“Report path”));

    Thanks,

  • Posted 17 September 2018, 10:40 pm EST - Updated 30 September 2022, 8:57 am EST

    Hi.

    No, It’s Not.

    As you can see in the imagem, there is some information on the report.

    And below, is my full code to load the report.

    At last post was just a simple example, not the full code.

     public ActionResult ReportViewer(bool? print)
            {
                if (print ?? false)
                {
                    //try to use same action. Doesn't work
                    var rpt = new PageReport();
                    rpt.Run();
                    var export = new PdfExport();
                    var memStream = new MemoryStream();
                    export.Export(rpt.Document, memStream);
                    return new System.Web.Mvc.FileStreamResult(memStream, "application/pdf");
                }
    
                var id = Convert.ToInt64(Request.QueryString["id"]);
                var reportDefinition = DependencyManager.Resolver.GetService<ReportLoader>().Load(id);
    
                var report = default(PageReport);
    
                var reportDef = ReportHelper.ConfigureConnection(reportDefinition.Model);
    
                //será usada mais tarde quando o contexto não existir, mas é preciso saber a string de conexão.
                var stringCnn = Tenant.Manager.ConnectionString;
    
                Reports.DataProvider.DataProviderFactory.DataProviderService = () => new DataProviderService(stringCnn);
    
                using (var reader = new StringReader(reportDef))
                {
                    report = new PageReport();
                    report.Load(reader);
                }
    
                report.ConfigurationProvider = new ConfigurationProvider();
                ViewBag.Report = report;
    
                return View();
            }
    
  • Posted 18 September 2018, 8:02 pm EST

    Hello Marcelo,

    Please refer the following code

    
    if (print ?? false)
                {
                    //try to use same action. Doesn't work
                    var rpt = new PageReport();
                    rpt.Run();
                    var export = new PdfExport();
                    var memStream = new MemoryStream();
                    export.Export(rpt.Document, memStream);
                    return new System.Web.Mvc.FileStreamResult(memStream, "application/pdf");
                }
    
    

    You are exporting the rpt object to PDF format which does not contain any report definition that’s why you get the empty pdf.

    Hope it clarifies.

    Thanks,

  • Posted 20 September 2018, 3:42 am EST

    Hi.

    In example that you sent to me, you create a new one empty too.

    The class MyCustomHtmlOutputter is just for create the name and the file with an “empty/or not” report, because in this line Dim rpt As New SectionReport1() you create new and in this line rpt.Run(False) you execute the report, but, from where the report is loaded?

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            If (Request("Task") IsNot Nothing) Then
                If (Request("Task").ToString().Contains("Print")) Then
    
                    Dim rpt As New SectionReport1()
                    Try
    
                        rpt.Run(False)
                    Catch eRunReport As Exception
                        'If the report fails to run, report the error to the user
    
                        Response.Clear()
                        Response.Write("<h1>Error running report:</h1>")
                        Response.Write(eRunReport.ToString())
                        Return
                    End Try
                    'Buffer this page's output until the report output is ready.
    
                    Response.Buffer = True
                    'Clear any part of this page that might have already been buffered for output.
    
                    Response.ClearContent()
                    'Clear any headers that might have already been buffered (such as the content type 
    
                    'for an HTML page)
    
                    Response.ClearHeaders()
                    'Tell the browser and the "network" that the resulting data of this page should be 
    
                    'cached since this could be a dynamic report that changes upon each request.
    
                    Response.Cache.SetCacheability(HttpCacheability.NoCache)
                    'Tell the browser this is an Html document so it will use an appropriate viewer.
    
                    Response.ContentType = "text/HTML"
                    'Create the Html export object
    
                    Dim HtmlExport1 As New GrapeCity.ActiveReports.Export.Html.Section.HtmlExport()
                    Dim outputter As New MyCustomHtmlOutputter(Me.Context)
                    HtmlExport1.Export(rpt.Document, outputter, "")
                    Response.Redirect("ReportOutput" + "/" + System.IO.Path.GetFileName(outputter.mainPage))
    
                End If
            End If
        End Sub
    
  • Posted 20 September 2018, 6:18 pm EST

    Hello Marcelo,

    “SectionReport1” is “Code-based section report” in which report layouts are saved as C# or Visual Basic files within the project in which they are created. So you don’t need to load the report into object, you can simply create the object of “Code-based section report”. Where as report layout of “PageReport” is saved in “RDLX” format file that’s why you need to load the report definition into “PageReport” class object. For more information, Please refer the following documentation link:

    http://help.grapecity.com/activereports/webhelp/AR12/webframe.html#ReportTypes.html

    Hope it clarifies.

    Thanks,

  • Posted 20 September 2018, 11:02 pm EST

    I’m using a page report and load it from a string saved on my database using a [i]StringReader[/in].

     using (var reader = new StringReader(reportDef))
                {
                    report = new PageReport();
                    report.Load(reader);
                }
    

    The RDLX file do not stay on the client, because I can have a lot of customizations for each tenant.

    A workaround I’m thinking about, is print each DIV, but for this, I have to simule the next page click, save the current DIV on the browser memory e so on, and I don’t think is a good ideia.

    So, why ActiveReports doesn’t have a builtin printer button for webviewer?

    If there a way to get parameters selected by users, I can recreate the report on server side and convert it to pdf and send to browser.

  • Posted 23 September 2018, 4:34 pm EST

    Hello Marcelo,

    I have updated the sample according to your requirement. You can load the report object the from the WebViewer to Print the Report. Could you please refer to the attached project If it could help you.

    Also, could you please confirm are you comfortable to use “HTML5” viewer to have inbuilt Print functionality.

    Thanks,

    WebApplication5-Modified.zip

  • Posted 17 October 2018, 4:59 am EST

    Hi.

    Sorry for taking long time to get back to this topic. But here I’m again.

    I create an example that has the same behavior as I have in my application.

    Below is the link to download the example.

    In my example, I am not able to print or export to .PDF as well

    http://desenvolvedores.net/grapecity/myexample.rar

    [edit]

    I forgot to say. I’m using NWIND.mdb to create a report sample[/edit]

  • Posted 17 October 2018, 7:21 pm EST

    Thanks for the sample application. When clicking on the custom Print button, I got the error ‘The sections collection is invalid’ because the SectionReport doesn’t have any sections. I modified the code and added sections to the report and modified the code to export to Pdf as well, as given below:

    public ActionResult PrintReport()
            {
                var rpt = new SectionReport();
                rpt.Sections.Add(new GrapeCity.ActiveReports.SectionReportModel.PageHeader());
                rpt.Sections.Add(new GrapeCity.ActiveReports.SectionReportModel.Detail());
                rpt.Sections.Add(new GrapeCity.ActiveReports.SectionReportModel.PageFooter());
    
                var txt = new GrapeCity.ActiveReports.SectionReportModel.TextBox
                {
                    Text = "New TextBox"
                };
                rpt.Sections[1].Controls.Add(txt);
    
                rpt.Run(false);
    
                Response.Buffer = true;
                Response.ClearContent();
                Response.ClearHeaders();
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.ContentType = "application/pdf";
                var export = new PdfExport();
                var memStream = new MemoryStream();
                export.Export(rpt.Document, memStream);
                Response.AddHeader("content-disposition", "inline");
                Response.BinaryWrite(memStream.GetBuffer());
                Response.End();            
                return null;
                //return new FileStreamResult(memStream, "application/pdf");
            }
    

    I have attached the modified sample for your reference. You would need to change the db path in the SampleReport.txt back to original and also version info in the Licenses.licx and web.config files.

    Hope it helps

    myexample_modifiied.zip

  • Posted 18 October 2018, 1:38 am EST - Updated 30 September 2022, 8:57 am EST

    Hi Abdias

    In your example, you have created a hard coded report and insert a textbox.

    I need to print the report already shown on the page, not create a new one.

    Sorry, but the english is not my primary language, maybe I have some dificulty to tell you what I really need.

  • Posted 21 October 2018, 7:20 pm EST

    Hi Marcelo,

    Since you had created a new object of SectionReport in the PrintReport method in your sample, I provided the code accordingly. Nevertheless, as you’re using the WebViewer control, there is no need to print/export the already shown report from the server. You can use the WebViewer’s client side ViewModel to print and export the displayed report. Here’s the code that you can use:

    var viewModel;
            $(document).ready(function () {
                $(".arvToolBar").append("<span><input id='btnPrint' type='Button' value='Print' onclick='print()'/></span>");
                $(".arvToolBar").append("<span><input id='btnExport' type='Button' value='Pdf' onclick='PdfExport()'/></span>");
    
                $('#_wvr').bind('loaded', function () {
                    viewModel = GetViewModel("_wvr");
                });
            });
    
            function print() {
                viewModel.Print();
            }
    
            function PdfExport() {
                viewModel.Export(ExportType.Pdf, function (uri) {
                    window.open(uri);
                }, true);
            }
    

    Hope it helps achieve what you’re looking for

  • Posted 23 October 2018, 3:00 am EST

    Thanks Abdias.

    That’s was I really looking for.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels