VondeTaconadis
VondeTaconadis

Reputation: 84

Error while trying to generate a jspdf with html2canvas

I'm using react and I'm trying to make a pdf with html2canvas and jspdf. I want to give in input to html2canvas a that I make at the moment. When I try to make the pdf I get this error:

 index.ts:36 Uncaught (in promise) Error: Element is not attached to a Document

Here is the code:

 const input = (<div><h2>Test</h2></div>);
html2canvas(input).then((canvas)=>{
  const imgData = canvas.toDataURL('image/png');
    const pdf = new jsPDF();
    pdf.addImage(imgData, 'JPEG', 0, 0);

    pdf.save("download.pdf")
})

Upvotes: 0

Views: 1850

Answers (1)

sandeep.kgp
sandeep.kgp

Reputation: 886

html2canvas accepts DOM elements but in your case input is not attached to the DOM.

if in case you want to generate custom template as pdf then you can do var node = document.createElement("div"); node.appendChild(yourElement) & hide it (if needed) from the dom by mentioning position fixed and top:-5000px and then use that for generating pdf

documentation: https://html2canvas.hertzen.com/getting-started

Upvotes: 0

Related Questions