niklasvh / html2canvas

Screenshots with JavaScript
https://html2canvas.hertzen.com/
MIT License
30.39k stars 4.78k forks source link

not able to capture svg image #1578

Open Manaskumar119 opened 6 years ago

Manaskumar119 commented 6 years ago

not able to capture svg image ..

Any Solution ,Please help

Cellcote commented 6 years ago

+1, also running into this issue.

alansouzati commented 6 years ago

+1 here is a codesandbox that reproduces the issue:

https://codesandbox.io/s/wo3oww275k

motodimago commented 6 years ago

I can not capture this.

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48">...</svg>

But I can capture this svg.

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48" width="48" height="48">...</svg>

I just add width and height.

muratcatal commented 6 years ago

I want to add another solution based on @motodimago 's solution, that is for dynamic created svg's which you can not add width or height staticly. I faced that problem with amcharts, which i can not add width and height to created svg charts.


html2canvas(this.ImageCanvas,
        {
            onclone: (element) => {
                const svgElements: any[] = element.body.getElementsByClassName("printable-area")[0].getElementsByTagName("svg");
                Array.from(svgElements).forEach((svgElement) => {
                    const bBox: any = svgElement.getBBox();
                    svgElement.setAttribute("width", bBox.width);
                    svgElement.setAttribute("height", bBox.height);
                });
            },
        }).then((canvas) => {
            document.body.appendChild(canvas);
        });
Cellcote commented 6 years ago

Thanks @muratcatal , this fixes most of our issues.

jacknkandy commented 6 years ago

Thanks @muratcatal

Haroenv commented 4 years ago

Just adding here for SEO, this only happened on Chrome for me, not on Safari