Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Sytriox
New Member

Capture Screenshot of Custom Visual with Button

Hi,

I am currently developing a custom visual for Power BI and I would like to add a feature that allows users to take a screenshot of the visual.

 

The idea is to include a button within the visual, and when clicked, it would capture a screenshot of the visual, potentially using an external library like html2canvas.

 

I am aware that Power BI provides a context menu solution to copying it , but this approach won’t work for me due to limitations.

Given that Power BI hosts custom visuals within an iframe, is it possible to implement this functionality on the client side? Can html2canvas or a similar library be used to capture the visual's content?

 

I would appreciate any insights or advice on how to achieve this.

 

Thanks in advance!

2 REPLIES 2
Sytriox
New Member

Hi @Anonymous 

I wanted to follow up and thank you for your earlier response to my question about using html2canvas in my Power BI custom visual!

The error I am encountering is:

 

Uncaught (in promise) SecurityError: Failed to read a named property 'document' from 'Window': Blocked a frame with origin "null" from accessing a cross-origin frame.

 

 

From my understanding, this issue is happening because Power BI hosts custom visuals inside an iframe. This is likely what’s causing the problem, as html2canvas tries to access the visual's content and is blocked due to cross-origin policies.

Unfortunately, I'm not sure how to resolve this. Is there any  approach I can take to capture a screenshot of the visual? I’ve tried using the useCORS and allowTaint options in html2canvas, but that doesn’t seem to fix the issue.

If anyone has any insights or possible workarounds for dealing with this iframe sandboxing limitation, I would really appreciate your help!

Thanks again!

Best regards,

Anonymous
Not applicable

Hi @Sytriox ,

You can refer the following links to get it:

About | html2canvas

import * as d3 from "d3";
// Other imports as needed
import "html2canvas";

export class MyVisual implements IVisual {
    // Visual properties and constructor

    private captureButton: HTMLButtonElement;

    constructor(options: VisualConstructorOptions) {
        // Your constructor code

        this.captureButton = document.createElement("button");
        this.captureButton.innerText = "Capture Screenshot";
        this.captureButton.onclick = this.captureScreenshot.bind(this);
        options.element.appendChild(this.captureButton);
    }

    public update(options: VisualUpdateOptions) {
        // Your update code

        // Optionally append the button to the visual's DOM if it isn't already there
        if (!this.captureButton.parentElement) {
            options.element.appendChild(this.captureButton);
        }
    }

    private captureScreenshot() {
        html2canvas(document.querySelector('YOUR_VISUAL_ROOT_ELEMENT_SELECTOR')).then(canvas => {
            const link = document.createElement('a');
            link.download = 'screenshot.png';
            link.href = canvas.toDataURL();
            link.click();
        });
    }
}

In addition, you can raise a ticket in the Custom Visuals Development community for dedicated support.

Custom Visuals Development Discussion - Microsoft Fabric Community

Best Regards

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.