17

I am working on a project where I need to generate a pdf file (onclick saveFile as PDF) on the client side as some data are not to be sent to the server. I have read about these options available; fileSaver.js and html2canvas, and none seem to help. I also want to point out that I am not so good with javascript. Here is my js code

<script type="text/javascript">
    $(document).ready(function() {
        $("#saveOutput").click(function() {
            $("#Screenshot").html2canvas({ 
                onrendered: function(canvas) {
                    var png = canvas.toDataURL()
                    window.open(png);
                }
            });
        });
    });
</script>

and my Html

<div id="Screenshot">
    <p>This is it</p>
    <svg xmlns:xlink="http://www.w3.org/1999/xlink" width="250" height="250" style="height: 250px; width: 250px;">       
          <circle id="1" cx="100" cy="100" r="25" fill="#A52A2A"></circle>
    </svg>
</div>
<div id="content">
    <a class="button" id="saveOutput" href="#">Save as File</a>
</div>

Why cant it shows the SVG file? How can these files be converted to Canvas?could someone shows me with code sample? Any help will be appreciated.

Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42
Kennan Obura
  • 315
  • 1
  • 4
  • 14
  • To plot the svg's you've to use "canvg.js", it converts all the svgs into elements refer [canvg.js](https://github.com/gabelerner/canvg) – GeekExplorer Nov 24 '15 at 10:55

5 Answers5

35

Like already said in other answers: the latest html2canvas support svg elements.

But apparently CSS is not applied and has a bug when is set the css "width" style and svg "width" attribute.

Before the screenshot, I added a treatment to svg elements.

var svgElements = document.body.querySelectorAll('svg');
svgElements.forEach(function(item) {
    item.setAttribute("width", item.getBoundingClientRect().width);
    item.setAttribute("height", item.getBoundingClientRect().height);
    item.style.width = null;
    item.style.height= null;
});

Edit: added height based on comment

Eonasdan
  • 7,563
  • 8
  • 55
  • 82
Isabella Lopes
  • 526
  • 6
  • 9
7
  • Solution 1 : You can use latest html2canvas which will support svg elements.
  • Solution 2 : Before calling html2canvas function you can convert all existing svg elements into canvas elements, which is supported by html2canvas. For this conversion you can use canvg.js.
  • On StackOverflow we care about high quality questions and answers. Please read [How do I write a good answer](https://stackoverflow.com/help/how-to-answer) before you answer a question again. This will ensure that your answers live up to Stackoverflow's standards. – Noel Widmer Aug 31 '17 at 10:23
  • While this might be a valuable hint to solve the problem, a good answer also demonstrates the solution. Please [edit] to provide example code to show what you mean. Alternatively, consider writing this as a comment instead. – Toby Speight Aug 31 '17 at 12:24
  • 3
    I am using the latest version as of `v1.0.0-alpha.12`, however, I still can't render `SVG` tags, even very simple graphs. – AGamePlayer Jan 02 '19 at 10:54
  • 2
    Well, I do can render svg but I can only render `svg` tags when width specified. – AGamePlayer Jan 02 '19 at 11:15
  • 1
    @AGamePlayer Thank you! I resolve the problem adding "width" to the svg – Lucas Moyano Angelini Mar 28 '20 at 23:38
2

Faced the same issue.

Moved all the CSS into a container element (div) and applied the CSS (in my case, position:absolute;) to the container element instead of the svg element.

As Isabella Lopes has pointed out correctly, height and width need to be applied as attributes and not styles.

mimetyped
  • 21
  • 3
0

in my case I had a margin-top, which moved the svg outside of the visible area (worked perfect in html). Maybe helpful for someone... just replace margin by padding of the parent element.

-1

try to download the latest version of html2canvas on the following link

https://github.com/niklasvh/html2canvas/releases

at the moment it's version 0.5.0-alpha1

i tried it and it works for me.

jay
  • 1,055
  • 3
  • 15
  • 35