Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

162
Visualizações
save chart html to word file as image

i want to save chart html to word file as image

but word file can download but chart image is just X image it means doesn't have data

could you give me solution

  function ExportToDoc(filename = "") {
    html2canvas(document.getElementById("pyramid")).then((canvas) => {
      canvas.toBlob((blob) => {
        if (blob) {
          myBlob = blob;
        }
      });
    });
    var HtmlHead =
      "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'><head><meta charset='utf-8'><title>Export HTML To Doc</title></head><body>";
    var EndHtml = "</body></html>";
    //complete html
    // var html = HtmlHead + document.getElementById("pyramid").innerHTML + EndHtml;
    var html =
      HtmlHead +
      "<img src='" +
      URL.createObjectURL(
        new Blob(new Uint8Array(myBlob), { type: "image/png" })
      ) +
      "'/>" +
      EndHtml;
    // "<div id='export'><img class='hide' src='" + URLObj.createObjectURL() +"'/></div>"
    //specify the type
    var blob = new Blob(["\ufeff", html], {
      type: "application/msword",
    });
    // Specify link url
    var url =
      "data:application/vnd.ms-word;charset=utf-8," + encodeURIComponent(html);
    // Specify file name
    filename = filename ? filename + ".doc" : "document.doc";
    // Create download link element
    var downloadLink = document.createElement("a");
    document.body.appendChild(downloadLink);
    if (navigator.msSaveOrOpenBlob) {
      navigator.msSaveOrOpenBlob(blob, filename);
    } else {
      downloadLink.href = url;
      downloadLink.download = filename;
      downloadLink.click();
    }
    document.body.removeChild(downloadLink);
  }

enter image description here

for HTML

<Chart
              id="pyramid"
...
            </Chart>

chart is working well

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

  function ExportToDoc(filename = "") {
    var img = new Image();
    html2canvas(document.getElementById("pyramid")).then((canvas) => {
      img.src = canvas.toDataURL();

      var HtmlHead =
        "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'><head><meta charset='utf-8'><title>Export HTML To Doc</title></head><body>";
      var EndHtml = "</body></html>";
      var html =
        HtmlHead + "<img class='hide' src='" + img.src + "'/>" + EndHtml;

      var blob = new Blob(["\ufeff", html], {
        type: "application/msword",
      });
      var url =
        "data:application/vnd.ms-word;charset=utf-8," +
        encodeURIComponent(html);
      filename = filename ? filename + ".doc" : "Document.doc";
      var downloadLink = document.createElement("a");
      document.body.appendChild(downloadLink);
      if (navigator.msSaveOrOpenBlob) {
        navigator.msSaveOrOpenBlob(blob, filename);
      } else {
        downloadLink.href = url;
        downloadLink.download = filename;
        downloadLink.click();
      }
      document.body.removeChild(downloadLink);
    });
  }

I solved it by myself.

Also upgraded:

  function htmlToFile(element, file = "hwp") {
    var img = new Image();
    html2canvas(document.getElementById("pyramid")).then((canvas) => {
      img.src = canvas.toDataURL();

      var header = "<html><head><meta charset='utf-8'></head><body>";
      var footer = "</body></html>";
      var sourceHTML =
        header + "<img class='hide' src='" + img.src + "'/>" + footer;

      var source =
        "data:application/vnd.ms-word;charset=utf-8," +
        encodeURIComponent(sourceHTML);
      var fileDownload = document.createElement("a");
      document.body.appendChild(fileDownload);
      fileDownload.href = source;
      fileDownload.download = "Document." + file;
      fileDownload.click();
      document.body.removeChild(fileDownload);
    });
  }
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda