Search code examples
powerbipowerbi-embedded

Printing PowerBI Embeded Report with custom header


I am using the powerbi-react-client to embed a powerbi report in a website.

The website lets users print the report which I am doing as the package recommends using

https://learn.microsoft.com/en-us/javascript/api/overview/powerbi/embedding-basic-interactions#print-a-report

I get a reference of the embedded report object using

<PowerBIEmbed
            embedConfig = { sampleReportConfig }
            eventHandlers = { eventHandlersMap }
            cssClassName = { reportClass }
            getEmbeddedComponent = { (embedObject: Embed) => {
                // stores the report ref in a state
                setReport(embedObject as PowerBI.Report);
            } }
await report.print()

  • the client basically puts an IFrame in the page
  • Unfortunately Power BI decided to put a default "Microsoft Power BI" title which appears as the header when this report is printed.
  • I cannot access / alter this title using javascript as this is loaded cross domain

Question

  • How can I get around this, remove / custom update this title ?

Solution

  • There is NOT a good away to do this programmatically using the javascript client. However we were able to get this done using Power BIs nuget package that triggers an export request behind the scene to the PowerBI Capacity

    below code snippet has been copied from the above reference link. The link also gives code examples of how to track the status of the export request and download the actual PDF.

    private async Task<string> PostExportRequest(
        Guid reportId,
        Guid groupId,
        FileFormat format,
        IList<string> pageNames = null, /* Get the page names from the GetPages REST API */
        string urlFilter = null)
    {
        var powerBIReportExportConfiguration = new PowerBIReportExportConfiguration
        {
            Settings = new ExportReportSettings
            {
                Locale = "en-us",
            },
            // Note that page names differ from the page display names
            // To get the page names use the GetPages REST API
            Pages = pageNames?.Select(pn => new ExportReportPage(Name = pn)).ToList(),
            // ReportLevelFilters collection needs to be instantiated explicitly
            ReportLevelFilters = !string.IsNullOrEmpty(urlFilter) ? new List<ExportFilter>() { new ExportFilter(urlFilter) } : null,
    
        };
    
        var exportRequest = new ExportReportRequest
        {
            Format = format,
            PowerBIReportConfiguration = powerBIReportExportConfiguration,
        };
    
        // The 'Client' object is an instance of the Power BI .NET SDK
        var export = await Client.Reports.ExportToFileInGroupAsync(groupId, reportId, exportRequest);
    
        // Save the export ID, you'll need it for polling and getting the exported file
        return export.Id;
    }