html to png

I've been looking for a way to convert a html code into an image, and after several reviews decided that html2cancas.js satisfy my requirements, however I'm having difficulty getting it to work.

What I'm trying to do is:-
<html>
<head>
<script src="jquery-1.9.1.js"></script>
<script src="html2canvas.js"></script>
<script>
    function test() {
		var $strHTML = "<strong>hello</strong>";
        html2canvas($strHTML, {
            onrendered: function(canvas) {
				document.body.appendChild(canvas);
                var dataUrl = canvas.toDataURL();
				$("#testImg").attr("src", "data:image/png;base64," + dataUrl);
                }
            });
      }
</script>
</head>
<body>
<div onClick="test();">test</div>
<img id="testImg" />
</body>

</html>

Open in new window



Im not getting any error messages, so Im a bit confused at whats going wrong. According to the documentation I can replace $strHTML with document.body, which works with "document.body.appendChild(canvas);" but not inserting into the image src.

Any ideas how I can fix this / get this working?

Thank you
tonelm54Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rainer JeschorCommented:
Hi,
I had also troubles - it seems the element has to be part of the document DOM otherwise you get errors inside of the html2canvas.js.

The following code worked in my environment - tested with IE10 and FF23:
<!doctype html>
<html>
  <head>
    <title>Test HTML2Canvas</title>
    <script src="jquery-1.9.1.js"></script>
    <script src="html2canvas.js"></script>
	<script type="text/javascript">
		function test() {
		var htmlElement = document.createElement("strong"); //$('<div>hello World</div>');
		var newContent = document.createTextNode("Hello world");
		htmlElement.appendChild(newContent); //add the text node to the newly created div.
		htmlElement.style.visibility ="hidden";
		my_div = document.getElementById("caller");
		document.body.insertBefore(htmlElement, my_div);
        html2canvas(htmlElement, {
            onrendered: function(canvas) {
                $("#testImg").attr("src", canvas.toDataURL());
                }
            });
      }

	</script>
  </head>
  <body>
	<div id="caller" onclick="test();">Generate PNG</div>
	<img id="testImg" />
  </body>
</html>

Open in new window


HTH
Rainer
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.