Estoy usando html2canvas para convertir el html a png, recibo el siguiente error:
html2canvas.min.js:20 Uncaught (in promise) Error: Document is not attached to a Window at html2canvas.min.js:20:193237 at html2canvas.min.js:20:1976 at Object.next (html2canvas.min.js:20:2081) at html2canvas.min.js:20:1023 at new Promise (<anonymous>) at a (html2canvas.min.js:20:774) at Vs (html2canvas.min.js:20:192912) at html2canvas.min.js:20:196186 at takeScreenShot (set style [modal].html:94:7) at action (set style [modal].html:68:11) let htmlString = document.documentElement.innerHTML; let virtualDom = new DOMParser().parseFromString(htmlString, "text/html"); html2canvas(virtualDom.body).then((canvas) => { let base64image = canvas.toDataURL("image/png"); }); Si paso el DOM del navegador, está bien, pero necesito un nuevo DOM por alguna razón, por eso uso DOMParser . pregunta similar: enlace
No hay una forma adecuada de hacerlo ahora. Retransmisión html2canvas en el objeto de ventana adjunto al elemento . Debido a que html2canvas ya permite pasar opciones relacionadas con defaultView a través del objeto de opciones, la protección que arroja un error parece un error porque debe verificar que se pasen todas las opciones necesarias. ¡Así que sería mejor abrir un problema allí!
Como solución, si confía en HTML, puede intentar usar iframe oculto, pero aún debe colocarse en el documento
const html = "<div>Hello World!</div>"; const iframe = document.createElement("iframe"); document.body.appendChild(iframe); // 👈 still required iframe.contentWindow.document.open(); iframe.contentWindow.document.write(html); iframe.contentWindow.document.close(); html2canvas(iframe.contentWindow.document.body);