Search results

Export Data Visualization in Core Environment

Report Writer uses WebBrowser to export the data visualization to PDF, Word, Excel, and CSV file formats. The WebBrowser is not supported in Core environment. To overcome this limitation in Core environment, we have provided an option to export the data visualization report items using PhantomJS.

It supports ASP.NET Core 2.1 or above version only. To download PhantomJS application and deploy it on your machine, you should accept its license terms on LICENSE and Third-Party document.

  1. Download PhantomJS here and extract the download file.
  2. Copy the PhantomJS.exe file from the extracted bin folder and paste into wwwroot/PhantomJS folder in your application.
  3. Open the Report Writer application Web API file.
  4. Set the UsePhantomJS property to true.
  5. Set the PhantomJSPath, Scripts and DependentScripts property in Report Writer instance. The following code example demonstrates how to configure PhantomJS in your ASP.NET Core application.

In this tutorial, the product-line-sales.rdl report is used, and it can be downloaded in this link.

public class HomeController : Controller
{
    // IHostingEnvironment used with sample to get the application data from wwwroot.
    private Microsoft.AspNetCore.Hosting.IHostingEnvironment _hostingEnvironment;

    // IHostingEnvironment initialized with controller to get the data from application data folder.
    public HomeController(Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment)
    {
        _hostingEnvironment = hostingEnvironment;
    }

    public IActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public IActionResult Export(string writerFormat)
    {
        string fileName = null;
        WriterFormat format;
        string basePath = _hostingEnvironment.WebRootPath;
        // Here, we have loaded the sample report from application the folder wwwroot.
        FileStream inputStream = new FileStream(basePath + @"\Resources\product-line-sales.rdl", FileMode.Open, FileAccess.Read);
        BoldReports.Writer.ReportWriter writer = new BoldReports.Writer.ReportWriter(inputStream);

        // PhantomJS Option to export the data visualization report items.
        writer.ExportResources.UsePhantomJS = true;
        writer.ExportResources.PhantomJSPath = basePath + @"\PhantomJS\";

        writer.ExportResources.Scripts = new List<string>
        {

            //Gauge component scripts
            "https://cdn.boldreports.com/2.3.39/scripts/common/ej2-base.min.js",
            "https://cdn.boldreports.com/2.3.39/scripts/common/ej2-data.min.js",
            "https://cdn.boldreports.com/2.3.39/scripts/common/ej2-pdf-export.min.js",
            "https://cdn.boldreports.com/2.3.39/scripts/common/ej2-svg-base.min.js",
            "https://cdn.boldreports.com/2.3.39/scripts/data-visualization/ej2-lineargauge.min.js",
            "https://cdn.boldreports.com/2.3.39/scripts/data-visualization/ej2-circulargauge.min.js",
            //Map component script
            "https://cdn.boldreports.com/2.3.39/scripts/data-visualization/ej2-maps.min.js",
            "https://cdn.boldreports.com/2.3.39/scripts/common/bold.reports.common.min.js",
            "https://cdn.boldreports.com/2.3.39/scripts/common/bold.reports.widgets.min.js",
            //Chart component script
            "https://cdn.boldreports.com/2.3.39/scripts/data-visualization/ej.chart.min.js",
             //Report Viewer Script
            "https://cdn.boldreports.com/2.3.39/scripts/bold.report-viewer.min.js",
        };
        writer.ExportResources.DependentScripts = new List<string>
        {
            "https://code.jquery.com/jquery-1.10.2.min.js"
        };

        if (writerFormat == "PDF")
        {
            fileName = "ProductLineSales.pdf";
            format = WriterFormat.PDF;
        }
        else if (writerFormat == "Word")
        {
            fileName = "ProductLineSales.doc";
            format = WriterFormat.Word;
        }
        else if (writerFormat == "CSV")
        {
            fileName = "ProductLineSales.CSV";
            format = WriterFormat.CSV;
        }
        else
        {
            fileName = "ProductLineSales.xls";
            format = WriterFormat.Excel;
        }

        MemoryStream memoryStream = new MemoryStream();
        writer.Save(memoryStream, format);

        // Download the generated document from client.
        memoryStream.Position = 0;
        FileStreamResult fileStreamResult = new FileStreamResult(memoryStream, "application/pdf");
        fileStreamResult.FileDownloadName = fileName;
        return fileStreamResult;
    }
}

The Scripts and Dependent scripts must be added to export the data visualization report items.