New to Telerik Reporting? Download free 30-day trial

Print a Report Directly at Client-Side Without Displaying It in a Viewer

Environment

Product Progress® Telerik® Reporting

Description

This article lists the required steps for printing a report directly on the client-side without displaying it in a report viewer.

The existing print functionality in web viewers is based on the following:

  1. The report is exported in PDF format;
  2. On rendering the PDF document, the reporting engine injects Adobe Javascript that forces the used PDF reader to open its Print dialog;
  3. The PDF file is sent to the client in the browser in a hidden frame, where the browser must have properly configured and enabled PDF plugin to load the PDF file in the browser window;
  4. Once the PDF is loaded in the browser, hidden from the user, the PDF plugin used as a PDF reader shows its Print dialog.

In the above scenario, the PDF plugin is a must for the browser. The approach is based on PDF export because applications running in a browser do not have permissions to access the machine (technology restrictions). For more details, please check Printing Reports.

To export the report in a PDF file, you can use a ReportProcessor in code instead of a viewer.

Solution

Here is sample approach you can use to achieve the above in an ASP.NET Core application.

  1. Creating a controller endpoint that returns the report exported in PDF format.

    public class HomeController : Controller
    {
        private IWebHostEnvironment _environment;
    
        public HomeController(IWebHostEnvironment environment)
        {
            _environment = environment;
        }
    
        public IActionResult GenerateReportPDF(string reportName)
        {
            ReportProcessor reportProcessor = new ReportProcessor();
            Telerik.Reporting.UriReportSource uriReportSource = new Telerik.Reporting.UriReportSource();
            uriReportSource.Uri = Path.Combine(_environment.ContentRootPath, "Reports", reportName);
            RenderingResult result = reportProcessor.RenderReport("PDF", uriReportSource, null);
    
            return File(result.DocumentBytes, result.MimeType);
        }
    }
    
  2. Loading the generated PDF file within a hidden iframe and opening the browser's print dialog.

    function printReport() {
        fetch("/Home/GenerateReportPDF?reportName=Barcodes Report.trdp")
            .then(res => {
                if (res.status === 200) {
                    return res.blob();
                } else {
                    console.log("Could not retrieve PDF document.");
                }
            })
            .then(blob => {
                let objectURL = URL.createObjectURL(blob);
    
                var iframe = document.createElement("iframe");
                iframe.style.display = "none";
                iframe.src = objectURL;
                iframe.onload = function () {
                    iframe.contentWindow.print();
                }
    
                document.body.appendChild(iframe);
            });
    }
    

You can download the ASP.NET Core Client Print Example to see the full solution.

Notes

You can also test the examples (ASP.NET WebForms and ASP.NET MVC) illustrating how to load a PDF file, generated by Telerik Reporting in code, in browser and rely on the browser's PDF plugin to print the document.

The result PDF file rendered on the server is returned with Content-Disposition header set to inline.

The confirmation of the print operation cannot be avoided due to security restrictions.

See Also

In this article