How to add a header to a captured image with html2canvas?

Asked

Viewed 55 times

1

I am trying to issue a subscription voucher on a page, and in it you have the option to save the voucher as image, on the voucher page DO NOT have the header with the company logo and the name of the signature, i want to add this to the image but without it appearing to my user,the code below and what I have and generates the image perfectly the way I want, I just need to add a header in the image with the logo and any writing in front.

function gerarImagem() {
  html2canvas(document.getElementById('telaPrint')).then(function(canvas) {
    var name = 'img-print';
    let xhr = new XMLHttpRequest();
    xhr.responseType = 'blob';
    xhr.onload = function() {
      let a = document.createElement('a');
      a.href = window.URL.createObjectURL(xhr.response);
      a.download = name + '.png';
      a.style.display = 'none';
      document.body.appendChild(a);
      a.click();
      a.remove()
    };
    xhr.open('GET', canvas.toDataURL("image/png", 1.0));
    xhr.send();
  });

}
<!doctype html>
<html>

<head>
  <meta charset="utf-8">
  <title>34571073</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>
</head>

<body>

  <div id="telaPrint" style="background-color: #fff">
    <pre>
        dsadadadada
        asdadadsada
    
        adadada
    
        adadadasdada
      </pre>
  </div>
  <button onClick="gerarImagem()">img</button>
</body>

in case the above code does not work for Cdn has this codepen.

sorry for the inexperience here at stackoverflow editor

1 answer

1

We can add the header before converting and place the encoded image in Base64.

function gerarImagem() {

    var telaPrint = document.getElementById('telaPrint');
    var header = document.createElement("h1");
    header.textContent = "Título";
    telaPrint.prepend(header);
    var img = new Image();
    img.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAWgAAADICAYAAADFjnuPAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyRpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUu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emzEcsjZfCDSXbv2P7lsVotx8IdCkLOZMiDYhzFuMYdzz2FKbLwSaxMSct9vmafuqxRimHTz+5sCW4nwh0CQm5nhmm0tcvrUYw7TnsaU2Xwg0Ca6gq1ZkTT/JY5VVXzfi2P6LEFUd5oi9oP8lzBcCTYJirgjX5PPwHjrY96yj/VzKfCHQXGCg85rRecq6uVzoXeTY7s0XAk2KYi96X6zwYj6CahG5Gpxl1ceZZ5FP6Yt9XmenuRTnkOcLgSZBsRcHKlaqPw+s9vLwtfuO9/kcebviGO6PELtDq9HPhocfzmm+GDhXs7sMbUIz37M6K54+x7y49Ra2cWlFV8SmzpssbrL4sxHqjG0d5uWpdN9ynLtaYQ95vhBoBqDNi2PzAyvVmA9FLcewyRkU4xqr4vLYYj6jcN+qui9Dni8c4iAxRQiWJ9jPaxb3LsHvY2tyv0udLwSaBE17Xq09t3iqPg1B7DNW45rjG/J8IdAkqDi17b3jEO5Wm3ctH2caxtZHtIrH/Mjqnykx5PlCoEl0JV3EqovzbmcdR3US4vXYURQnYaXaZnU+5PlCoEnQ7rjvsmF4bsJ9l1k/b0+ehyg+Nnz83eGJj45WqkOfLwbCWRx0/dS/2IozKIqzEPLSn7/fbly6/SkiMw6hnmdf1+XYncK2b3yT7OuNJpMLnC8GYLTZbM5y4L9fXjZ+fECsX7e3o3Mbs0McAAINgEADCDQAAg0g0AAINAACDSDQAAg0gEADINAACDSAQAMg0AACDYBAAwg0AAINgEADCDQAAg0g0AAINACpBHrkxwek3AsraACB9lsR0Ik6rhKa/I3/D4GUFnCjzUbXAAQaAIEGEGgABBpAoAEQaAAEGkCgARBoAIEGQKABEGgAgQZAoAEEGgCBBhBoAAQaAIEGEGgABBpAoAEQaAAEGkCgARBoAIEGQKABBBoAgQZAoAEEGgCBBhBoAAQaAIEGEGgABBpAoAEQaACBNgsAAg2AQAMINAACDSDQAAg0AAININAAtPZHgAEASgSjajGP/6kAAAAASUVORK5CYII="
    img.onload = function() {
        telaPrint.prepend(img);
        converter();
        header.remove();
        img.remove();
    }

}

function converter(){

    html2canvas(document.getElementById('telaPrint')).then(function (canvas) {
    var name = 'img-print';
    let xhr = new XMLHttpRequest();
    xhr.responseType = 'blob';
    xhr.onload = function () {
        let a = document.createElement('a');
        a.href = window.URL.createObjectURL(xhr.response);
        a.download = name + '.png';
        a.style.display = 'none';
        document.body.appendChild(a);
        a.click();
        a.remove()
    };
    xhr.open('GET', canvas.toDataURL("image/png", 1.0));
    xhr.send();
    });

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.