Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!