2

I want to create a .png file of a HTML page in angularjs and download it. For this I'm currently using dom-to-image.js and using the domToImage.toBlob function and passing the node element to it. But internally when it goes to dom-to-image.js it throws the error:

node.cloneNode() is not a function

Can anyone please assist me here?

Thanks

Yehor Androsov
  • 4,885
  • 2
  • 23
  • 40

1 Answers1

2

This error arises when you attempt to call cloneNode() on anything other than a single DOM node.

In this case, the error is coming from the dom-to-image library, which calls that method internally.

Note that without a code snippet, its hard to identify the precise issue with your code, but the point is, when you call domtoimage.toBlob(), you need to supply a single DOM node.

So double check what you are calling it with. If you are selecting by class, for instance, you could end up with more than one element.

Its best practice to be precise with which node you want to convert to a blob - use the id attribute, like this:

domtoimage.toBlob(document.getElementById('element')).then(...)

where element is the id of your target node.

Its also possible you're selecting with angular.element() or even using jQuery directly.

In both cases, this returns an Object -- which you can't call cloneNode() on.

Also note the following from the Angular docs for angular.element():

Note: All element references in AngularJS are always wrapped with jQuery or jqLite (such as the element argument in a directive's compile / link function). They are never raw DOM references.

Which means you would observe the same behavior in both cases, e.g. both of these:

domtoimage.toBlob($('#element')).then(...)
domtoimage.toBlob(angular.element('#element')).then(...)

would result in the error you see. You can index the Object before supplying it to domtoimage.toBlob(), perhaps like this:

domtoimage.toBlob($('#element')[0]).then(...)
domtoimage.toBlob(angular.element('#element')[0]).then(...)

and that would also work.

Also check out this post about "cloneNode is not a function".

costaparas
  • 5,047
  • 11
  • 16
  • 26